1

当我打印正在运行的进程的内容时,我得到了这样的结果

00400000-00401000 r-xp 00000000 08:01 137910                             /home/user/process/a.out
00600000-00601000 rw-p 00000000 08:01 137910                             /home/user/process/a.out
02434000-02455000 rw-p 00000000 00:00 0                                  [heap]

为什么 a.out 在两个内存区域加载?是相同的部分还是不同的部分。一个得到了r-xp,另一个得到了rw-p。尽管我在程序中使用 malloc 分配了一个整数大小,但显示的堆大小不止于此。为什么会这样?

4

1 回答 1

3

The r-xp region at 0x400000 is your text (code) section. It's readable, and executable, but not writable.

The rw-p region at 0x600000 is your data section. It's readable and writable, but not executable.

readelf -S a.out will show you the sections in your executable, and where they are going to be loaded into memory (first through a section-to-segment mapping.)


Your experiment about mallocing 4 bytes is insufficient, because that's not how memory management works. When you call malloc, your libc implementation is going to carve out a small piece out of the large pool that it is maintaining. (This is just your process doing this - the OS is not immediately involved.) When you have depleted that pool, it will use the brk (or mmap) system call to ask the kernel to give it more memory.

If you malloc a much larger amount of memory, you will probably see the heap grow. Also, you can run strace on your executable, and see when it actually makes the brk or memmap system calls.

于 2013-09-26T02:21:42.027 回答