Jump to content
  • Advertisement
Sign in to follow this  
xllx_relient_xllx

free'ing memory from..

This topic is 4960 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Advertisement
You need to manually free all the memory you allocate. free won't look in the data and free the pointers.

Share this post


Link to post
Share on other sites
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
I don't use class'es as I have'nt yet jumped into C++:D
So I would do something like this:

typedef struct _MyNode {
char *string;
struct _MyNode *next;
}TypeNode;

TypeNode *ptrNode;

free(ptrNode);
free(ptrNode->string);

But I get what your saying and thanks.

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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!