3 ms·
Not all C runtimes allow you to deallocate a null ppinter without consequence.
by SpaceNoodled 2y ago
Not all C runtimes allow you to deallocate a null ppinter without consequence.
- lifthrasiir 2y agoSuch C runtime is not standard-compliant [1]: > 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. [1] https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2310.pdf#page=270 https://www.open-std.org/jtc1/sc22/wg14/www/docs/n2310.pdf#p...
- dathinab 2y agodoesn't matter 50 years ago non standard compliance was the norm, even today it is quite common (to use non compliant extensions) and there is also stuff like caches, arena allocations, jemalloc etc. which might not be linked against libc/free and might require manual free function usage, external APIs providing a their own free/destruction functions is really really normal
- lifthrasiir 2y agoAccepting a null pointer in free is a very easy part of the conformance and I am yet to see a libc implementation that doesn't so far, so your statement should be pretty much outdated even if it were true. And since malloc-free interface is so popular in C, other allocators are also commonly modelled after that (plus their own APIs) anyway; jemalloc for example has an usual free with the standard interface.
- deleted 2y ago[deleted]
- magicalhippo 2y agoEven the C89 draft[1] says passing null should do nothing, and as far as I can tell it's still[2] the case. Which runtimes would this be? [1]: http://jfxpt.com/library/c89-draft.html#4.10.3.2 http://jfxpt.com/library/c89-draft.html#4.10.3.2 [2]: https://en.cppreference.com/w/c/memory/free https://en.cppreference.com/w/c/memory/free
- tjalfi 2y agoI wish I didn't know this, but it could be SunOS4[0] which went out of support in 2003. It's probably the most well-known OS where free(NULL) wasn't supported. I would like to think that people aren't writing new code for it. [0] https://lists.gnu.org/archive/html/bug-gnulib/2020-12/msg00164.html https://lists.gnu.org/archive/html/bug-gnulib/2020-12/msg001...
- jcelerier 2y agoIf free(NULL) doesn't work it's not the C programming language but something else
- jabl 2y agoThat was a long time ago. A decade or so ago Jim Meyering did experiments with a lot of allocators, and subsequently went on a spree removing NULL checks before free() in a lot free software toolchain projects (gcc, gdb, binutils, etc).
- 1propionyl 2y agoDespite the sparing number of cycles this saves per execution... I have to wonder how many cycles this has saved since.
- epcoa 2y agoLife is too short to deal with such brokenness then. Seriously, those jobs typically don't pay more than peanuts anyhow.