为什么我会遇到 C malloc 断言失败?
我正在实现一个分而治之的多项式算法,因此我可以针对 OpenCL 实现对其进行基准测试,但是我无法让 malloc
工作.当我运行程序时,它会分配一堆东西,检查一些东西,然后将 size/2
发送给算法.然后,当我再次点击 malloc
行时,它会吐出:
I am implementing a divide and conquer polynomial algorithm so I can benchmark it against an OpenCL implementation, but I can't get malloc
to work. When I run the program, it allocates a bunch of stuff, checks some things, then sends the size/2
to the algorithm. Then when I hit the malloc
line again it spits out this:
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
", size);
out = (int *)malloc(sizeof(int) * size * 2);
}
我用 fprintf
检查了大小,它是一个正整数(通常是 50).我也尝试使用普通数字调用 malloc
,但仍然出现错误.我只是被正在发生的事情难住了,到目前为止我从 Google 发现的任何东西都没有帮助.
I checked size with a fprintf
, and it is a positive integer (usually 50 at that point). I tried calling malloc
with a plain number as well and I still get the error. I'm just stumped at what's going on, and nothing from Google I have found so far is helpful.
有什么想法吗?我正试图弄清楚如何编译一个更新的 GCC,以防它是一个编译器错误,但我真的很怀疑.
Any ideas what's going on? I'm trying to figure out how to compile a newer GCC in case it's a compiler error, but I really doubt it.
99.9% 的可能性是您的内存已损坏(缓冲区溢出或下溢,在释放后写入指针,在同一个指针上调用两次 free指针等)
99.9% likely that you have corrupted memory (over- or under-flowed a buffer, wrote to a pointer after it was freed, called free twice on the same pointer, etc.)
在 Valgrind 下运行您的代码,以查看您的程序哪里出错了.
Run your code under Valgrind to see where your program did something incorrect.