> `free(NULL);` will crash on some platforms that gcc supports, I believe.
I'm pretty certain that `free(NULL)` is part of the C99 standard, so compiler vendors have had 25 years to address it.
If your `free(NULL)` is crashing on a certain platform, you probably have bigger problems, starting with "Compiler that hasn't been updated in 25 years".
Then it's in violation of the C standard, at least as of C11 (I didn't check C99 or C89).
> 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. Otherwise, if the argument does not match a pointer earlier returned by a memory management function, or if the space has been deallocated by a call to free or realloc, the behavior is undefined.
While I agree it shouldn't, that particular document is the UNIX specification, not the C specification, so it does not apply to C compilers on non-UNIX platforms.
That feels like a "citation needed", since that would be very clear violation of the C spec and thus a rather serious bug in the standard library for that platform.
lelanthran|5 months ago
I'm pretty certain that `free(NULL)` is part of the C99 standard, so compiler vendors have had 25 years to address it.
If your `free(NULL)` is crashing on a certain platform, you probably have bigger problems, starting with "Compiler that hasn't been updated in 25 years".
jibal|5 months ago
SAI_Peregrinus|5 months ago
> 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. Otherwise, if the argument does not match a pointer earlier returned by a memory management function, or if the space has been deallocated by a call to free or realloc, the behavior is undefined.
Emphasis mine
po1nt|5 months ago
>If ptr is a null pointer, no action occurs.
quietbritishjim|5 months ago
jibal|5 months ago
No, of course it won't. `free(NULL)` has been a noop ever since C89 (and before, for that matter).
unwind|5 months ago
mrheosuper|5 months ago
inkyoto|5 months ago
menaerus|5 months ago
jibal|5 months ago
No, certainly not, but you can do
`if(ptr == NULL) return;`
which is correct but unnecessary since `free` is required to do that check.