【发布时间】:2017-02-13 18:47:46
【问题描述】:
我正在尝试为函数中的结构指针数组动态分配内存。它可以工作到 3 次迭代,但在出现此错误后崩溃:
double free or corruption (fasttop): ...
这是我的结构指针数组声明:
Intersection** alreadyUse = malloc(sizeof(Intersection*));
if(alreadyUse == NULL) {
exit(1);
}
int size = 1;
alreadyUse[0] = inter; // Pointer of an Intersection
// Some Code
checkFunction(alreadyUse, &size, interLeft);
这是我的功能
bool checkFunction(Intersection** alreadyUse, int* size, Intersection* inter) {
for(int i = 0; i < *size; i++) {
if(alreadyUse[i] == inter) {
return true;
}
}
*size = *size +1;
Intersection** tmp = realloc(alreadyUse, sizeof(Intersection*) * *size);
if(tmp == NULL){
exit(1);
}
else {
alreadyUse = tmp;
}
alreadyUse[*size-1] = inter;
return false;
}
正如我所说,它适用于 1、2、3,然后我得到错误。
是否有人知道它为什么会起作用然后突然崩溃?
感谢您的帮助。
【问题讨论】:
-
我们不知道 Intersection 类型
标签: c pointers malloc realloc memory-corruption