gpt4 book ai didi

c - C中的内存起始位置

转载 作者:IT王子 更新时间:2023-10-28 23:38:00 27 4
gpt4 key购买 nike

我正在研究给定进程的内存布局。我注意到每个进程的起始内存位置不是0。在这个website , TEXT 从 0x08048000 开始。一个原因可能是用 NULL 指针来区分地址。我只是想知道是否还有其他好的理由?谢谢。

最佳答案

空指针实际上不必为 0。在 C 标准中保证,当在指针上下文中给出 0 值时,编译器会将其视为 NULL

但您在源代码中使用的 0 只是 语法糖,它与空指针值“指向”的实际物理地址无关。

更多详情见:

操作系统上的应用程序有其唯一的地址空间,它会将其视为连续的内存块(内存在物理上不是连续的,它只是操作系统给每个程序的“印象”)。

在大多数情况下,每个进程的虚拟内存空间都以相似且可预测的方式布局(这是 Linux 进程中的内存布局,32 位模式):

Memory layout in a Linux process(图片来自Anatomy of a Program in Memory)

查看文本段(基于 x86 的默认 .text 为 0x08048000,由默认链接描述文件选择用于静态绑定(bind))。

为什么是神奇的 0x08048000?可能是因为 Linux 从 System V i386 ABI 借用了该地址。

...为什么 System V 使用 0x08048000?

The value was chosen to accommodate the stack below the .text section, growing downward. The 0x48000 bytes could be mapped by the same page table already required by the .text section (thus saving a page table in most cases), while the remaining 0x08000000 would allow more room for stack-hungry applications.

0x08048000以下有什么吗?可能什么都没有(只有 128M),但是 you can pretty much map anything you desire there, using the mmap() system call .

另见:

关于c - C中的内存起始位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26272790/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com