堆栈内存布局c(Stack memory layout in c)

编程入门 行业动态 更新时间:2024-10-27 14:18:34
堆栈内存布局c(Stack memory layout in c)

我已经写了一个小程序来学习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 --> 0x7ffc67ca5c9c

Code:

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 --> 0x7ffc67ca5c9c

My 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.

更多推荐

本文发布于:2023-07-24 01:59:00,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/1240180.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:堆栈   布局   内存   layout   Stack

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!