Solved

why memory is not freed?

Posted on 2008-10-30
8
168 Views
Last Modified: 2012-05-05
Hello group,

I was reading a book which has a nice example to start with link lists (in snippet) but I'm wondering why allocated memory is not given back to system?!
Is this an exception or is missed?

since I'm still learning I need to make sure things I'm learning here are right.

Regards,
ak

#define RECORDS 6
 

int main()

{

  struct jb {

    char actor[25];

    struct jb *next;

};
 

  char *bonds[RECORDS] =  {

     "Sean Connery",

     "David Niven",

     "George Lazenby",

     "Roger Moore",

     "Timoth Dalton",

     "Pierce Brosnan"
 

  };
 

  struct jb *first_item;

  struct jb *current_item;

  struct jb *new_item;

  int index = 0;
 

  first_item = (struct jb *)malloc(sizeof(struct jb));

  current_item = first_item;
 

  //this loop is only for feeding the structure

  while(1)

  {

    strcpy(current_item->actor, bonds[index]);

    index++;

    if (index< RECORDS)

    {

      new_item=(struct jb *)malloc(sizeof(struct jb));

      current_item->next = new_item;

      current_item       = new_item;

    }

    else {

      current_item->next = NULL;

      break;

    }

  }
 

  current_item = first_item;   /*start over*/

  index = 1;
 

  while(current_item)

  {

    printf("Structure %d: ", index++);

    printf("%s\n", current_item->actor);

    current_item = current_item->next;

  }
 

  return 0;
 

}

Open in new window

0
Comment
Question by:akohan
  • 2
  • 2
  • 2
  • +2
8 Comments
 
LVL 28

Expert Comment

by:chilternPC
ID: 22846550
you hAVE TO UN-MALLOC THE MEMORY WHEN YOU HAVE FINISHED WITH IT
OTHERWISE IT WILL BE RELEASED WHEN THE PROGRAM ENDS
(SORRY CAPS ON)
0
 

Author Comment

by:akohan
ID: 22846566

So, using fee() function only is enough?
e.g.


  free(first_item);
  first_item=NULL:
and etc...


0
 
LVL 53

Expert Comment

by:Infinity08
ID: 22846570
>> (SORRY CAPS ON)

lol. You know you can turn it off, don't you ? ;)


akohan, dynamically allocated memory has to be explicitly free'd using free :

        http://www.cplusplus.com/reference/clibrary/cstdlib/free.html
0
 
LVL 84

Expert Comment

by:ozo
ID: 22846581
if not explicitly freed, the allocated memory is given back to system when the program exits
0
PRTG Network Monitor: Intuitive Network Monitoring

Network Monitoring is essential to ensure that computer systems and network devices are running. Use PRTG to monitor LANs, servers, websites, applications and devices, bandwidth, virtual environments, remote systems, IoT, and many more. PRTG is easy to set up & use.

 
LVL 53

Expert Comment

by:Infinity08
ID: 22846585
>> So, using fee() function only is enough?

For every malloc, you need to have a corresponding free. So, in your case, every node in the linked list needs to be free'd.
0
 
LVL 28

Expert Comment

by:chilternPC
ID: 22846630
p.s. only free the memory once you have finished using it. :-)
if you write to the memory  after you have sent it back the program will throw exeption

cool,  I like this CAPs ON CAPS off  toggle Key , although my keyboard has
stupidly marked as 'caps lock'  :-))
0
 
LVL 23

Expert Comment

by:Mysidia
ID: 22847683
Actually, no.

Usually no exception will be thrown if you attempt to write to free()'d memory.
What happens in practice is far more insidious.

Writing to free()'d memory will very likely corrupt the heap, and (if you're lucky) cause your program to crash the next time you attempt to allocate or free memory.

If you are unlucky, another malloc()'  call will have assigned that memory, and you will be silently corrupting data  elsewhere in your program.


If you want to release the memory,  you should free it after you take it out of your list, and perform no further operations on removed nodes, once they are freed.

 





struct jb* temp;
 

  while(current_item)

  {

    printf("Structure %d: ", index++);

    printf("%s\n", current_item->actor);

    temp = current_item;

    current_item = current_item->next;
 

    free(temp); /* temp is no longer in the list, now free it... */

  }

Open in new window

0
 
LVL 84

Accepted Solution

by:
ozo earned 50 total points
ID: 22847723
If the he value of a pointer that refers to space deallocated by a call to the free or
realloc function is used, the behavior is undefined.
0

Featured Post

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Binary Bomb: Phase 4 func4 3 310
C#, VS15, StructLayout 1 122
List out all word 7 261
How to copy documents folder to network folder automatically as backup? 15 84
Have you thought about creating an iPhone application (app), but didn't even know where to get started? Here's how: ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ ~ Important pre-programming comments: I’ve never tri…
Windows programmers of the C/C++ variety, how many of you realise that since Window 9x Microsoft has been lying to you about what constitutes Unicode (http://en.wikipedia.org/wiki/Unicode)? They will have you believe that Unicode requires you to use…
The goal of this video is to provide viewers with basic examples to understand and use pointers in the C programming language.
Video by: Grant
The goal of this video is to provide viewers with basic examples to understand and use for-loops in the C programming language.

895 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

16 Experts available now in Live!

Get 1:1 Help Now