Freeing memory twice

suggest change

Freeing the same allocation twice is undefined behavior. This example also ignores the possibility that malloc fails; a null pointer may be passed to free, but the second free of a non-null pointer is still undefined:

int * x = malloc(sizeof(int));
*x = 9;
free(x);
free(x);

Quote from standard(7.20.3.2. The free function of C99 ):

Otherwise, if the argument does not match a pointer earlier returned by the calloc, malloc, or realloc function, or if the space has been deallocated by a call to free or realloc, the behavior is undefined.

Feedback about page:

Feedback:
Optional: your email if you want me to get back to you:



Table Of Contents