在 Linux 中创建堆区域/段

oth*_*man 4 memory linux-kernel memory-management

据我了解,在 Linux 上,进程加载器不会像堆栈那样自动为进程创建“堆”区域,这是正确的吗?

我使用过/proc/$$/maps,在调用之前malloc()没有“堆”区域。

cha*_*aos 5

Linux 不会为堆段和堆栈段“自动创建”区域。

\n\n

在编程语言中总是有一个入口点,控制权从操作系统转移到程序。在C中,这就是main()函数。

\n\n

Linux 中的每个进程在 32 位环境中具有 4GB 的内存映射,在 64 位环境中具有 8TB 的内存映射。这是操作系统可以寻址的最大内存量。请注意,这与您的系统实际拥有的物理内存数量无关。每个进程都给人一种错觉,以为它是单独在计算机上的。

\n\n

在调用之前main(),操作系统将用于调用程序的命令行元素推送到最初为空的堆栈的“顶部”,这就是堆栈所在的位置。如果main()调用自身为函数,则传递的参数将被推送到堆栈段中。

\n\n
 4GB/8TB\xe2\x94\x8c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x90\n        \xe2\x94\x82   stack   \xe2\x94\x82 <- main(), argc/argv parameters and functions, growing downwards\n        \xe2\x94\x9c \xe2\x94\x80 \xe2\x94\x80 \xe2\x94\x80 \xe2\x94\x80 \xe2\x94\x80 \xe2\x94\xa4\n        \xe2\x94\x82           \xe2\x94\x82\n        \xe2\x94\x82           \xe2\x94\x82 <- empty\n        \xe2\x94\x82           \xe2\x94\x82\n        \xe2\x94\x82           \xe2\x94\x82\n        \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xa4\n        \xe2\x94\x82   data    \xe2\x94\x82 <- static data and literal constants\n        \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xa4\n        \xe2\x94\x82   text    \xe2\x94\x82 <- all the instructions; the compiled code\n       0\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x98\n
Run Code Online (Sandbox Code Playgroud)\n\n
\n\n

现在你的问题是:

\n\n
\n

我使用过/proc/$$/maps,在调用 malloc 之前没有“堆”区域。

\n
\n\n

内存:进程可以在运行时使用malloc()系统调用扩展到虚拟内存的未占用部分。该动态分配的内存位于堆段数据的上方。因此,在第一次调用之前没有堆区域或段是正常的。malloc()

\n\n
 4GB/8TB\xe2\x94\x8c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x90\n        \xe2\x94\x82   stack   \xe2\x94\x82 <- main(), argc/argv parameters and functions, growing downwards\n        \xe2\x94\x9c \xe2\x94\x80 \xe2\x94\x80 \xe2\x94\x80 \xe2\x94\x80 \xe2\x94\x80 \xe2\x94\xa4\n        \xe2\x94\x82           \xe2\x94\x82\n        \xe2\x94\x82           \xe2\x94\x82 <- empty\n        \xe2\x94\x9c \xe2\x94\x80 \xe2\x94\x80 \xe2\x94\x80 \xe2\x94\x80 \xe2\x94\x80 \xe2\x94\xa4\n        \xe2\x94\x82   heap    \xe2\x94\x82 <- malloc()\'d memory space, growing upwards\n        \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xa4\n        \xe2\x94\x82   data    \xe2\x94\x82 <- static data and literal constants\n        \xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\xa4\n        \xe2\x94\x82   text    \xe2\x94\x82 <- all the instructions; the compiled code\n       0\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x80\xe2\x94\x98\n
Run Code Online (Sandbox Code Playgroud)\n