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);
If you want to delete the original pointer, you need to pass a pointer to a pointer:
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"); } return 0; }
Merlyn morgan-graham
source share