由买买提看人间百态

boards

本页内容为未名空间相应帖子的节选和存档,一周内的贴子最多显示50字,超过一周显示500字 访问原贴
JobHunting版 - heap&stack Linux vs. Windows  (转载)
相关主题
one C++ questionC++ object size一问
heap里面delete一个非root的节点One C++ question
[leetcode] merge k lists 求助发个题目给大家复习一下marco
amazon的那道题目Why I can't compile this function successfully
Interview questions, BloombergC++: what is the output? How to interpret it?
一个容易记忆的permutation算法C++ Q42: (C22)
好记(但不是最优)的combination算法问个c++题
one C++ questionC++问题
相关话题的讨论汇总
话题: stack话题: heap话题: obja话题: endl话题: linux
进入JobHunting版参与讨论
1 (共1页)
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
8
我上面那个图是不是 win32 only ?
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很容易看,不用费劲的打印地址

1 (共1页)
进入JobHunting版参与讨论
相关主题
C++问题Interview questions, Bloomberg
弱问个C++ 问题 (const_cast)一个容易记忆的permutation算法
新手问个C++(Thinking in C++ source code)好记(但不是最优)的combination算法
新手请教:C++ decrement loop (转载)one C++ question
one C++ questionC++ object size一问
heap里面delete一个非root的节点One C++ question
[leetcode] merge k lists 求助发个题目给大家复习一下marco
amazon的那道题目Why I can't compile this function successfully
相关话题的讨论汇总
话题: stack话题: heap话题: obja话题: endl话题: linux