Can a call to free() fail in any way?
For example:
free(NULL);
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
Freeing a NULL pointer cannot fail. And
freedoesn’t return any error, but freeing unallocated memory, already freed memory or the middle of an allocated block is undefined behaviour – it may cause a memory error and the program may abort (or worse, it will corrupt the heap structure and crash later).Or, even worse than that, keep running but totally corrupt your data and write it to disk without you realising 🙂
The relevant portion of the standard (C99) is section
7.20.3.2: