I'm writing a library in which I've found the non-standard C function alloca
to be really useful. Several sources (linux man pages, this SO question) caution against using alloca
. Unlike malloc
and friends, which are supposed to return NULL
when you run out of heap memory, using alloca
is undefined behavior in the event of a stack overflow. In my case, I have hard limits (enforced by plenty of assert
statements) on the sizes of the objects I'm allocating, so I think that this completely sensible caveat isn't an issue.
Nonetheless, I don't see why alloca
necessarily has to result in undefined behavior in this circumstance. Why couldn't alloca
check where the stack pointer is and return NULL
in the event of a possible stack overflow? Implementing these checks would of course be platform- and architecture-specific, but I don't see why it should be impossible.