c - 为什么会出现 C malloc 断言失败?

标签 c gcc malloc assertion

我正在实现分治多项式算法,因此我可以根据 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/

相关文章:

c - C语言中如何获取指向指针的指针?

ios - 编译 PJSIP 以在 iOS 5 中使用

c - 为什么或何时需要在 C 中动态分配内存?

c - 在 C 中,追加到打开的文件以供 Windows 控制台和 Linux 上的另一个程序读取

c - doxygen 中是否可以提取函数注释并以 markdown 形式显示?

linux - 定位 typesizes.h

使用 malloc 替换字符串中的子字符串的 C 函数——无字符串函数

c - 内置函数的隐式声明不兼容 ‘malloc’

编译错误 "Incomplete definition of type ' struct proc'”

c - MinGW gcc 设置 fp 舍入模式