Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

On many systems, malloc(0) returns NULL. I recall one man page describing it as "a stupid answer to a stupid question" or some such.


Which, it's not remotely a stupid question: malloc gets passed a zero-length argument when it's used to allocate variable length data. That malloc(0) works and is handled by free() without crashing the program is a simplifying assumption, as is the assumption that free(NULL) won't corrupt the heap.

Any use of alloca() on the other hand seems risky. Similar arguments could be made about the semantics of jmp_bufs, which also get used to get a handle on the stack.


"the assumption that free(NULL) won't corrupt the heap."

That's not an assumption, that's how the free() function is defined to work by the language standard. It never ceases to astound me how many otherwise good C programmers think free(NULL) is an error.


... yes, that's what I'm saying.


I see. The word "assumption" confused me, as in context it would generally mean something that isn't necessarily true.


as is the assumption that free(NULL) won't corrupt the heap.

When you refer to simplifying assumptions, are you talking about assumptions made by the programmer, or by the compiler and libc? For example, the POSIX manual page[0] for free( void* ptr ) says, "If ptr is a null pointer, no action shall occur." The malloc manpage says, "If size is 0, either a null pointer or a unique pointer that can successfully passed to free() shall be returned." That sounds more like a definition than an assumption to me. What am I missing?

[0] Obtained by installing manpages-posix-dev on Ubuntu and running man 3posix free.




Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: