内存起始位置在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

相关文章

本程序的编译和运行环境如下(如果有运行方面的问题欢迎在评...
水了一学期的院选修,万万没想到期末考试还有比较硬核的编程...
补充一下,先前文章末尾给出的下载链接的完整代码含有部分C&...
思路如标题所说采用模N取余法,难点是这个除法过程如何实现。...
本篇博客有更新!!!更新后效果图如下: 文章末尾的完整代码...
刚开始学习模块化程序设计时,估计大家都被形参和实参搞迷糊...