#include <stdio.h>
#include <stdlib.h>
int foo; /* a staticly allocated variable */
int recur(int i) { /* a recursive function */
int j = i; /* a stack allocated variable within a recursive function */
printf("recur call (i)%d: stack@ %lx\n", i, (long unsigned int) &j); /* fix this so it print
s the address of j */
if (i > 0) {
return recur(i-1);
}
return 0;
}
int stuff = 7; /* a statically allocarted, pre-initialized variable */
int main (int argc, char *argv[]) {
int i; /* a stack allocated variable */
char *buf1 = malloc(100); /* dynamically allocate some stuff */
char *buf2 = malloc(100); /* and some more stuff */
char *buf3 = malloc(100); /* and some more stuff */
printf("_main @ %lx\n", (long unsigned int) &main); /* fix to print address of main */
printf("_recur @ %lx\n", (long unsigned int) &recur); /* fix to print address of recur */
printf("main call (i):stack@ %lx\n", (long unsigned int) &i); /* fix to get address of the s
tack variable */
printf("_static foo: %lx\n", (long unsigned int) &foo); /* fix to get address of the static v
ariable */
printf("_static stuff: %lx\n", (long unsigned int) &stuff); /* fix to get address of a stati
c variable */
printf("Heap: malloc 1: %lx\n", (long unsigned int) buf1);
printf("Heap: malloc 2: %lx\n", (long unsigned int) buf2);
printf("Heap: malloc 3: %lx\n", (long unsigned int) buf3);
recur(3);
return 0;
}
该程序的输出结果如下:
_main @ 4005c2
_recur @ 40057d
main call (i):stack@ 7fff26397694
_static foo: 601050
_static stuff: 601048
Heap: malloc 1: 1862010
Heap: malloc 2: 1862080
Heap: malloc 3: 18620f0
recur call (i)3: stack@ 7fff2639766c
recur call (i)2: stack@ 7fff2639763c
recur call (i)1: stack@ 7fff2639760c
recur call (i)0: stack@ 7fff263975dc
任何人都可以解释以下内容吗?
答案 0 :(得分:3)
首先要明确的是,地址是进程内存空间的虚拟内存地址。也就是说,对于32位环境,进程内存空间的布局或多或少是这样的:
0 +----------------------------------------+
| .text (code) |
+----------------------------------------+
| .data (initialized static variables) |
+----------------------------------------+
| .bss (uninitialized static variables) |
+----------------------------------------+ <- Heap start, grows upwards
| heap |
+----------------------------------------+
| stack |
3GB +----------------------------------------+ <- Stack start, grows downwards
| kernel code |
4GB +----------------------------------------+
静态变量地址的大小不是很短,所有地址都是32位地址,只有printf()
截断前导零(请参见上图,静态变量的地址较低)。 / p>
malloc()
通常会放置bookeeping数据。这是依赖于实现的。
&#34; int
只占用4个字节&#34;,这取决于您的系统。编译器也可以为堆栈帧对齐添加填充。
答案 1 :(得分:2)
malloc()
保证返回对齐的指针。在64位环境中,它通常位于16字节边界上,因此您分配的100个字节将被舍入&#39;到112(7 * 16)进行下一次分配。此外,malloc()
需要使用其他数据跟踪分配,因此使用的空间将始终高于malloc的数量。stuff
和foo
之间的主要区别在于,其中一个已初始化而另一个未初始化,因此stuff
将放置在.data
部分,{ {1}}将放置在foo
部分(零初始化部分)中。这意味着这两个变量不太可能彼此相邻。在你的情况下,他们很近,因为没有别的。