0

如果我们不调用 malloc,我试图找出进程的地址空间中是否存在堆。

#include <stdio.h>
int main()
{
    getchar();
    return 0;
}

即使我不调用 malloc,地图中也存在堆部分

cat /proc/73268/maps
55d0b405c000-55d0b4160000 r-xp 00000000 08:01 5505031                    /bin/bash
55d0b435f000-55d0b4363000 r--p 00103000 08:01 5505031                    /bin/bash
55d0b4363000-55d0b436c000 rw-p 00107000 08:01 5505031                    /bin/bash
55d0b436c000-55d0b4376000 rw-p 00000000 00:00 0 
55d0b567e000-55d0b5815000 rw-p 00000000 00:00 0                          [heap]
7f57533fd000-7f5753408000 r-xp 00000000 08:01 2102129                    /lib/x86_64-linux-gnu/libnss_files-2.27.so
7f5753408000-7f5753607000 ---p 0000b000 08:01 2102129                    /lib/x86_64-linux-gnu/libnss_files-2.27.so
7f5753607000-7f5753608000 r--p 0000a000 08:01 2102129                    /lib/x86_64-linux-gnu/libnss_files-2.27.so
7f5753608000-7f5753609000 rw-p 0000b000 08:01 2102129                    /lib/x86_64-linux-gnu/libnss_files-2.27.so
7f5753609000-7f575360f000 rw-p 00000000 00:00 0 
7f575360f000-7f5753626000 r-xp 00000000 08:01 2102123                    /lib/x86_64-linux-gnu/libnsl-2.27.so
7f5753626000-7f5753825000 ---p 00017000 08:01 2102123                    /lib/x86_64-linux-gnu/libnsl-2.27.so
7f5753825000-7f5753826000 r--p 00016000 08:01 2102123                    /lib/x86_64-linux-gnu/libnsl-2.27.so
7f5753826000-7f5753827000 rw-p 00017000 08:01 2102123                    /lib/x86_64-linux-gnu/libnsl-2.27.so
7f5753827000-7f5753829000 rw-p 00000000 00:00 0 
7f5753829000-7f5753834000 r-xp 00000000 08:01 2102140                    /lib/x86_64-linux-gnu/libnss_nis-2.27.so
7f5753834000-7f5753a33000 ---p 0000b000 08:01 2102140                    /lib/x86_64-linux-gnu/libnss_nis-2.27.so
7f5753a33000-7f5753a34000 r--p 0000a000 08:01 2102140                    /lib/x86_64-linux-gnu/libnss_nis-2.27.so
7f5753a34000-7f5753a35000 rw-p 0000b000 08:01 2102140                    /lib/x86_64-linux-gnu/libnss_nis-2.27.so
7f5753a35000-7f5753a3d000 r-xp 00000000 08:01 2102125                    /lib/x86_64-linux-gnu/libnss_compat-2.27.so
7f5753a3d000-7f5753c3d000 ---p 00008000 08:01 2102125                    /lib/x86_64-linux-gnu/libnss_compat-2.27.so
7f5753c3d000-7f5753c3e000 r--p 00008000 08:01 2102125                    /lib/x86_64-linux-gnu/libnss_compat-2.27.so
7f5753c3e000-7f5753c3f000 rw-p 00009000 08:01 2102125                    /lib/x86_64-linux-gnu/libnss_compat-2.27.so
7f5753c3f000-7f575460e000 r--p 00000000 08:01 793285                     /usr/lib/locale/locale-archive
7f575460e000-7f57547f5000 r-xp 00000000 08:01 2102039                    /lib/x86_64-linux-gnu/libc-2.27.so
7f57547f5000-7f57549f5000 ---p 001e7000 08:01 2102039                    /lib/x86_64-linux-gnu/libc-2.27.so
7f57549f5000-7f57549f9000 r--p 001e7000 08:01 2102039                    /lib/x86_64-linux-gnu/libc-2.27.so
7f57549f9000-7f57549fb000 rw-p 001eb000 08:01 2102039                    /lib/x86_64-linux-gnu/libc-2.27.so
7f57549fb000-7f57549ff000 rw-p 00000000 00:00 0 
7f57549ff000-7f5754a02000 r-xp 00000000 08:01 2102062                    /lib/x86_64-linux-gnu/libdl-2.27.so
7f5754a02000-7f5754c01000 ---p 00003000 08:01 2102062                    /lib/x86_64-linux-gnu/libdl-2.27.so
7f5754c01000-7f5754c02000 r--p 00002000 08:01 2102062                    /lib/x86_64-linux-gnu/libdl-2.27.so
7f5754c02000-7f5754c03000 rw-p 00003000 08:01 2102062                    /lib/x86_64-linux-gnu/libdl-2.27.so
7f5754c03000-7f5754c28000 r-xp 00000000 08:01 2102197                    /lib/x86_64-linux-gnu/libtinfo.so.5.9
7f5754c28000-7f5754e28000 ---p 00025000 08:01 2102197                    /lib/x86_64-linux-gnu/libtinfo.so.5.9
7f5754e28000-7f5754e2c000 r--p 00025000 08:01 2102197                    /lib/x86_64-linux-gnu/libtinfo.so.5.9
7f5754e2c000-7f5754e2d000 rw-p 00029000 08:01 2102197                    /lib/x86_64-linux-gnu/libtinfo.so.5.9
7f5754e2d000-7f5754e54000 r-xp 00000000 08:01 2102011                    /lib/x86_64-linux-gnu/ld-2.27.so
7f5754fb6000-7f575503c000 rw-p 00000000 00:00 0 
7f575504d000-7f5755054000 r--s 00000000 08:01 1050910                    /usr/lib/x86_64-linux-gnu/gconv/gconv-modules.cache
7f5755054000-7f5755055000 r--p 00027000 08:01 2102011                    /lib/x86_64-linux-gnu/ld-2.27.so
7f5755055000-7f5755056000 rw-p 00028000 08:01 2102011                    /lib/x86_64-linux-gnu/ld-2.27.so
7f5755056000-7f5755057000 rw-p 00000000 00:00 0 
7ffd41cc9000-7ffd41cea000 rw-p 00000000 00:00 0                          [stack]
7ffd41db7000-7ffd41dba000 r--p 00000000 00:00 0                          [vvar]
7ffd41dba000-7ffd41dbc000 r-xp 00000000 00:00 0                          [vdso]
ffffffffff600000-ffffffffff601000 r-xp 00000000 00:00 0                  [vsyscall]

我在 Ubuntu 18.04 上试过这个,gcc 版本是 7.4.0,你们能帮我理解为什么即使我们没有任何 malloc 也会创建堆部分。

4

2 回答 2

1

尝试这个

int main()
{
        for(;;);
}

并编译它gcc test.c -nodefaultlibs -Wl,-e,main -o test -nostdlib -static

于 2019-08-01T05:39:47.847 回答
0

您的程序正在使用malloc(),至少在您的系统中使用标准 glibc 时:

$ cat failmalloc.c
#include <unistd.h>
#define MSG     "out of memory!\n"
void *malloc(size_t z){
        write(2, MSG, sizeof MSG - 1); _exit(13);
}
$ cc -Wall -shared failmalloc.c -o failmalloc.so
$ LD_PRELOAD=./failmalloc.so ./your_program
out of memory!

此外,不能保证malloc()会使用“堆”。有malloc()专门基于mmap(2).

于 2019-08-01T12:07:18.373 回答