[英]Actual C++ program memory layout on Windows (VS2010)
Lately I have been reading some articles about C++ memory layout and simplifiying I understand there is 3 main blocks: 最近,我一直在阅读一些有关C ++内存布局的文章,并简化了我的理解,主要有3个主要模块:
According to the post I read I assume a big block of memory is allocated and splited on the above parts. 根据我读过的帖子,我假设在上述部分分配了一块大内存并对其进行了拆分。
To check this I have created a simple program: 为了检查这一点,我创建了一个简单的程序:
#include <stdio.h>
int g_loopCount;
static int gs_one = 1;
int getLifeResult(int a)
{
printf("&a %d\t\t%p\n", g_loopCount, &a);
if(++g_loopCount < 4)
{
getLifeResult(a);
}
else
{
return g_loopCount * 10 + a;
}
}
int main()
{
//fixed
printf("-fixed-\n");
printf("&gs_one\t\t\t%p\n", &gs_one);
g_loopCount = 0;
printf("&g_loopCount\t\t%p\n\n", &g_loopCount);
int* lifeResult = new int(0);
int* lifeResultCopy = new int(0);
//stack
printf("-stack-\n");
printf("&lifeResult\t\t%p\n", &lifeResult);
printf("&lifeResultC\t\t%p\n", &lifeResultCopy);
*lifeResult = getLifeResult(2);
*lifeResultCopy = *lifeResult;
printf("\n");
//heap
printf("-heap-\n");
printf("lifeResult\t\t%p\n", lifeResult);
printf("lifeResultC\t\t%p\n\n", lifeResultCopy);
return *lifeResult;
}
However even when the memory adress are consistent in between the mentioned memory blocks. 但是,即使提到的存储块之间的存储地址一致。 The order of these main blocks changes form one execution to other.
这些主要块的顺序从一个执行更改为另一个执行。
run 0 run 1 run 2
-fixed-
&gs_one 00E37000 00A37000 00047000
&g_loopCount 00E37140 00A37140 00047140
-stack-
&lifeResult 0037FD6C 0030FD44 003EF784
&lifeResultC 0037FD60 0030FD38 003EF778
&a 0 0037FC70 0030FC48 003EF688
&a 1 0037FB98 0030FB70 003EF5B0
&a 2 0037FAC0 0030FA98 003EF4D8
&a 3 0037F9E8 0030F9C0 003EF400
-heap-
lifeResult 00684670 00184670 00724670
lifeResultC 006846B0 001846B0 007246B0
Is that normal? 那是正常的吗? What the location of these blocks depends on: OS, compiler?
这些块的位置取决于:OS,编译器? Does it happen to every operating systems?
是否会发生在每个操作系统上? Can anyone explain me a little bit more about this mechanism?
有人可以向我解释一下这种机制吗?
Thank you! 谢谢!
The OS rules everything. 操作系统掌控一切。
First off, modern code is relocatable. 首先,现代代码是可重定位的。 Pretty much an operating system, particularly Windows, will figure out where your program runs at dynamically when it is loaded.
几乎所有的操作系统,尤其是Windows,都会弄清楚程序在加载时在何处动态运行。 This is pretty important in making DLLs play together but it can happen at any time.
这对于使DLL一起播放非常重要,但它可以随时发生。
Check this out: http://en.wikipedia.org/wiki/Portable_Executable 检查一下: http : //en.wikipedia.org/wiki/Portable_Executable
Also, the amount of stack your program can consume can change. 此外,程序可以使用的堆栈数量可以更改。
You can disable this at least for the base address, by going to your project settings and go to: 您可以转到项目设置,然后转到:
Project|Properties|Linker|Advanced|Randomize Base Address
Set this entry to "No (/DYNAMICBASE:NO)" 将此条目设置为“否(/ DYNAMICBASE:NO)”
You can also use a fixed base address (/FIXED /BASE) 您还可以使用固定的基地址(/ FIXED / BASE)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.