为什么全局变量存储在堆中? [英] Why global variables are stored in heap?

查看:283
本文介绍了为什么全局变量存储在堆中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

为什么在下面的例子中全局变量存储在堆段,而不是数据/ bss段?

Why in the following example the global variables are stored in the heap segment, instead of the data/bss segment?

从地图伪文件的下面的输出,数据/ BSS段第3行。这是因为它是读/写,和匿名。以下2项是堆(如标签指示)。

From the following output of the maps pseudo-file, the data/bss segment is the 3rd line. This is because it is read/write, and anonymous. The following 2 entries are the heap (as the label indicates).

这是在/ proc //地图的输出:

This is the output of the /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]

然而,当我打印2全局变量的位置,我得到属于堆VM区域以下地址:

However, when I print the location of 2 global variables, I get the following addresses that belong to the heap vm area:

glb1:6061b0 glb2:6061c0

我打印使用地点:

I print the locations using:

printf("glb1:%lx glb2:%lx\n", (uint64_t) &glb1, (uint64_t) &glb2);


推荐答案

的BSS / DATA是与所有的全局定义的变量,通过缺省初始化为一个特定的值或零的段。这部分是可执行图像的一部分。

The BSS/DATA is the segment with all the globally defined variables, initialized to a specific value or to zero by default. This segment is part of the executable image.

堆段只是的的将在程序加载被分配额外的存储量。它不包含的图像中。此金额以下的BSS /数据段的终点。

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和堆段具有相同的基址。 /想堆段虚拟的作为图像的BSS /数据段的虚拟extentsion。

Hence the BSS/DATA and heap segment have the same base address. Think of the heap segment as virtual/as a virtual extentsion of the bss/data segment of the image.

最后,经常栈段再次被添加到这个量,而堆长大,堆栈向下增长音符。 (这可能是编译器相关的。)

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.)

这篇关于为什么全局变量存储在堆中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆