malloc 返回 0x100000000

发布于 2024-10-24 04:31:30 字数 2626 浏览 0 评论 0原文

我在 malloc 方面遇到了一个奇怪的问题。在使用多个 malloc 分配 <10mb 后,malloc 突然返回地址 0x100000000,这会在访问时导致 SIGSEGV。我不知道出了什么问题。 errno 设置为 0 并且我的内存有足够的空间,所以它不应该是一个空间问题。 malloc 返回的最后地址小于 0x6255f0。知道要寻找什么吗?

有关我的系统的一些信息:

  • gcc version 4.4.5 (Ubuntu/Linaro 4.4.4-14ubuntu5)
  • flags: -Wall
  • uname: 2.6.35-27-generic #48-Ubuntu SMP Tue Feb 22 20:25:46 UTC 2011 x86_64 GNU/Linux

PMAP 输出:

Address           Kbytes     RSS   Dirty Mode   Mapping
0000000000400000       0      32       0 r-x--  tests
000000000060a000       0       4       4 r----  tests
000000000060b000       0       4       4 rw---  tests
000000000060c000       0     116     116 rw---    [ anon ]
00007ffff75cd000       0     348       0 r-x--  libc-2.12.1.so
00007ffff7747000       0       0       0 -----  libc-2.12.1.so
00007ffff7946000       0      16      16 r----  libc-2.12.1.so
00007ffff794a000       0       4       4 rw---  libc-2.12.1.so
00007ffff794b000       0      12      12 rw---    [ anon ]
00007ffff7950000       0      32       0 r-x--  libm-2.12.1.so
00007ffff79d2000       0       0       0 -----  libm-2.12.1.so
00007ffff7bd1000       0       4       4 r----  libm-2.12.1.so
00007ffff7bd2000       0       4       4 rw---  libm-2.12.1.so
00007ffff7bd3000       0      28       0 r-x--  liblinopt.so
00007ffff7bdb000       0       0       0 -----  liblinopt.so
00007ffff7dda000       0       4       4 r----  liblinopt.so
00007ffff7ddb000       0       4       4 rw---  liblinopt.so
00007ffff7ddc000       0     108       4 r-x--  ld-2.12.1.so
00007ffff7f6c000       0     432     432 rw---    [ anon ]
00007ffff7ff8000       0      12      12 rw---    [ anon ]
00007ffff7ffb000       0       4       0 r-x--    [ anon ]
00007ffff7ffc000       0       4       4 r----  ld-2.12.1.so
00007ffff7ffd000       0       4       4 rw---  ld-2.12.1.so
00007ffff7ffe000       0       4       4 rw---    [ anon ]
00007ffffffde000       0      16      16 rw---    [ stack ]
ffffffffff600000       0       0       0 r-x--    [ anon ]
----------------  ------  ------  ------
total kB            9160    1196     648

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7bd8e67 in bound_knapsack (sizes=0x610b30, profits=0x610ad0, B=103, 
    limit=2) at /home/x/Development/binpacking/src/lib/knapsack.c:123

更新

运行 valgrind 揭示了问题:它是之前几行的 calloc: calloc( n, sizeof(unsigned int)); 这应该是: calloc( n, sizeof(unsigned int*)); 这导致用于存储 malloc 结果的分配块太小。 叹气

谢谢你的橡皮鸭!

I have a strange problem with malloc. After allocating <10mb with a number of mallocs, malloc suddenly returns the address 0x100000000, which causes a SIGSEGV when accessed. I have no idea what is wrong. The errno is set to 0 and I have enough space in ram, so it shouldn't be a space problem. The last addresses returned by malloc were smaller than 0x6255f0. Any idea what to look for?

Some info about my system:

  • gcc version 4.4.5 (Ubuntu/Linaro 4.4.4-14ubuntu5)
  • flags: -Wall
  • uname: 2.6.35-27-generic #48-Ubuntu SMP Tue Feb 22 20:25:46 UTC 2011 x86_64 GNU/Linux

PMAP output:

Address           Kbytes     RSS   Dirty Mode   Mapping
0000000000400000       0      32       0 r-x--  tests
000000000060a000       0       4       4 r----  tests
000000000060b000       0       4       4 rw---  tests
000000000060c000       0     116     116 rw---    [ anon ]
00007ffff75cd000       0     348       0 r-x--  libc-2.12.1.so
00007ffff7747000       0       0       0 -----  libc-2.12.1.so
00007ffff7946000       0      16      16 r----  libc-2.12.1.so
00007ffff794a000       0       4       4 rw---  libc-2.12.1.so
00007ffff794b000       0      12      12 rw---    [ anon ]
00007ffff7950000       0      32       0 r-x--  libm-2.12.1.so
00007ffff79d2000       0       0       0 -----  libm-2.12.1.so
00007ffff7bd1000       0       4       4 r----  libm-2.12.1.so
00007ffff7bd2000       0       4       4 rw---  libm-2.12.1.so
00007ffff7bd3000       0      28       0 r-x--  liblinopt.so
00007ffff7bdb000       0       0       0 -----  liblinopt.so
00007ffff7dda000       0       4       4 r----  liblinopt.so
00007ffff7ddb000       0       4       4 rw---  liblinopt.so
00007ffff7ddc000       0     108       4 r-x--  ld-2.12.1.so
00007ffff7f6c000       0     432     432 rw---    [ anon ]
00007ffff7ff8000       0      12      12 rw---    [ anon ]
00007ffff7ffb000       0       4       0 r-x--    [ anon ]
00007ffff7ffc000       0       4       4 r----  ld-2.12.1.so
00007ffff7ffd000       0       4       4 rw---  ld-2.12.1.so
00007ffff7ffe000       0       4       4 rw---    [ anon ]
00007ffffffde000       0      16      16 rw---    [ stack ]
ffffffffff600000       0       0       0 r-x--    [ anon ]
----------------  ------  ------  ------
total kB            9160    1196     648

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7bd8e67 in bound_knapsack (sizes=0x610b30, profits=0x610ad0, B=103, 
    limit=2) at /home/x/Development/binpacking/src/lib/knapsack.c:123

UPDATE

Running valgrind revealed the problem: it was a calloc some lines before:
calloc( n, sizeof(unsigned int));
which should have been:
calloc( n, sizeof(unsigned int*));
which lead to a too small allocated block, which is used to store the result of the malloc.
sigh

Thank you for rubber ducking!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

傲世九天 2024-10-31 04:31:30

尝试将以下行添加到您的程序中:

#define MALLOC_CHECK_ 3

这应该会导致使用不同版本的 malloc,该版本可以检测某些错误并将其报告给您。有关更多详细信息,请参阅 malloc 手册页 的“注释”部分。

Try adding the following line to your program:

#define MALLOC_CHECK_ 3

This should cause a different version of malloc to be used, one that can detect certain errors and report them to you. See the 'Notes' section of the malloc man page for more details.

£冰雨忧蓝° 2024-10-31 04:31:30

malloc 工作正常,但结果未存储。 (参见更新)。
感谢您的建议。

如果您仍然不想看到一些代码:
github/knapsack.c

malloc worked fine, but the result wasn't stored. ( see Update ).
Thank you for your suggestions.

If you still wan't to see some code:
github/knapsack.c

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文