Mysterious malloc: sysmalloc: Assertion failed error

筅森魡賤 提交于 2019-12-06 02:29:50

问题


I am getting a mysterious error and I have no idea why. This code runs several times before failing, and it always fails at the same point.

Here is my code:

    assert(size > 0);
    int* sorted = malloc(size * sizeof(int));

And here is the error I am getting when I run it:

    malloc.c:2369: 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.

I have tried printing out size and it is 1 in this case. This code runs several times with varying values of size, including 1, before failing.

Does anyone have any idea what I am doing wrong?

Thanks.


回答1:


This usually happens when your code freed an invalid address at some prior time, or wrote past the end of the allocated memory block. Run your code through valgrind or some other memory profiler to see the point where this happens.



来源:https://stackoverflow.com/questions/19476627/mysterious-malloc-sysmalloc-assertion-failed-error

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!