【发布时间】:2014-04-12 02:07:43
【问题描述】:
堆困扰着我,因为我不明白谁创建它,谁维护它以及谁决定它应该在哪里......这个测试显示了我的难题的一部分:
源代码:
#include <malloc.h>
#include <stdio.h>
int a;
int b = 5;
int * getMeAPointer() {
int * e = malloc(4);
*e = 5;
return e;
}
void main() {
a = 5;
int c = 5;
int * d = (int *) 0x405554;
*d = 5;
int * e = getMeAPointer();
printf("Address of a located in .bss is %x\n", &a);
printf("Address of b located in .data is %x\n", &b);
printf("Address of c located in stack is %x\n", &c);
printf("Address of d located in stack is %x\n", &d);
printf("Address of *d located absolutely is %x\n", d);
printf("Address of e located in stack is %x\n", &e);
printf("Address of *e located on heap is %x\n", e);
printf("Address of getMeAPointer() located in .text is %x\n", getMeAPointer);
free(e);
}
示例打印输出:
Address of a located in .bss is 0x405068
Address of b located in .data is 0x402000
Address of c located in stack is 0x22ff1c
Address of d located in stack is 0x22ff18
Address of *d located absolutely is 0x405554
Address of e located in stack is 0x22ff14
Address of *e located on heap is 0x541738
Address of getMeAPointer() located in .text is 0x4013b0
Address of a located in .bss is 0x405068
Address of b located in .data is 0x402000
Address of c located in stack is 0x22ff1c
Address of d located in stack is 0x22ff18
Address of *d located absolutely is 0x405554
Address of e located in stack is 0x22ff14
Address of *e located on heap is 0x3a1738
Address of getMeAPointer() located in .text is 0x4013b0
Address of a located in .bss is 0x405068
Address of b located in .data is 0x402000
Address of c located in stack is 0x22ff1c
Address of d located in stack is 0x22ff18
Address of *d located absolutely is 0x405554
Address of e located in stack is 0x22ff14
Address of *e located on heap is 0x351738
Address of getMeAPointer() located in .text is 0x4013b0
....etc....
现在这些是我的担忧:
为什么堆在移动而其他段没有移动?这是在带有 MinGW 的 Windows 7 操作系统上,这个文件是用 GCC 编译的,没有进一步的标志(我不相信这是地址空间布局随机化的一个例子)。
谁决定堆的位置?我相信链接器为堆保留了一个位置(我已经在符号表中看到堆符号)但是什么时候确定确切的地址,它是由 RUNNABLE 本身(C 代码)在加载后完成的运行时事情,还是它由链接器/加载器/动态链接器在执行之前加载程序时完成?
有没有办法在ld中设置堆地址?我知道我可以设置除堆栈之外的所有段(因为它内置在操作系统的内核中)但是我可以设置堆地址吗?
按照我的理解,堆并不是真正的汇编语言结构,如果我们选择只进行汇编编程,我们就无法访问堆。因此它是一个 C 构造,但我对它如何影响堆的生命感兴趣(我的意思是我们说堆就像它与段和堆栈处于同一级别,但如果不是,那么应该给它很多其他条件)...这是正确的,谁能告诉我更多关于它的信息?
老实说,我整天都在谷歌上搜索,我渴望得到一些答案!
【问题讨论】:
-
您的示例打印输出几乎相等...
-
no.. 看看堆中的 e 指向的是什么......这就是我整个问题的重点......
-
哦,是的,没错。实际上,这是这里唯一有趣的地方。
标签: c assembly linker heap-memory memory-address