我遇到了一个神秘的错误,我不知道为什么。这段代码在失败之前运行了几次,并且总是在同一点失败。
这是我的代码:
assert(size > 0);
int* sorted = malloc(size * sizeof(int));
这是我运行时遇到的错误:
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.
我试过打印尺寸,在本例中为 1。在失败之前,此代码使用不同的大小值(包括 1)运行了几次。
有谁知道我做错了什么吗?
谢谢。
最佳答案
这通常发生在您的代码在之前某个时间free
d 了一个无效地址,或者写入了已分配内存块的末尾之后。通过 valgrind 或其他一些内存分析器运行您的代码,以查看发生这种情况的时间点。
关于c - 神秘的 malloc : sysmalloc: Assertion failed error,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19476627/