获取当前进程堆的起始地址?
我正在探索系统的较低级别工作原理,并且想知道 malloc 如何确定堆的起始地址。堆的偏移量是否恒定,或者是否有某种调用来获取起始地址?栈会影响堆的起始地址吗?
I am exploring the lower level workings of the system, and was wondering how malloc
determines the start address of the heap. Is the heap at a constant offset or is there a call of some sort to get the start address? Does the stack affect the start address of the heap?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
sbrk 返回它添加(或删除)的字节的起始地址。在尚未分配堆的新进程中,对 sbrk 的第一次调用应返回堆的“break”部分的起始地址。如果我必须打赌,这就是使用 brk/sbrk 的 malloc 实现在第一次运行时可能会做的事情。
sbrk returns the start address of the bytes it adds (or removes). In a fresh process with no heap allocated yet, the first call to sbrk should then return the start address of the "break" section of the heap. If I had to bet, that's what malloc implementations which use brk/sbrk probably do on their first run.
传统上,堆从文本部分上方开始并逐渐增大。堆栈帧根本不影响起始地址,因为它们会向下增长到未映射的 0 页。然而,现在更常见的是
malloc()
通常只是调用mmap()
获取虚拟地址空间中任意位置的地址Traditionally, the heap started just above the text section and grew up; stack frames didn't affect start address at all as they grow down towards the unmapped 0 page. However, it's more common these days for
malloc()
usually just callsmmap()
to get an address anywhere in the virtual address space