程序一开始执行,操作系统为其分配“虚拟地址空间” ,对于x-86架构的32bit操作系统,会分配2^32 = 4G Byte 的虚拟地址空间,对于64bit系统,理论上会分配2^64 Byte 大小的虚拟内存,但目前没有这么大的需要,所以一般只分配2^48 Byte
#include <cstdio>
#include <thread>
const char c = 'c';
const char * str{"abc"};
int g_i = 0;
int main()
{
const int a = 100;
auto b = new int[20]{0};
::printf("全局初始化const对象 -> %p\n", std::addressof(c));
::printf("全局常量指针对象 -> %p\n", std::addressof(str));
::printf("字符字面量 -> %p\n", std::addressof("abc"));
::printf("全局未初始化对象 -> %p\n", std::addressof(g_i));
::printf("局部对象 -> %p\n", std::addressof(a));
::printf("动态分配空间 -> %p\n", std::addressof(*b));
std::this_thread::sleep_for(std::chrono::seconds(100));
delete[] b;
return 0;
}
全局初始化const对象 -> 0x55bfad049012
全局常量指针对象 -> 0x55bfad04b010
字符字面量 -> 0x55bfad049013
全局未初始化整形对象 -> 0x55bfad04b01c
局部变量 -> 0x7fffed525af0
动态分配空间 -> 0x55bfadb84eb0
g++ test.cpp; ./a.out; ps -ef | grep a.out cat /proc/[pid]/maps
55bfad047000-55bfad048000 r--p 00000000 08:05 2494317 /home/yz/share/c++memerylearn/a.out
55bfad048000-55bfad049000 r-xp 00001000 08:05 2494317 /home/yz/share/c++memerylearn/a.out
55bfad049000-55bfad04a000 r--p 00002000 08:05 2494317 /home/yz/share/c++memerylearn/a.out
55bfad04a000-55bfad04b000 r--p 00002000 08:05 2494317 /home/yz/share/c++memerylearn/a.out
55bfad04b000-55bfad04c000 rw-p 00003000 08:05 2494317 /home/yz/share/c++memerylearn/a.out
55bfadb73000-55bfadb94000 rw-p 00000000 00:00 0 [heap]
7f7131915000-7f7131919000 rw-p 00000000 00:00 0
7f7131919000-7f713191c000 r--p 00000000 08:05 656457 /usr/lib/x86_64-linux-gnu/libgcc_s.so.1
7f713191c000-7f713192e000 r-xp 00003000 08:05 656457 /usr/lib/x86_64-linux-gnu/libgcc_s.so.1
7f713192e000-7f7131932000 r--p 00015000 08:05 656457 /usr/lib/x86_64-linux-gnu/libgcc_s.so.1
7f7131932000-7f7131933000 r--p 00018000 08:05 656457 /usr/lib/x86_64-linux-gnu/libgcc_s.so.1
7f7131933000-7f7131934000 rw-p 00019000 08:05 656457 /usr/lib/x86_64-linux-gnu/libgcc_s.so.1
7f7131934000-7f7131943000 r--p 00000000 08:05 662665 /usr/lib/x86_64-linux-gnu/libm-2.31.so
7f7131943000-7f71319ea000 r-xp 0000f000 08:05 662665 /usr/lib/x86_64-linux-gnu/libm-2.31.so
7f71319ea000-7f7131a81000 r--p 000b6000 08:05 662665 /usr/lib/x86_64-linux-gnu/libm-2.31.so
7f7131a81000-7f7131a82000 r--p 0014c000 08:05 662665 /usr/lib/x86_64-linux-gnu/libm-2.31.so
7f7131a82000-7f7131a83000 rw-p 0014d000 08:05 662665 /usr/lib/x86_64-linux-gnu/libm-2.31.so
7f7131a83000-7f7131aa8000 r--p 00000000 08:05 662023 /usr/lib/x86_64-linux-gnu/libc-2.31.so
7f7131aa8000-7f7131c20000 r-xp 00025000 08:05 662023 /usr/lib/x86_64-linux-gnu/libc-2.31.so
7f7131c20000-7f7131c6a000 r--p 0019d000 08:05 662023 /usr/lib/x86_64-linux-gnu/libc-2.31.so
7f7131c6a000-7f7131c6b000 ---p 001e7000 08:05 662023 /usr/lib/x86_64-linux-gnu/libc-2.31.so
7f7131c6b000-7f7131c6e000 r--p 001e7000 08:05 662023 /usr/lib/x86_64-linux-gnu/libc-2.31.so
7f7131c6e000-7f7131c71000 rw-p 001ea000 08:05 662023 /usr/lib/x86_64-linux-gnu/libc-2.31.so
7f7131c71000-7f7131c75000 rw-p 00000000 00:00 0
7f7131c75000-7f7131d0b000 r--p 00000000 08:05 660665 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28
7f7131d0b000-7f7131dfc000 r-xp 00096000 08:05 660665 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28
7f7131dfc000-7f7131e45000 r--p 00187000 08:05 660665 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28
7f7131e45000-7f7131e46000 ---p 001d0000 08:05 660665 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28
7f7131e46000-7f7131e51000 r--p 001d0000 08:05 660665 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28
7f7131e51000-7f7131e54000 rw-p 001db000 08:05 660665 /usr/lib/x86_64-linux-gnu/libstdc++.so.6.0.28
7f7131e54000-7f7131e59000 rw-p 00000000 00:00 0
7f7131e6b000-7f7131e6c000 r--p 00000000 08:05 661807 /usr/lib/x86_64-linux-gnu/ld-2.31.so
7f7131e6c000-7f7131e8f000 r-xp 00001000 08:05 661807 /usr/lib/x86_64-linux-gnu/ld-2.31.so
7f7131e8f000-7f7131e97000 r--p 00024000 08:05 661807 /usr/lib/x86_64-linux-gnu/ld-2.31.so
7f7131e98000-7f7131e99000 r--p 0002c000 08:05 661807 /usr/lib/x86_64-linux-gnu/ld-2.31.so
7f7131e99000-7f7131e9a000 rw-p 0002d000 08:05 661807 /usr/lib/x86_64-linux-gnu/ld-2.31.so
7f7131e9a000-7f7131e9b000 rw-p 00000000 00:00 0
7fffed506000-7fffed527000 rw-p 00000000 00:00 0 [stack]
7fffed5f8000-7fffed5fc000 r--p 00000000 00:00 0 [vvar]
7fffed5fc000-7fffed5fe000 r-xp 00000000 00:00 0 [vdso]
ffffffffff600000-ffffffffff601000 --xp 00000000 00:00 0 [vsyscall]
全局初始化const对象 -> .rodata
全局常量指针对象 -> .data
字符字面量 -> .rodata
全局未初始化对象 -> .bss
局部对象 -> stack
动态分配空间 -> heap
|