Find Out Stack Direction
A program's stack segment direction might goes up or goes down. This snippet[1] shows how to find out stack direction.
- Declare a
static
variable addr. - Declare a
local
variable dummy; dummy's address will be grows up or down with the next function call. - Store dummy's address to addr, and compare the value when the next function call.
static int find_stack_direction() {
static char *addr = 0;
auto char dummy;
if (addr = 0) {
addr = &dummy;
return find_stack_direction();
} else {
return ((&dummy > addr) ? 1 : -1);;
}
}