How can the below code result in a dangling pointer.
{
char *cPointer = malloc ( some constant number );
/* some code */
free ( cPointer );
cPointer = NULL;
/* some code */
}
How can the below code result in a dangling pointer.
{
char *cPointer = malloc ( some constant number );
/* some code */
free ( cPointer );
cPointer = NULL;
/* some code */
}
If the first "some code" copies the pointer value from cPointer to some other pointer variable, and the second "some code" uses that other pointer variable, it will be in trouble.
If the first "some code" generates an exception so the free is never reached, it will leak memory.
It can't.
This would:
char * a = malloc(556);
char * b = a;
free(a);
a = NULL;
b is now dangling pointer, because the object it pointed to it's gone but b
still stores address to memory where the object used to be, you get funny results when you try to access it - it depends if the memory has been reused or is untouched.