main() calls Call_By_Test() function with argument parameter First Node.
I have freed the First Node in Call_By_Test() but First node address not freed in main(), why ?.
typedef struct LinkList{
int data;
struct LinkList *next;
}mynode;
void Call_By_Test(mynode * first)
{
free(first->next);
first->next = (mynode *)NULL;
free(first);
first = (mynode *)NULL;
}
int main()
{
mynode *first;
first = (mynode *)malloc(sizeof(mynode));
first->data = 10;
first->next = (mynode *)NULL;
cout<<"\n first pointer value before free"<<first<<endl;
Call_By_Test(first);
// we freed first pointer in Call_By_Test(), it should be NULL
if(first != NULL)
cout<< " I have freed first NODE in Call-By-Test(), but why first node pointer has the value "<<first<<endl;
}
Output:
first pointer value 0x804b008
I have freed first NODE in Call-By-Test(), but why first node pointer has the value 0x804b008
Since the question is tagged c++, I would refactor to:
That conveys the pass-by-reference without extra dereferences. All the solutions that propose passing a pointer to the pointer (
void Call_By_Test( mynode ** first )) are using pass-by-value semantics in a pointer to the pointer variable. While you can do this in C++, pass-by-reference is clearer.