3 ms·
This should tell one whether the pointer returned by alloca(3C) is safe to use: #include <sys/time.h> #include <sys/resource.h> #include <stdio.h> int
by Annatar 11y ago
This should tell one whether the pointer returned by alloca(3C) is safe to use:
#include <sys/time.h>
#include <sys/resource.h>
#include <stdio.h>
int main(int argc, char *argv[])
{
struct rlimit limit;
getrlimit(RLIMIT_STACK, &limit);
printf("Stack start at %p, end at %p.\n", &argc, &argc - limit.rlim_cur);
return(0);
}
% cc limit.c -o limit && ./limit
Stack start at 0x7fff51094ba8, end at 0x7fff4f094ba8.
If the pointer returned by alloca(3C) is within those memory addresses, and does not exceed the bottom (in this example, $7fff4f094ba8), it is safe to use.