神秘的malloc:sysmalloc:断言失败错误

Ian*_*Ian 5 c malloc assert

我收到一个神秘的错误,我不明白为什么.此代码在失败之前运行多次,并且始终在同一点失败.

这是我的代码:

    assert(size > 0);
    int* sorted = malloc(size * sizeof(int));
Run Code Online (Sandbox Code Playgroud)

这是我运行时遇到的错误:

    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.
Run Code Online (Sandbox Code Playgroud)

我已经尝试打印出尺寸,在这种情况下它是1.在失败之前,此代码会以不同的大小值(包括1)运行多次.

有谁知道我做错了什么?

谢谢.

das*_*ght 6

这通常发生在您的代码free在某个先前时间处于无效地址或写入已分配内存块的末尾之后.通过valgrind或其他内存分析器运行代码以查看发生这种情况的位置.