Search code examples
memorygdbmalloccorruption

Malloc failed to allocate bytes even there is sufficient amount of bytes available


I tried to allocate 21,128 bytes using a wrapper function, which calls malloc internally.

Malloc Stats() :-

system bytes     =   14618624
in use bytes     =   13759424
Arena 1:

Arena 0:
system bytes     =   14626816
in use bytes     =   13759600
Arena 1:
system bytes     =     135168
in use bytes     =       3280
Arena 2:
system bytes     =     135168
in use bytes     =      13088

But still, I see malloc has failed. What could be possibly the reason?

*** Error in `./wr_acc': malloc(): memory corruption: 0x00007ff4747a2ff0 ***

======= Backtrace: =========
/lib64/libc.so.6(+0x82c86)[0x7ff48c9d5c86]
/lib64/libc.so.6(__libc_malloc+0x4c)[0x7ff48c9d884c]
./wr_acc[0xdf4c28]

Please help. I am a beginner.


Solution

  • The error message is clear:

    *** Error in `./wr_acc': malloc(): memory corruption: 0x00007ff4747a2ff0 ***
    

    malloc() detected an invalid state in its ancillary structures so it gave up trying to allocate memory and aborted the program to avoid potentially damaging side effects.

    The private data malloc() uses to keep track of allocated and free blocks may have been overwritten by your program, for example by writing beyond the end of an allocated block or before its beginning. You could post the code and see if anyone can spot such problems.