C - 矩阵realloc和uninicialized值

时间:2013-11-21 00:22:14

标签: c dynamic initialization valgrind realloc

我有根据输入动态分配的矩阵。一行输入是矩阵中的一行。分配以EOF结束。问题是,如果我输入多行,valgrind就会显示出来。 我真的不知道出了什么问题。

==6142== Conditional jump or move depends on uninitialised value(s)
==6142==    at 0x402C5F9: realloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==6142==    by 0x80488F4: reallocMe (in /home/jan/NetBeansProjects/PA1/pokus)
==6142==    by 0x80493CC: readMatrix (in /home/jan/NetBeansProjects/PA1/pokus)
==6142==    by 0x804962A: main (in /home/jan/NetBeansProjects/PA1/pokus)
==6142==  Uninitialised value was created by a heap allocation
==6142==    at 0x402C63E: realloc (in /usr/lib/valgrind/vgpreload_memcheck-x86-linux.so)
==6142==    by 0x80488A4: reallocMe (in /home/jan/NetBeansProjects/PA1/pokus)
==6142==    by 0x80493CC: readMatrix (in /home/jan/NetBeansProjects/PA1/pokus)
==6142==    by 0x804962A: main (in /home/jan/NetBeansProjects/PA1/pokus)

有reallocMe()函数

int ** reallocMe(int ** resultsMatrix, int rows, int cols) {
    int i;

    resultsMatrix = (int **) realloc(resultsMatrix, sizeof(*resultsMatrix)*rows);
    if(!resultsMatrix)
        return NULL;

    for(i = 0; i < rows; i++) {
        resultsMatrix[i] = (int *) realloc(resultsMatrix[i], sizeof(*resultsMatrix[i])*cols);
        if(!resultsMatrix[i]) {
            freeMatrix(resultsMatrix, i);
            return NULL;
        }

    }

    return resultsMatrix;
}

还有readMatrix()函数,我调用reallocMe()

int ** readMatrix(int ** matrix, int * rows, int * cols, int * matrixSum) {
    int number;
    char delimiter;
    int numbersCount = 0;
    int delimitersCount = 0;
    int breaker = 0;
    int actualCol = 0;
    int ** test;

    printf("Enter the matrix:\n");

    while (1) {
        if (scanf("%d", &number) != 1) break;
        numbersCount++;

        if(number <= 0) breaker = 1;

        if (*rows == 1)
            *cols = *cols +1;

        /* Memory allocation */ 
        test = reallocMe(matrix, *rows, *cols);
        if(!test) {
            freeMatrix(matrix, *rows);
            return NULL;
        }

        matrix = test;

        matrix[*rows-1][actualCol] = number;
        *matrixSum += number;
        actualCol++;

        if (scanf("%c", &delimiter) == 1 && delimiter == ',') {
            delimitersCount++; 

        } else if(iscntrl(delimiter)) {

            if ((actualCol > *cols) && (*rows > 1)) { /* if the count of columns is not same on every row, then break */
                freeMatrix(matrix, *rows);
                return NULL;
            }            

            if((*rows * *cols)-*rows != delimitersCount) {
                freeMatrix(matrix, *rows);
                return NULL;
            }

            actualCol = 0;
            *rows = *rows + 1;        
        } else {
            freeMatrix(matrix, *rows);
            return NULL;
        }
    }


    *rows = *rows - 1;

    if (!feof(stdin)) {
        freeMatrix(matrix, *rows);
        return NULL;
    }
    if(*rows * *cols != numbersCount) {
        freeMatrix(matrix, *rows);
        return NULL;
    }

    if(breaker == 1) {
        freeMatrix(matrix, *rows);
        return NULL;
    }

    if(*rows * *cols == 1) {
        freeMatrix(matrix, *rows);
        return NULL;    
    }

    return matrix;
}

我不知道如何解决这个问题。任何人都可以给我一些建议,提示,不管怎样?谢谢:))

2 个答案:

答案 0 :(得分:0)

resultsMatrix[i]未在行中初始化:

resultsMatrix[i] = (int *) realloc(resultsMatrix[i], sizeof(*resultsMatrix[i])*cols);

所以你不应该在上面调用realloc。

考虑循环resultMatrix并在此循环之前初始化其所有单元格。

答案 1 :(得分:0)

realloc不会将新内存归零。因此,为每行分配内存的循环对于新行是不正确的。

要解决此问题,您应该在循环之前将所有指针归零到新(添加)的行,或者仅针对旧行运行realloc循环,并使用malloc作为新行。< / p>