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.

  1. Declare a static variable addr.
  2. Declare a local variable dummy; dummy's address will be grows up or down with the next function call.
  3. 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);;
	}
}

  1. https://stackoverflow.com/questions/664744/what-is-the-direction-of-stack-growth-in-most-modern-systems ↩︎