我正在实现分治多项式算法,因此我可以根据 OpenCL 实现对其进行基准测试,但我无法让 malloc
工作。当我运行程序时,它会分配一堆东西,检查一些东西,然后将 size/2
发送给算法。然后当我再次点击 malloc
行时,它会吐出这个:
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
有问题的行是:
int *mult(int size, int *a, int *b) {
int *out,i, j, *tmp1, *tmp2, *tmp3, *tmpa1, *tmpa2, *tmpb1, *tmpb2,d, *res1, *res2;
fprintf(stdout, "size: %d\n", size);
out = (int *)malloc(sizeof(int) * size * 2);
}
我用 fprintf
检查了大小,它是一个正整数(此时通常为 50)。我也尝试使用普通数字调用 malloc
,但仍然出现错误。我只是对正在发生的事情感到困惑,到目前为止,我从 Google 中发现的任何东西都没有帮助。
有什么想法吗?我正在尝试找出如何编译更新的 GCC,以防出现编译器错误,但我真的对此表示怀疑。
最佳答案
99.9% 的可能性是您损坏了内存(溢出或不足缓冲区,在释放后写入指针,在同一个指针上调用 free 两次,等等)
在 Valgrind 下运行您的代码查看您的程序在哪里做错了。
关于c - 为什么会出现 C malloc 断言失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2987207/