d****n 发帖数: 1637 | 1 【 以下文字转载自 Programming 讨论区 】
发信人: dryden (拽的狠-仁义礼智信), 信区: Programming
标 题: heap&stack Linux vs. Windows
发信站: BBS 未名空间站 (Fri Sep 14 13:29:51 2012, 美东)
最近才玩windows,发现windows 里面heap stack 位置不
太和linux一样。
一直以为win 和 Linux是一样的
欢迎讨论
thrust免进。 |
g**w 发帖数: 969 | 2 x86的stack,地址从高到低,这个window图肯定不对
【在 d****n 的大作中提到】 : 【 以下文字转载自 Programming 讨论区 】 : 发信人: dryden (拽的狠-仁义礼智信), 信区: Programming : 标 题: heap&stack Linux vs. Windows : 发信站: BBS 未名空间站 (Fri Sep 14 13:29:51 2012, 美东) : 最近才玩windows,发现windows 里面heap stack 位置不 : 太和linux一样。 : 一直以为win 和 Linux是一样的 : 欢迎讨论 : thrust免进。
|
d****n 发帖数: 1637 | 3 我和你开始想的一样。但是运行后就不一样了。
不知道是不是被windows paging给搞乱了
我贴code output
【在 g**w 的大作中提到】 : x86的stack,地址从高到低,这个window图肯定不对
|
d****n 发帖数: 1637 | 4 //in windows
#include
#include
using namespace std;
class A{
public :
int a;
int b;
};
int main()
{
cout<<&main<
int before=4;
cout<<"STACK &before="<<&before<
A objA;
void *p=malloc(1000);
cout<<"HEAP *p="<
cout<<"&objA.a"<<&(objA.a)<
cout<<"&objA.b"<<&(objA.b)<
cout<<"objA="<<&objA<
int after=4;
cout<<"STACK &after="<<&after<
return 0;
}
//output from windows win32
/*
00A0119A
STACK &before=0033FD00
HEAP *p=007983E0
&objA.a0033FCF0
&objA.b0033FCF4
objA=0033FCF0
STACK &after=0033FCD8
Press any key to continue . . .
*/ |
d****n 发帖数: 1637 | 5 //Linux 64bit
1
STACK &before=0x7fff6b620984
HEAP *p=0xa0ad010
&objA.a0x7fff6b620970
&objA.b0x7fff6b620974
objA=0x7fff6b620970
STACK &after=0x7fff6b62096c |
d****n 发帖数: 1637 | 6 stack 和 heap 高低正好调换了。
而且我也认为windows那个有点问题,stack should grow downward
高人给解释下 |
d****n 发帖数: 1637 | 7 重新编译了一下,发现 x64的和win32的不一样。x64的和linux 一致。
//windows 64
000000013F4D1000
STACK &before=000000000029FB00
HEAP *p=0000000000148860
&objA.a000000000029FB10
&objA.b000000000029FB14
objA=000000000029FB10
STACK &after=000000000029FB08
Press any key to continue . . . |
d****n 发帖数: 1637 | |
g**w 发帖数: 969 | 9 I think your code couldn't tell whether stack growing downward or not. in
main() function frame, it cut enough from stack for local variable, but it's
up to compiler to assign them to variables, which order doesn't matter.
you can try this: call another function from main, in that function print
out the address of variables, they should be lower than main()'s.
another one: heap & stack usually are just residing in two memory segments,
they are not related at all. only in old days, such as the programming C
language book, illustrate as they are growing toward each other. |
d****n 发帖数: 1637 | 10 thanks for you advice.
I just add a function in main()
print a local variable from fun()
Something like this.
void fun(){int a; cout<<&a<
main(){
fun();
}
###win32
0035119A
STACK &before=002BF998
HEAP *p=005D83E0
&objA.a002BF988
&objA.b002BF98C
objA=002BF988
STACK &after=002BF970
&a=002BF890 //local variable from fun()
###x64
000000013FB41000
STACK &before=00000000001BF798
HEAP *p=00000000005B67A0
&objA.a00000000001BF7A8
&objA.b00000000001BF7AC
objA=00000000001BF7A8
STACK &after=00000000001BF790
&a=00000000001BF7A0
~~~~~~
so my conclusion is both win32 grows stack downward.
but x64 goes the other way.
suggestions?
's
segments,
【在 g**w 的大作中提到】 : I think your code couldn't tell whether stack growing downward or not. in : main() function frame, it cut enough from stack for local variable, but it's : up to compiler to assign them to variables, which order doesn't matter. : you can try this: call another function from main, in that function print : out the address of variables, they should be lower than main()'s. : another one: heap & stack usually are just residing in two memory segments, : they are not related at all. only in old days, such as the programming C : language book, illustrate as they are growing toward each other.
|
g**w 发帖数: 969 | 11 x64 有点奇怪。建议turn off 优化。
其实用windbg很容易看,不用费劲的打印地址
【在 d****n 的大作中提到】 : thanks for you advice. : I just add a function in main() : print a local variable from fun() : Something like this. : void fun(){int a; cout<<&a<: main(){ : fun(); : } : ###win32 : 0035119A
|
d****n 发帖数: 1637 | 12 回去学习,谢谢了
【在 g**w 的大作中提到】 : x64 有点奇怪。建议turn off 优化。 : 其实用windbg很容易看,不用费劲的打印地址
|