siri
siri

Reputation: 723

malloc error in c++

Hi when I was trying to execute my program(c++) i was getting the following error:

a.out: malloc.c:3096: sYSMALLOc: Assertion `(old_top == (((mbinptr) (((char *) &((av)->bins[((1) - 1) * 2])) - __builtin_offsetof (struct malloc_chunk, fd)))) && old_size == 0) || ((unsigned long) (old_size) >= (unsigned long)((((__builtin_offsetof (struct malloc_chunk, fd_nextsize))+((2 * (sizeof(size_t))) - 1)) & ~((2 * (sizeof(size_t))) - 1))) && ((old_top)->size & 0x1) && ((unsigned long)old_end & pagemask) == 0)' failed.
Aborted

and when i traced my program using cout's, I could find that, it is because of the following line

BNode* newNode=new BNode();

If i remove this line I was not getting the error.

Can any one please help in this regard...

Upvotes: 1

Views: 715

Answers (2)

AnT stands with Russia
AnT stands with Russia

Reputation: 320719

The error message means that the integrity of the program heap was violated. The heap was broken. The line you removed... maybe it was the culprit, maybe it was not to blame. Maybe the heap was damaged by some code before that (or even well before that) and the new that you removed simply revealed the problem, not caused it. There's no way to say from what you posted.

So, it is possible that you actually changed nothing by removing that line. The error could still be there, and the program will simply fail in some other place. Buffer overrun, double free or something like that is normally to blame for the invalidated heap. Run your code through some static or dynamic checker to look for these problems (valgrind, coverity etc.)

Upvotes: 1

Peter G.
Peter G.

Reputation: 15144

The shown line of code is ok in general. The heap probably was corrupted before. I would use a memory checker like valgrind to find out where.

Without a memory checking tool you just have to look hard at your code and find the error.

Sometimes a binary search strategy helps. Deliberately deactivate parts of your code and narrow down. Don't be fooled by false positives like the line you posted.

Another alternative is to switch to a programming language with automatic memory management.

Upvotes: 3

Related Questions