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

#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]

我在Ubuntu18.04和gcc版本是7.4.0上试过这个,你们能帮我理解为什么堆部分是创建的,即使我们没有malloc。

最佳答案

试试这个

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

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

关于c - 当没有使用malloc时,为什么堆部分存在,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/57300908/

10-11 21:17