相关疑难解决方法(0)

堆栈是向上还是向下增长?

我在c中有这段代码:

int q = 10;
int s = 5;
int a[3];

printf("Address of a: %d\n",    (int)a);
printf("Address of a[1]: %d\n", (int)&a[1]);
printf("Address of a[2]: %d\n", (int)&a[2]);
printf("Address of q: %d\n",    (int)&q);
printf("Address of s: %d\n",    (int)&s);
Run Code Online (Sandbox Code Playgroud)

输出是:

Address of a: 2293584
Address of a[1]: 2293588
Address of a[2]: 2293592
Address of q: 2293612
Address of s: 2293608
Run Code Online (Sandbox Code Playgroud)

所以,我看到,从那里a开始a[2],内存地址每个增加4个字节.但是,从qs,内存地址减少了4个字节.

我想知道两件事:

  1. 堆栈是成长还是下降?(在这种情况下看起来对我来说都是)
  2. a[2]q内存地址之间发生了什么?为什么那里存在很大的记忆差异?(20个字节).

注意:这不是作业问题.我很好奇堆栈是如何工作的.谢谢你的帮助.

c memory stack

82
推荐指数
4
解决办法
6万
查看次数

标签 统计

c ×1

memory ×1

stack ×1