-1

或者,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;
}
4

1 回答 1

2

对于您“分配”的每个新矩阵,您应该扩展全局指针列表。你没有。您只需将它们重新分配到与以前相同的大小:

这个:

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)使用该值是因为您从nMatrixas开始(-1),并且在第一次递增时它是(0),下一个是(1)等等...即它总是索引插入的最后一行,但是您的向量幅度需要为 +1 以希望显而易见原因。

我强烈建议您考虑realloc()失败时会发生什么,因为它将返回 NULL 并且在进程中泄漏您传入的指针指向的任何内存

可能还有其他问题,但那是我第一个跳出来的问题。

于 2013-01-19T15:12:52.777 回答