d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > Trouble With Freeing Allocated Memory
12Next
Add Reply New Topic New Poll
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Mar 12 2013 10:53pm
Not sure why my code doesn't free memory, and it seems that the currently allocated memory is lost?

When I run valgrind I get the following leaks, for example for rmve_sec (which removes the 2nd element of the link list) I get the following output

The problem isn't where I allocate memory so I'm assuming there is something wrong with my functions?


rmve_sec output from valgrind:



rmve_head output from valgrind:



Code
void rmve_sec(Node *list) {
 Node *temp;

 if (list != NULL && list->next != NULL) {
   temp= list->next;
   list->next= list->next->next;
   free(temp);
 }
}

int rmve_head(Node **start) {
 Node *temp= NULL, *curr;
 int result= 0;

 if (start != NULL && *start != NULL) {

   result= 1;
   temp= *start;
   curr = *start;
   curr = curr->next;
   (*start) = curr;
   free(temp);

 }

 return result;
}


This post was edited by lopelurag on Mar 12 2013 10:55pm
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Mar 12 2013 11:20pm
Are you missing a for loop in that remove function?

anyway, drawing pictures always helps with pointer problems


consider my drawing: (Not an artist FYI)




Notice:

Node *temp = you are allocating space for another node object

Temp points to list->next

you free temp (which was list->next)

but it would appear to me that whatever memory temp used originally never got freed?

Now I am not a C master by any means but it seems to me that you would need to free another thing

This post was edited by Eep on Mar 12 2013 11:21pm
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Mar 12 2013 11:29pm
Quote (Eep @ Mar 13 2013 01:20am)
Are you missing a for loop in that remove function?

anyway, drawing pictures always helps with pointer problems


consider my drawing: (Not an artist FYI)

http://img685.imageshack.us/img685/4328/pointerlulz.jpg


Notice:

Node *temp = you are allocating space for another node object

Temp points to list->next

you free temp (which was list->next)

but it would appear to me that whatever memory temp used originally never got freed?

Now I am not a C master by any means but it seems to me that you would need to free another thing


Node *temp doesn't allocate memory, it just creates a pointer to a node

Temp is just a pointer to a node, temp eventually points to the allocated space at list->next

Freeing temp, should free the allocated memory at the address it points to

There doesn't need to be a loop in the removal of the 2nd element because it can be done easily without one and there's no need to iterate through the entire list

This post was edited by lopelurag on Mar 12 2013 11:30pm
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Mar 13 2013 12:52am
Quote (Eep @ Mar 13 2013 01:20am)
Are you missing a for loop in that remove function?

anyway, drawing pictures always helps with pointer problems


consider my drawing: (Not an artist FYI)

http://img685.imageshack.us/img685/4328/pointerlulz.jpg


Notice:

Node *temp = you are allocating space for another node object

Temp points to list->next

you free temp (which was list->next)

but it would appear to me that whatever memory temp used originally never got freed?

Now I am not a C master by any means but it seems to me that you would need to free another thing


freeing and mallocing memory is the same thing in c++ pretty much. not knowing C is not an excuse.

@op have you tried compiling with -g and running your app through gdb? try printing out the addresses of list->next and temp to see if they are the same. also how are nodes added (does the list actually work/have you tested it).
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Mar 13 2013 12:55am
Quote (AbDuCt @ Mar 13 2013 01:52am)
freeing and mallocing memory is the same thing in c++ pretty much. not knowing C is not an excuse.

@op have you tried compiling with -g and running your app through gdb? try printing out the addresses of list->next and temp to see if they are the same. also how are nodes added (does the list actually work/have you tested it).


I just always forget about how free works
Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Mar 13 2013 12:58am
Quote (Eep @ Mar 13 2013 02:55am)
I just always forget about how free works


how is that even possible.

its a function that accepts a pointer to free memory the pointer is pointing at. its not even that complicated to use and the name of the function gives away its purpose...
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Mar 13 2013 01:02am
Quote (AbDuCt @ Mar 13 2013 01:58am)
how is that even possible.

its a function that accepts a pointer to free memory the pointer is pointing at. its not even that complicated to use and the name of the function gives away its purpose...


okay I misworded that

I forgot that pointers themselves don't need to be freed but only when they point to something created by malloc etc.

Member
Posts: 13,425
Joined: Sep 29 2007
Gold: 0.00
Warn: 20%
Mar 13 2013 01:11am
This post is a violation of the site rules and appropriate action was taken.

Quote (Eep @ Mar 13 2013 03:02am)
okay I misworded that

I forgot that pointers themselves don't need to be freed but only when they point to something created by malloc etc.


same concept as using the `new` and `delete` keywords in c++. youre still retarded.
Member
Posts: 23,862
Joined: Aug 16 2006
Gold: 20.00
Mar 13 2013 02:17am
Quote (AbDuCt @ Mar 13 2013 02:11am)
same concept as using the `new` and `delete` keywords in c++. youre still retarded.


whoa there buddy I think you need to calm the fuck down
Member
Posts: 24,101
Joined: Nov 8 2007
Gold: 5,561.70
Mar 13 2013 07:29am
Quote (AbDuCt @ Mar 13 2013 02:52am)
freeing and mallocing memory is the same thing in c++ pretty much. not knowing C is not an excuse.

@op have you tried compiling with -g and running your app through gdb? try printing out the addresses of list->next and temp to see if they are the same. also how are nodes added (does the list actually work/have you tested it).


Yeah the list actually works, memory is allocated correctly. It seems to be that my remove functions don't work correctly.

They work correctly, as in they remove the correct element, but for some reason there is a memory leak.

Think I fixed the mem leaks after printing out the malloc addresses before and after freeing

This post was edited by lopelurag on Mar 13 2013 07:45am
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll