malloc error C++ [duplicate]

2019-07-24 03:43发布

Possible Duplicate:
malloc.c:3074 error?

I am getting this strange error on execution of my C++ code:

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

The program runs fine upto a point where it catches the above Segmentation fault(SIGSEGV) from an already executed line. I found out this using gdb.

标签: c++ malloc
2条回答
淡お忘
2楼-- · 2019-07-24 03:52

Something has corrupted the heap by writing to an invalid memory location. The most likely causes are writing outside the bounds of an allocated object, or writing to an object after it has been deleted.

These errors can be difficult to track down with a debugger. The best tool is a memory checker, such as valgrind.

查看更多
何必那么认真
3楼-- · 2019-07-24 04:12

Based on your statement that the breaking line is called previously in program's execution and runs without fail to a point: This error is prabably caused by "damaged" memory structures.

This kind of strange and inconsistent behavior can be expected if you were to allocate memory and overrun writing to the buffer or if you were to allocate memory and then use the returned address without checking it to ensure that it is a non-zero address (memory allocation failure).

查看更多
登录 后发表回答