07-26-2023, 09:54 PM
Suppose I came across an instance in a program where I would either free a NULL pointer, or first check whether it was NULL and skip the `free()` function call.
Would it be more efficient to simply free a NULL pointer? I searched around a bit and apparently, for implementations post C89, it is harmless to free a NULL pointer - so the decision comes down to efficiency.
My presumption is that there may potentially entail quite a bit of overhead when calling `free()`. As such, perhaps the simple logical check before calling the `free()` function is quite necessary.
--------
tl;dr version,
What is happening internally when a call to `free()` is made that may make it more or less efficient to first check whether or pointer is NULL before freeing?
Would it be more efficient to simply free a NULL pointer? I searched around a bit and apparently, for implementations post C89, it is harmless to free a NULL pointer - so the decision comes down to efficiency.
My presumption is that there may potentially entail quite a bit of overhead when calling `free()`. As such, perhaps the simple logical check before calling the `free()` function is quite necessary.
--------
tl;dr version,
What is happening internally when a call to `free()` is made that may make it more or less efficient to first check whether or pointer is NULL before freeing?