Reputation: 14603
If you read through the GNU libs docs, you can see:
Some non-GNU systems fail to support alloca, so it is less portable. However, a slower emulation of alloca written in C is available for use on systems with this deficiency.
How would a C emulation of alloca()
look like, assuming VLAs are not available either?
Upvotes: 2
Views: 624
Reputation: 70472
Since you were looking at GNU libc documentation, you might consider how this would be emulated in GCC.
GCC offers the cleanup
attribute to allow a cleanup function to be called when a variable falls out of scope.
void foo (void *p) {
printf("foo: %p\n", p);
}
int main(void) {
int x __attribute__((cleanup(foo)));
x = 7;
printf("%p\n", &x);
return 0;
}
In the above program, when x
falls out of scope, foo()
is passed the address of x
.
Upvotes: 0
Reputation: 8209
According to what alloca()
is
The alloca() function allocates size bytes of space in the stack frame of the caller. This temporary space is automatically freed when the function that called alloca() returns to its caller.
implementation will be platform-specific, and probably compiler should be aware of it, since generated code must respect to non-fixed offsets of locals at stack frame. So if your toolchain has no VLA - you have nothing to do with it.
Upvotes: 1