TIL alloca
TIL alloca
function in C to dynamically allocate memory on stack. Also learned it’s extremely unsafe and is discouraged to use.
alloca()
is machine and compiler dependent; its use is discouraged.
alloca()
is slightly unsafe because it cannot ensure that the pointer returned points to a valid and usable block of memory. The allocation made may exceed the bounds of the stack, or even go further into other objects in memory, andalloca()
cannot determine such an error. Avoidalloca()
with large unbounded allocations.
The use of C99 variable-length arrays and
alloca()
in the same function will cause the lifetime of alloca’s storage to be limited to the block containing thealloca()
.