6

我遇到了一个神秘的错误,我不知道为什么。此代码在失败之前运行了多次,并且总是在同一点失败。

这是我的代码:

    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)运行多次。

有谁知道我做错了什么?

谢谢。

4

1 回答 1

7

这通常发生在您的代码free在之前某个时间使用无效地址,或者写入超过分配的内存块的末尾时。通过 valgrind 或其他一些内存分析器运行您的代码,以查看发生这种情况的点。

于 2013-10-20T11:06:00.037 回答