How to check if a pointer is freed already in C?

Kitcha picture Kitcha · Nov 28, 2011 · Viewed 51.9k times · Source

I would like to check if a pointer is freed already or not. How do I do this using gnu compiler set?

Answer

Chad picture Chad · Nov 28, 2011

You can't. The way to track this would be to assign the pointer to 0 or NULL after freeing it. However as Fred Larson mentioned, this does nothing to other pointers pointing to the same location.

int* ptr = (int*)malloc(sizeof(int));
free(ptr);
ptr = NULL;