gpt4 book ai didi

c++ - 具有空 main() 的程序可以分配内存吗?

转载 作者:搜寻专家 更新时间:2023-10-31 02:19:09 25 4
gpt4 key购买 nike

我一直在寻找链接到 -lcairo-lX11 的程序中的内存问题。最后,我决定注释掉 main() 中的所有行,并确保 valgrind 正常运行。令我惊讶的是,它不是:

==7570== Memcheck, a memory error detector
==7570== Copyright (C) 2002-2013, and GNU GPL'd, by Julian Seward et al.
==7570== Using Valgrind-3.10.0.SVN and LibVEX; rerun with -h for copyright info
==7570== Command: ./Test
==7570==
==7570==
==7570== HEAP SUMMARY:
==7570== in use at exit: 10,360 bytes in 5 blocks
==7570== total heap usage: 5 allocs, 0 frees, 10,360 bytes allocated
==7570==
==7570== 2,072 bytes in 1 blocks are still reachable in loss record 1 of 5
==7570== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7570== by 0x5FCCE9A: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==7570== by 0x5FCBACE: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==7570== by 0x5FCD585: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==7570== by 0x5F7F508: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==7570== by 0x4010139: call_init.part.0 (dl-init.c:78)
==7570== by 0x4010222: _dl_init (dl-init.c:36)
==7570== by 0x4001309: ??? (in /lib/x86_64-linux-gnu/ld-2.19.so)
==7570==
==7570== 2,072 bytes in 1 blocks are still reachable in loss record 2 of 5
==7570== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7570== by 0x5FCCE9A: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==7570== by 0x5FCA61F: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==7570== by 0x5FCD5A0: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==7570== by 0x5F7F508: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==7570== by 0x4010139: call_init.part.0 (dl-init.c:78)
==7570== by 0x4010222: _dl_init (dl-init.c:36)
==7570== by 0x4001309: ??? (in /lib/x86_64-linux-gnu/ld-2.19.so)
==7570==
==7570== 2,072 bytes in 1 blocks are still reachable in loss record 3 of 5
==7570== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7570== by 0x5FCCE9A: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==7570== by 0x5FE5A8F: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==7570== by 0x5FBC1A5: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==7570== by 0x5FCD5AB: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==7570== by 0x5F7F508: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==7570== by 0x4010139: call_init.part.0 (dl-init.c:78)
==7570== by 0x4010222: _dl_init (dl-init.c:36)
==7570== by 0x4001309: ??? (in /lib/x86_64-linux-gnu/ld-2.19.so)
==7570==
==7570== 2,072 bytes in 1 blocks are still reachable in loss record 4 of 5
==7570== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7570== by 0x5FCCE9A: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==7570== by 0x60053CF: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==7570== by 0x5FCD5AB: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==7570== by 0x5F7F508: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==7570== by 0x4010139: call_init.part.0 (dl-init.c:78)
==7570== by 0x4010222: _dl_init (dl-init.c:36)
==7570== by 0x4001309: ??? (in /lib/x86_64-linux-gnu/ld-2.19.so)
==7570==
==7570== 2,072 bytes in 1 blocks are still reachable in loss record 5 of 5
==7570== at 0x4C2AB80: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==7570== by 0x5FCCE9A: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==7570== by 0x5FCFCBF: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==7570== by 0x5F7F508: ??? (in /usr/lib/x86_64-linux-gnu/libpixman-1.so.0.30.2)
==7570== by 0x4010139: call_init.part.0 (dl-init.c:78)
==7570== by 0x4010222: _dl_init (dl-init.c:36)
==7570== by 0x4001309: ??? (in /lib/x86_64-linux-gnu/ld-2.19.so)
==7570==
==7570== LEAK SUMMARY:
==7570== definitely lost: 0 bytes in 0 blocks
==7570== indirectly lost: 0 bytes in 0 blocks
==7570== possibly lost: 0 bytes in 0 blocks
==7570== still reachable: 10,360 bytes in 5 blocks
==7570== suppressed: 0 bytes in 0 blocks
==7570==
==7570== For counts of detected and suppressed errors, rerun with: -v
==7570== ERROR SUMMARY: 0 errors from 0 contexts (suppressed: 0 from 0)

main() 为空的程序是否可以分配内存并以一些可访问的 block 结束?有没有办法摆脱这个奇怪的问题?

最佳答案

是的。这是可能的。

一种方法是,您要链接的库中可能有一些静态对象。 @M.M 已经指出了这一点

另一种方法是,如果您要链接到共享库,没有静态对象,并且此类库中的某些函数使用 __attribute__((constructor)) 。此属性导致这些函数在 main() 之前执行(仅适用于 gcc),因此您会看到内存分配。如果在您的程序中使用这些函数,库主要使用这些函数来声明有关其函数的某些属性。有关这些属性的更多详细信息,您可以引用:

http://gcc.gnu.org/onlinedocs/gcc/Function-Attributes.html

关于c++ - 具有空 main() 的程序可以分配内存吗?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33860276/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com