-2

Possible Duplicate:
Does free(ptr) where ptr is NULL corrupt memory?
Is it good practice to free a NULL pointer in C?

I have a question concerning freeing a null pointer.

char *p = NULL;
free(p);

Could the free(NULL) cause a crash?

Or does it depend on the compiler?

Community
  • 1
  • 1
MOHAMED
  • 41,599
  • 58
  • 163
  • 268
  • 1
    http://stackoverflow.com/questions/1938735/does-freeptr-where-ptr-is-null-corrupt-memory – Faruk Sahin Nov 20 '12 at 11:00
  • 2
    It is always safe. See http://stackoverflow.com/questions/6084218/is-it-good-practice-to-free-a-null-pointer-in-c for a duplicate question and answers. – simonc Nov 20 '12 at 11:00

1 Answers1

3

From man page of free

void  free(void *ptr);

The free() function deallocates the memory allocation pointed to by ptr. If ptr is a NULL pointer, no operation is performed.

If you want to get confirmation from C manual itself

The free function causes the space pointed to by ptr to be deallocated, that is, made available for further allocation. If ptr is a null pointer, no action occurs.

See page 313 of this document.

Krishnabhadra
  • 34,169
  • 30
  • 118
  • 167
  • 3
    Note that man pages are not a reliable source for the definition of a standard C function, because they might contain for example Posix-specific guarantees that aren't guaranteed for C in general. In this case, the behavior is guaranteed by the C standard too. – Steve Jessop Nov 20 '12 at 11:15
  • @SteveJessop correct.. I will edit the answer.. eventhough the question is closed.. – Krishnabhadra Nov 20 '12 at 11:19
  • this is a classic example of exception handling in the system library! – Harsha J K Apr 03 '18 at 21:29