Why is my pointer non-zero after free?

void getFree(void *ptr) { if(ptr != NULL) { free(ptr); ptr = NULL; } return; } int main() { char *a; a=malloc(10); getFree(a); if(a==NULL) printf("it is null"); else printf("not null"); } 

Why is the output of this program not NULL?

+9
source share
6 answers

Because the pointer is copied by the value of your function. You assign NULL local copy of the variable ( ptr ). This does not appropriate its original copy.

The memory will still be freed, so you can no longer safely access it, but your original pointer will not be NULL .

This is the same as if you were passing int functions. You did not expect the original int be edited by this function, unless you have a pointer to it.

 void setInt(int someValue) { someValue = 5; } int main() { int someOtherValue = 7; setInt(someOtherValue); printf("%i\n", someOtherValue); // You'd expect this to print 7, not 5... return 0; } 

If you want to delete the original pointer, you need to pass a pointer to a pointer:

 void getFree(void** ptr) { /* Note we are dereferencing the outer pointer, so we're directly editing the original pointer */ if (*ptr != NULL) { /* The C standard guarantees that free() safely handles NULL, but I'm leaving the NULL check to make the example more clear. Remove the "if" check above, in your own code */ free(*ptr); *ptr = NULL; } return; } int main() { char *a; a = malloc(10); getFree(&a); /* Pass a pointer-to-pointer */ if (a == NULL) { printf("it is null"); } else { printf("not null"); } return 0; } 
+21
source

Since the getFree() function takes a copy of the pointer. ptr and c are both pointers, but they are different variables. This is for the same reason why this function outputs "6":

 void Magic(int x) { x = 1; } void main() { int a = 6; Magic(a); printf("%d", a); } 
+7
source

You pass the pointer a by value, so it is not changed by the function. This is only a copy of the pointer, changed inside the function, does not affect the original value of the variable.

Update:

If you want to make your life easier by replacing freeing + zeroing a variable with a single line of code, you will need a macro:

 #define MYFREE(x) free(x); x = NULL; 

or a function with a pointer to a pointer argument:

 void myfree(void** pp) { free(*pp); *pp = NULL; } 
+5
source

Pointers are stored as integers somewhere in memory.

When you do a = malloc(10); , a has some meaning, say 0x1.

When you call getFree(a); , the function copies a to void *ptr .

Now a=0x1 and ptr=0x1 .

When you do ptr=NULL , only ptr changes to NULL, but a is still 0x1 ..

+3
source

You pass a pointer By value .. (By default, C passes an argument by value), which means that you are updating the copy only. not in real location .. for this you may need to use a pointer to a pointer in C

 void getFree(void **ptr) { if(*ptr != NULL) { free(*ptr); *ptr = NULL; } return; } 
+1
source

This question has already been answered, but if it helps, I can explain it graphically.

You do this -> the pointer is copied by value to your function, so it points to an array

but instead you want this → to point to the source pointer

As Merlin Morgan-Graham has said, the way to solve it is to add the * and & amp;

0
source

All Articles