Sign in to follow this  
xllx_relient_xllx

free'ing memory from..

Recommended Posts

I assume you have something like this:


class Node
{
char * string;

public:
Node()
{
string = new char[100];
}
};

...
Node * node = new Node();
delete node;



And the answer is yes. You need to manually delete the memory allocated for string, just deleting the class its pointer is contained in will not free the memory.

Alan

Share this post


Link to post
Share on other sites
You'd actually want to free the string first. The way you have it now will free the node before it frees the string and that memory isn't guaranteed to exist after you free it.

Also, you'll only want to free mallocd nodes. That code frees a compile-time allocated node, which won't work.

Share this post


Link to post
Share on other sites
By the way, here's some incentive to jump into C++ ;) :

class Node
{
public:
char* string;
Node()
{
string = new char[100];
}

~Node()
{
delete[] string;
}
};



This way when you go to delete a Node* (using new and delete in place of malloc() and free(), respectively) the string memory will be freed properly.

Share this post


Link to post
Share on other sites
Heh, that's nice SoulSkorpion but I'm gonna have to stick with Just C for now.

Anyway, I ran into a "maybe" problem here. It's the first time I've made a function solely for free'ing up Memory just before the program closes out. And It seems that it's not deleting the nodes after calling free();

Here's a little test I did:


void _FREE_MEMORY(void)
{
node_t* current, *temp;
current = temp = NULL;

if (!(current = head))//if list is empty, don't continue
return;

while (current != NULL)
{
printf("name before free: %s\n", current->name);
free(current->name);
printf("name after free : %s\n\n", current->name);
current = current->next;

}

printf("\n");
current = head;
while (current != NULL)
{
temp = current->next;
printf("node before free: %d\n", current);
free(current);
printf("node after free : %d\n\n", current);
current = temp;
}
}

Output:

name before free: Bush
name after free : ¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„

name before free: Gore
name after free : ¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„

name before free: Kerry
name after free : ¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„


node before free: 115745880
node after free : 115745880

node before free: 115746000
node after free : 115746000

node before free: 115746120
node after free : 115746120


Good Bye!
Press any key to continue . . .



Obviously, It looks like it does free up the names based on the output results but the nodes still have the same address' after free'ing them up. hmm.. is there something wrong in my code?

p.s: yeahhh.. so I try to make my code political, sue me! lol.

Share this post


Link to post
Share on other sites
Quote:
free(current->name);
printf("name after free : %s\n\n", current->name);


This is illegal. Once the memory has been freed, you are not allowed to touch it at all, in any circumstance.

What happened here is that though you have released the memory, the computer hasn't yet reused it for something else. That is all. It would have also been possible for your program to simply crash (access violation).

Share this post


Link to post
Share on other sites
To answer the question, though, it's perfectly reasonable that the "nodes still have the same address' after free'ing them up", because you are indeed printing values of node pointers. And those are just numbers that correspond to locations in memory. You still have a pointer to the bit of space where the node used to be - it's just that there isn't a node there any more - or in fact, any memory that you're allowed to touch. That is, your pointer is "dangling", pointing off into the middle of nowhere, which makes it just as non-kosher to dereference as NULL is.

Of course, these dangling pointers are part of other nodes that you're deleting anyway, so it's no real problem. Except for the 'head' pointer of course, but you won't be using it any more after this, I assume. If you had several 'head pointers' in the program and you wanted to delete lists one at a time in the middle of the program, then you would end up with an invalid head pointer after deleting the corresponding list. It's probably a good idea to set such a pointer to NULL, so that you can check for its invalidity more easily down the line. There is no way in general to check if a pointer points to "valid" memory (or even to memory that you own); but you do know that address 0 (NULL) is never valid (on typical architectures, anyway - on embedded devices etc. it might be different...).

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this