There are certain conditions that can cause stack overflows on an x86 Linux system:
struct my_big_object[HUGE_NUMBER]on the stack. Walking through it eventually causesSIGSEGV.- The
alloca()routine (likemalloc(), but uses the stack, automatically frees itself, and also blows up withSIGSEGVif it’s too big). Update: alloca() isn’t formally deprecated as I originally stated; it is merely discouraged.
Is there a way to programmatically detect if the local stack is big enough for a given object? I know the stack size is adjustable via ulimit, so I have hope there is a way (however non-portable it may be). Ideally, I would like to be able to do something like this:
int min_stack_space_available = /* ??? */; if (object_size < min_stack_space_available) { char *foo = alloca(object_size); do_stuff(foo); } else { char *foo = malloc(object_size); do_stuff(foo); free(foo); }
You can determine the stack space the process has available by finding the size of a process’ stack space and then subtracting the amount used.
shows the stack size on a linux system. For a programmatic approach, check out getrlimit(). Then, to determine the current stack depth, subtract a pointer to the top of the stack from one to the bottom. For example (code untested):