或者,重复Facing an error — glibc detected free invalid next size (fast)。
我一直在努力解决这个问题,我尝试使用valgrind跟踪它,但似乎无法确定错误的确切来源。我可以调用该函数4次,但之后会抛出realloc无效的下一个大小错误。
确切错误: *检测到glibc * ./matrix:realloc():下一个大小无效:0x0000000001a46010 ***
以下是代码:
char line[101];
int nMatrix = -1;
Dims *dimensions;
List *vals = NULL;
int **values;
int **columns;
int **rowPointer;
int *lineCount;
int *highestRow;
void newMatrix()
{
nMatrix++;
values = realloc(values, sizeof(int*));
columns = realloc(columns, sizeof(int*));
rowPointer = realloc(rowPointer, sizeof(int*));
dimensions = realloc(dimensions, sizeof(Dims));
lineCount = realloc(lineCount, sizeof(int));
highestRow = realloc(highestRow, sizeof(int));
}
void readIn(char* inputFile, int transpose)
{
FILE *fr;
int a = 0;
int b = 0;
int c = 0;
newMatrix();
if((fr = fopen(inputFile, "r")) != NULL)
{
while(fgets(line, 100, fr) != NULL)
{
if(lineCount[nMatrix] == 0)
{
if(transpose)
sscanf(line, "%d,%d", &dimensions[nMatrix].n, &dimensions[nMatrix].m);
else
sscanf(line, "%d,%d", &dimensions[nMatrix].m, &dimensions[nMatrix].n);
printf("nMatrix = %d, n%d,m%d\n", nMatrix, dimensions[nMatrix].n, dimensions[nMatrix].m);
}
else
{
sscanf(line, "%d,%d,%d", &a,&b,&c);
//printf("a = %d, b = %d, c = %d\n", a,b,c);
//rows[a] = insertList(c,b,rows[a]);c
if(transpose)
vals = insertList(c, a, b, dimensions[nMatrix].m, dimensions[nMatrix].n, vals);
else
vals = insertList(c, b, a, dimensions[nMatrix].m, dimensions[nMatrix].n, vals);
}
lineCount[nMatrix]++;
}
values[nMatrix] = calloc(lineCount[nMatrix], sizeof(int));
columns[nMatrix] = calloc(lineCount[nMatrix], sizeof(int));
rowPointer[nMatrix] = calloc(((dimensions[nMatrix].m)+1), sizeof(int));
values[nMatrix][lineCount[nMatrix]] = 0;
columns[nMatrix][lineCount[nMatrix]] = 0;
int i = 0;
int lastRow = -1;
while(i < dimensions[nMatrix].m)
{
rowPointer[nMatrix][i] = -1;
i++;
}
i = 0;
List *temp = NULL;
while(vals != NULL)
{
temp = vals;
//printf("pos = %d, row = %d, col = %d, val = %d, i=%d", temp->position, temp->row, temp->column, temp->value, i);
if(lastRow != temp->row)
{
rowPointer[nMatrix][temp->row] = i;
lastRow = temp->row;
highestRow[nMatrix] = i;
}
values[nMatrix][i] = temp->value;
columns[nMatrix][i] = temp->column;
i++;
vals = temp->next;
free(temp);
}
rowPointer[nMatrix][dimensions[nMatrix].m] = lineCount[nMatrix]-1;
fclose(fr);
return;
}
fclose(fr);
printf("File not found\n");
return;
}
答案 0 :(得分:1)
对于您“分配”的每个新矩阵,您应该扩展您的全局指针列表。你没有。您只需将它们重新分配到以前的大小:
此:
void newMatrix()
{
nMatrix++;
values = realloc(values, sizeof(int*));
columns = realloc(columns, sizeof(int*));
rowPointer = realloc(rowPointer, sizeof(int*));
dimensions = realloc(dimensions, sizeof(Dims));
lineCount = realloc(lineCount, sizeof(int));
highestRow = realloc(highestRow, sizeof(int));
}
应该是这样的:
void newMatrix()
{
nMatrix++;
values = realloc(values, (nMatrix+1)*sizeof(int*));
columns = realloc(columns, (nMatrix+1)*sizeof(int*));
rowPointer = realloc(rowPointer, (nMatrix+1)*sizeof(int*));
dimensions = realloc(dimensions, (nMatrix+1)*sizeof(Dims));
lineCount = realloc(lineCount, (nMatrix+1)*sizeof(int));
highestRow = realloc(highestRow, (nMatrix+1)*sizeof(int));
}
注意:使用(nMatrix+1)
值是因为您以nMatrix
作为(-1)
开头,第一次递增时为(0)
,下一个是(1)
等等......也就是说它总是为插入的最后一行编制索引,但是你的矢量幅度需要为+1,这是出于明显的原因。
我强烈建议你考虑当realloc()
失败时会发生什么,因为它将返回NULL并且在进程中泄漏传入的指针所指向的 的内存。
可能还有其他问题,但这是第一个跳出来的问题。