开发者

malloc error in c++

开发者 https://www.devze.com 2023-01-19 06:44 出处:网络
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]))

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.

Ca开发者_JAVA百科n any one please help in this regard...


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.


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.)

0

精彩评论

暂无评论...
验证码 换一张
取 消