What is the difference between
void *bytes = alloca(size);
and
char bytes[size]; //Or to be more precise, char x[size]; void *bytes = x;
...where size is a variable whose value is unknown at compile-time.
alloca()
does not reclaim memory until the current function ends, while the variable length array reclaims the memory when the current block ends.
Put another way:
void foo()
{
size_t size = 42;
if (size) {
void *bytes1 = alloca(size);
char bytes2[size];
} // bytes2 is deallocated here
}; //bytes1 is deallocated here
alloca()
can be supported (in a fashion) on any C89 compiler, while the variable length array requires a C99 compiler.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With