我已经写了一个小程序来学习C中的堆栈指针。通常,堆栈指针指向RAM的最后一个地址。 例如,我在Linux中运行此代码,其中堆栈指针位于最后一个地址( 0x7fffffffffff ),但变量存储在这些地址中(对于此程序):
c --> 0x7ffc67ca5c94 b --> 0x7ffc67ca5c98 a --> 0x7ffc67ca5c9c码:
void fun(int *ptr) { printf("c --> %p\n",ptr); ptr++; printf("b --> %p\n",ptr); ptr++; printf("a --> %p\n",ptr); } int main() { int a = 10,b = 20,c = 30; printf("%p %p %p\n",&a,&b,&c); fun(&c); return 0; }该计划的输出:
0x7ffc67ca5c9c 0x7ffc67ca5c98 0x7ffc67ca5c94 c --> 0x7ffc67ca5c94 b --> 0x7ffc67ca5c98 a --> 0x7ffc67ca5c9c我的问题是:
为什么变量没有存储在堆栈帧的最后一部分(0x7fffffffffff)中,它跳过一些内存位置并被存储? 这种行为有任何正当理由吗?
为什么堆栈指针地址有六个字节?
我正在使用32位GCC编译器的64位机器上工作。
I have written a small program for learning about the stack pointer in C. Usually, the stack pointer points to the last address of RAM. For example, I run this code in Linux, which has the stack pointer in last address (0x7fffffffffff) but the variables are stored in these addresses (for this program):
c --> 0x7ffc67ca5c94 b --> 0x7ffc67ca5c98 a --> 0x7ffc67ca5c9cCode:
void fun(int *ptr) { printf("c --> %p\n",ptr); ptr++; printf("b --> %p\n",ptr); ptr++; printf("a --> %p\n",ptr); } int main() { int a = 10,b = 20,c = 30; printf("%p %p %p\n",&a,&b,&c); fun(&c); return 0; }Output for this program:
0x7ffc67ca5c9c 0x7ffc67ca5c98 0x7ffc67ca5c94 c --> 0x7ffc67ca5c94 b --> 0x7ffc67ca5c98 a --> 0x7ffc67ca5c9cMy questions are:
Why are the variables not stored in last section of the stack frame (0x7fffffffffff) which skips some memory locations and gets stored? Any valid reason for such behavior?
Why the stack pointer address has six bytes?
I am working on a 64-bit machine with a 32-bit GCC compiler.
更多推荐
发布评论