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:
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
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!
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With