堆栈的增长方向

#define ADDRESS_FUNCTION(arg) &(arg) static int stack_dir; /* 1 or -1 once known. */ #define STACK_DIR stack_dir static void find_stack_direction () { static char *addr = NULL; /* Address of first `dummy', once known. */ auto char dummy; /* To get stack address. */ if (addr == NULL) { /* Initial entry. */ addr = ADDRESS_FUNCTION (dummy); find_stack_direction (); /* Recurse once. */ } else { /* Second entry. */ if (ADDRESS_FUNCTION (dummy) > addr) stack_dir = 1; /* Stack grew upward. */ else stack_dir = -1; /* Stack grew downward. */ } }

第一次进入,由于addr为NULL,所以将字符变量dummy的地址赋值给静态变量addr 第二次进入,由于静态变量addr已赋了值,所以进入 "Second entry." 接着,将第二次进入的dummy地址和第一次进入的dummy地址相比较 如果值为正,则堆栈向高地址增长;否则,堆栈向低地址增长

你可能感兴趣的:(function,null)