Alternatively, duplicate of Facing an error — glibc detected free invalid next size (fast).
I have been struggling with this, I tried using valgrind to track it down but can’t seem to pin down the exact source of the error. I can call the function 4 times, but after that it throws the realloc invalid next size error.
The exact error: * glibc detected * ./matrix: realloc(): invalid next size: 0x0000000001a46010 ***
Here’s the code:
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;
}
With each new matrix you “allocate”, you should be expanding your global pointer lists. you don’t. You just reallocate them to the same size the were before:
This:
Should be this:
Note: the
(nMatrix+1)value is used because you start withnMatrixas(-1), and upon first increment it is(0), the next is(1), etc… I.e. it always indexes the last row inserted, but your vector magnitudes needs to be +1 to that for hopefully obvious reasons.I would strongly suggest you consider what happens when
realloc()fails as well, as it will return NULL and in the process leak whatever memory was pointed to by the pointer you passed in.There may be other issues, but that was the first one that jumped out at me.