我正在尝试估计我的程序堆栈范围的跨度。我的策略是假设由于堆栈向下增长,我可以为当前堆栈帧创建一个局部变量,然后使用它的地址作为参考。
int main()
{
//Now we are in the main frame.
//Define a local variable which would be lying in the top of the stack
char a;
//Now define another variable
int b; //address should be lower assuming stack grows downwards
//Now estimate the stack size by rlimit
struct rlimit stack_size;
getrlimit(RLIMIT_STACK,&stack_size);
//A crude estimate would be stack goes from &a to &a - stack_size.rlim_cur
printf("%p \n",&a);
printf("%p \n",&b);
printf("stack spans from %u to %u",&a,&a - stack_size.rlim_cur);
return 0;
}
有趣的是,当我使用 gdb 调试 a 和 b 的值地址时,b 的地址比 a 的值更高。此外,堆栈指针始终保持在 .
0xbfca65f4
0xbfca660f
Stack spans from 0xbfca65f4 to 0xbbca65f4.
ebx 0xb7faeff4 -1208291340
esp 0xbffff670 0xbffff670
有人能帮我理解我哪里出错了吗?提前致谢!