微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

内存起始位置在C

参见英文答案 > Why do virtual memory addresses for linux binaries start at 0x8048000?1个
我正在研究给定进程的内存布局.我注意到每个进程的起始内存位置不是0.在这website,TEXT从0x08048000开始.一个原因可以是使用NULL指针区分地址.我只是想知道是否还有其他好的理由?谢谢.

解决方法

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

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

详情请见:

> Why is NULL/0 an illegal memory location for an object?
> Why is address zero used for the null pointer?

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

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

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

查看文本段(x86上的认.text基础是0x08048000,由静态绑定的链接描述文件选择).

为什么神奇的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.

也可以看看:

> What’s the memory before 0x08048000 used for in 32 bit machine?
> Reorganizing the address space
> mmap

原文地址:https://www.jb51.cc/c/117842.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐