Baju
Baju

Reputation: 2826

malloc returns 0x100000000

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

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!

Upvotes: 5

Views: 1555

Answers (2)

Baju
Baju

Reputation: 2826

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

Upvotes: 1

bta
bta

Reputation: 45057

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.

Upvotes: 4

Related Questions