free'ing memory from..

Started by
10 comments, last by Zahlman 19 years, 5 months ago
..nodes. When free'ing memory from a node, does it free just the node or it's members too, such as, char *string;? or would I need to free(string) too?
Advertisement
You need to manually free all the memory you allocate. free won't look in the data and free the pointers.
Ra
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
"There will come a time when you believe everything is finished. That will be the beginning." -Louis L'Amour
More work but it's all good I guess. "more control more power"

Thanks.
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.
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.
Ra
Yeah I knew that heh, Thanks though.
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.
-------------"On two occasions, I have been asked [by members of Parliament], 'Pray, Mr. Babbage, if you put into the machine wrong figures, will the right answers come out?' I am not able to rightly apprehend the kind of confusion of ideas that could provoke such a question."- Charles Babbage (1791-1871)
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: Bushname after free : ¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„name before free: Gorename after free : ¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„name before free: Kerryname after free : ¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„¨„node before free: 115745880node after free : 115745880node before free: 115746000node after free : 115746000node before free: 115746120node 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.
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).
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan

This topic is closed to new replies.

Advertisement