[英]Why global variables are stored in heap?
Why in the following example the global variables are stored in the heap segment, instead of the data/bss segment? 为什么在以下示例中,全局变量存储在堆段中,而不是存储在data / bss段中?
From the following output of the maps pseudo-file, the data/bss segment is the 3rd line. 从map伪文件的以下输出中,data / bss段是第3行。 This is because it is read/write, and anonymous.
这是因为它是读/写和匿名的。 The following 2 entries are the heap (as the label indicates).
以下2个条目是堆(如标签所示)。
This is the output of the /proc//maps: 这是/ proc // maps的输出:
00400000-00405000 r-xp 00000000 08:02 17962770 myexec
00604000-00605000 r--p 00004000 08:02 17962770 myexec
00605000-00606000 rw-p 00005000 08:02 17962770 myexec
00606000-00607000 rw-p 00000000 00:00 0 [heap]
00607000-00642000 rw-p 00000000 00:00 0 [heap]
7ffff7a15000-7ffff7bd0000 r-xp 00000000 08:02 22282470 ..libc2.19.so
7ffff7bd0000-7ffff7dcf000 ---p 001bb000 08:02 22282470 ..libc-.19.so
7ffff7dcf000-7ffff7dd3000 r--p 001ba000 08:02 22282470 ..libc-.19.so
7ffff7dd3000-7ffff7dd5000 rw-p 001be000 08:02 22282470 ..libc-.19.so
7ffff7dd5000-7ffff7dda000 rw-p 00000000 00:00 0
7ffff7dda000-7ffff7dfd000 r-xp 00000000 08:02 22282466 ..ld-2.19.so
7ffff7fec000-7ffff7fef000 rw-p 00000000 00:00 0
7ffff7ff6000-7ffff7ff7000 rw-p 00000000 00:00 0
7ffff7ff7000-7ffff7ffa000 rw-p 00000000 00:00 0
7ffff7ffa000-7ffff7ffc000 r-xp 00000000 00:00 0 [vdso]
7ffff7ffc000-7ffff7ffd000 r--p 00022000 08:02 22282466 ..ld-2.19.so
7ffff7ffd000-7ffff7ffe000 rw-p 00023000 08:02 22282466 ..ld-2.19.so
7ffff7ffe000-7ffff7fff000 rw-p 00000000 00:00 0
7ffffffde000-7ffffffff000 rw-p 00000000 00:00 0 [stack]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0 [vsyscall]
However, when I print the location of 2 global variables, I get the following addresses that belong to the heap vm area: 但是,当我打印2个全局变量的位置时,我得到以下属于堆vm区域的地址:
glb1:6061b0 glb2:6061c0
I print the locations using: 我使用以下方式打印位置:
printf("glb1:%lx glb2:%lx\n", (uint64_t) &glb1, (uint64_t) &glb2);
The BSS/DATA is the segment with all the globally defined variables, initialized to a specific value or to zero by default. BSS / DATA是具有所有全局定义变量的段,默认情况下初始化为特定值或零。 This segment is part of the executable image.
该段是可执行映像的一部分。
The heap "segment" is just the amount of additional storage that will be allocated at program load. 堆“段”是只是将在程序装入待分配的额外的存储量 。 It is not contained in the image.
它不包含在图像中。 This amount follows at the end of the BSS/DATA segment.
该数量在BSS / DATA段结束时跟随。
Hence the BSS/DATA and heap segment have the same base address. 因此,BSS / DATA和堆段具有相同的基址。 Think of the heap segment as virtual/as a virtual extentsion of the bss/data segment of the image.
将堆段视为虚拟/作为图像的bss /数据段的虚拟扩展。
Lastly, note that often the stack "segment" is added again to this amount and, whereas the heap grows up, the stack grows down. 最后,请注意,堆栈“段”通常会再次添加到此数量,而堆增长时,堆栈会逐渐减少。 (This may be compiler dependent.)
(这可能与编译器有关。)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.