free'ing memory from..
..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?
You need to manually free all the memory you allocate. free won't look in the data and free the pointers.
I assume you have something like this:
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
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
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.
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.
Also, you'll only want to free mallocd nodes. That code frees a compile-time allocated node, which won't work.
By the way, here's some incentive to jump into C++ ;) :
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.
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.
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:
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.
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).
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement