抱歉,您的浏览器无法访问本站

本页面需要浏览器支持(启用)JavaScript


了解详情 >

Linux-g++gcc编译后栈内存地址与实际相反

栈正常存储内存地址

image-20220927181002783

栈空间先使用高地址在使用底地址

Win_Devc++

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

using namespace std;

int main() {
int a = 0;
int b = 0;
printf("a == %p\n", &a);
printf("b == %p\n", &b);
return 0;
}

代码_截图

image-20220927155249912

运行_结果

1
2
3
a == 00000000006ffe1c
b == 00000000006ffe18
--------------------------------

Vim(Linux)

1
2
3
4
5
6
7
8
9
10
11
#include <iostream>

using namespace std;

int main() {
int a = 0;
int b = 0;
printf("a == %p\n", &a);
printf("b == %p\n", &b);
return 0;
}

运行_结果(默认)

发现二个变量 a, b在栈中存储的地址由高到底分别为 a>b> 跟课上面dev正好相反.

1
2
a == 0x7ffffa941740
b == 0x7ffffa941744

关闭栈保护

关闭栈保护后的结果:
与推断的结果相同.

1
g++ test.cpp  -fno-stack-protector -o ttw
1
2
a == 0x7fffc18bfd1c
b == 0x7fffc18bfd18

运行截图

image-20220927181441626

评论