或者,Facing an error — glibc detected free invalid next size (fast) 的副本。
我一直在为此苦苦挣扎,我尝试使用 valgrind 来追踪它,但似乎无法确定错误的确切来源。我可以调用该函数 4 次,但之后它会抛出 realloc invalid next size 错误。
确切的错误:* 检测到 glibc * ./matrix: realloc(): invalid next size: 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;
}