Reputation: 1823
i have this code:
#include <stdio.h>
#include <stdlib.h>
typedef struct node{
struct node* next;
int data;
}NODE;
NODE* makeNode (int x){
NODE* new;
new = (NODE*)malloc(sizeof(NODE));
new -> data = x;
new -> next= NULL;
return new;
}
typedef NODE* linkedlist;
void insertnode (NODE** node, int x){
if(!(*node))
*node=makeNode(x);
else
insertnode(&((*node)->next), x);
}
void deletenode(NODE** node){
(*node)=NULL;
free (*node);
}
int main()
{
NODE* node;
linkedlist mylist;
mylist=NULL;
insertnode(&mylist, 3); //insert a node
insertnode(&mylist, 4);
node = mylist->next; //
deletenode(&(mylist->next));
printf("\n%d\n",node->data); // prints 4
return 0;
}
I was studyng binary search trees and when i arrive to the deletion part i started to doubt about how pointers works in memory and my world got collapsed.
I will use this linked list code instead the BST code because is more easy to me for explain my doubts.
Well, if i do this node=NULL;
mylist will still exist because node is a copy of the pointer to mylist, but when i do this:
free(node->next);
node->next=NULL;
mylist->next will no longer exist because node->next
is the original pointer of that next node.
So in the code i did a copy of mylist->next
pointer in node
node = mylist->next;
i assume this is what happen:
mylist-------------->[|data|next|]->[|data|next|]<------------------node;
Then i delete the original node. But when i print node->date
for my surprise the program prints 4.
I understand thats the pointers are variables that hold a memory location. But i can't get why node->data;
still exist if i'd set free the node and set it NULL;
just affected the pointer and not the memory?
when i free and NULL a pointer what happens with the memory blocks what that pointer holds?
My problems is with the pointers to structs, and how they work with the memory allocated pointed by them.
I hope i make me understand, my english is't so good.
Upvotes: 2
Views: 71
Reputation: 19874
Pointer points to a memory location.
After free()
you are giving back the allocated memory back to OS.
Accessing this memory is UB(Undefined behavior)
So you might see expected result but since you have UB this is not guaranteed at all time.
In you case let's say you free the memory the pointer mylist->next is pointing to.
free(mylist->next)
mylist->next = NULL;
Now the memory held by the pointer mylist->next
is returned back to OS. Since this memory is available to the OS it can do anything with it. In your case it now writing anytihng to this location and retaining what is there before the free()
so you are getting back 4
but if this memory was used to allocate to someother purpose then you might have not got what you expected.
So basically accessing the memory which is freed leads to UB
Upvotes: 1
Reputation: 30489
Accessing memory after freeing invokes undefined behavior. Some possible outcomes are:
But this list is not comprehensive, some else may also happen.
Takeaway is, never access a data from memory location that is freed.
Upvotes: 2