Malloc和Calloc未正确释放

时间:2018-03-29 21:01:28

标签: c matrix memory-management malloc calloc

您好我在创建它们后释放了一些变量似乎存在问题。

我的计划背景很少。我的程序正在读取MatrixMarket文件并根据读入的信息构建矩阵结构。然后我使用矩阵进行标量和向量乘法。

这是我的矩阵结构。

typedef struct {
    double *values;       // Value of each non-zero element.
    unsigned int *col_indices; // Corresponding column coordinate.
    unsigned int *row_ptr; // Initial position of each row.
    unsigned int nz;  // Total non-zero elements.
    unsigned int row_count; // Total row_count of matrix.
    unsigned int cols_count; // Total columns of matrix.
} CSRMatrix;

我的目标是能够操纵我的矩阵并能够将矢量乘以CSR矩阵。 (CSR是压缩稀疏行矩阵)。

以下是我初始化内存分配的方法。

    /**
    * Set the correct values to the struct and create the memory allocation.
    */
    csrMatrix.row_count = numberOfRows;
    csrMatrix.cols_count = numberOfColumn;
    csrMatrix.nz = numberOfNonZeros;
    csrMatrix.row_ptr = calloc(numberOfRows, 1);
    csrMatrix.values = (double *)malloc(numberOfRows * numberOfRows * 1);
    csrMatrix.col_indices = calloc(numberOfRows * numberOfRows, 1);

这是我认为我的问题所在,我可能错了。我在排除内存问题时非常糟糕。

在我进一步深入讨论矩阵的主要乘法和迭代之前:

// Initialize vectors for multiplication

int x_size = numberOfRows * numberOfColumn;
int csr_x[x_size];
memset(csr_x, 0, sizeof(csr_x));
for(int h = 0; h < x_size; h++){
    csr_x[h] = 1;
}
double* csr_y;

printf("\nCalculating matrix-vector product of the CSR for %u iterations...\n", iterations);
clock_t begin = clock();
for(i=0; i < iterations; i++){
    csr_y = (double *)malloc(csrMatrix.row_count * csrMatrix.cols_count * 1);
    csrMVP(&csrMatrix, csr_x,csr_y);
    free(csr_y);
}
clock_t end = clock();
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("for loop used %f seconds to complete the execution\n", time_spent);
fclose(file);
free(csr_x);
free(csr_y);
exit(EXIT_SUCCESS);

当我运行我的程序时,奇怪的事情正在发生。程序将运行几个循环(总循环为1000次迭代),然后使用Process finished with exit code 0exit code 11退出不完整。这是我的故障排除开始的地方。

由于intellij没有抛出任何内存问题或错误,我必须更深入。

我决定问我的一位朋友谁曾使用Valgrind对我的节目进行记忆。这就是结果:

==5607== Memcheck, a memory error detector
==5607== Copyright (C) 2002-2017, and GNU GPL'd, by Julian Seward et al.
==5607== Using Valgrind-3.13.0 and LibVEX; rerun with -h for copyright info
==5607== Command: ./code
/home/tron/src/workspace/classes/cda5110/matrix/mat/ibm32/ibm32.mtx
==5607==
==5607== Invalid write of size 4
==5607==    at 0x109537: loadCSRMatrix (mainCSR.c:173)
==5607==    by 0x108F3A: main (mainCSR.c:72)
==5607==  Address 0x51f23b0 is 0 bytes after a block of size 128 alloc'd
==5607==    at 0x4C2EEF5: calloc (vg_replace_malloc.c:711)
==5607==    by 0x108EC5: main (mainCSR.c:64)
==5607==
==5607== Invalid read of size 4
==5607==    at 0x109300: csrMVP (mainCSR.c:140)
==5607==    by 0x109085: main (mainCSR.c:92)
==5607==  Address 0x51f23b0 is 0 bytes after a block of size 128 alloc'd
==5607==    at 0x4C2EEF5: calloc (vg_replace_malloc.c:711)
==5607==    by 0x108EC5: main (mainCSR.c:64)
==5607==
==5607== Invalid free() / delete / delete[] / realloc()
==5607==    at 0x4C2E10B: free (vg_replace_malloc.c:530)
==5607==    by 0x10910C: main (mainCSR.c:99)
==5607==  Address 0x1ffefff5a0 is on thread 1's stack
==5607==  in frame #1, created by main (mainCSR.c:24)
==5607==
==5607== Invalid free() / delete / delete[] / realloc()
==5607==    at 0x4C2E10B: free (vg_replace_malloc.c:530)
==5607==    by 0x10911B: main (mainCSR.c:100)
==5607==  Address 0x59d3bc0 is 0 bytes inside a block of size 8,192 free'd
==5607==    at 0x4C2E10B: free (vg_replace_malloc.c:530)
==5607==    by 0x109094: main (mainCSR.c:93)
==5607==  Block was alloc'd at
==5607==    at 0x4C2CEDF: malloc (vg_replace_malloc.c:299)
==5607==    by 0x109064: main (mainCSR.c:91)
==5607==
==5607==
==5607== HEAP SUMMARY:
==5607==     in use at exit: 12,416 bytes in 3 blocks
==5607==   total heap usage: 1,006 allocs, 1,005 frees, 8,213,160 bytes
allocated
==5607==
==5607== LEAK SUMMARY:
==5607==    definitely lost: 0 bytes in 0 blocks
==5607==    indirectly lost: 0 bytes in 0 blocks
==5607==      possibly lost: 0 bytes in 0 blocks
==5607==    still reachable: 12,416 bytes in 3 blocks
==5607==         suppressed: 0 bytes in 0 blocks
==5607== Rerun with --leak-check=full to see details of leaked memory
==5607==
==5607== For counts of detected and suppressed errors, rerun with: -v
==5607== ERROR SUMMARY: 1003 errors from 4 contexts (suppressed: 0 from 0)

我突然发现了一些东西。有一些invaild写入和读取大小。这些是由错误调出的每个代码的以下行:

  • at 0x109537:loadCSRMatrix(mainCSR.c:173):m->row_ptr[row_index + 1] = pairIndex;
  • by 0x108F3A:main(mainCSR.c:72):loadCSRMatrix(&csrMatrix, file);
  • by 0x108EC5:main(mainCSR.c:64):csrMatrix.row_ptr = calloc(numberOfRows, 1);
  • at 0x109300:csrMVP(mainCSR.c:140):y[i] += val[j] * x[col[j]];
  • by 0x109085:main(mainCSR.c:92):free(csr_y);
  • by 0x108EC5:main(mainCSR.c:64):csrMatrix.row_ptr = calloc(numberOfRows, 1);
  • by 0x10910C:main(mainCSR.c:99):free(csr_y);
  • by 0x10911B:main(mainCSR.c:100):exit(EXIT_SUCCESS);
  • by 0x109094:main(mainCSR.c:93):}
  • by 0x109064:main(mainCSR.c:91):csrMVP(&csrMatrix, csr_x,csr_y);

    从我提到的内容是我没有正确地使用malloc和calloc进行内存分配。

所以我在这里问我的问题,我不确定malloc或calloc是否正确使用。如果有人知道我可以在哪里开始寻找解决这个问题。

如果您想知道loadCSRMatrixcsrMVP功能是什么,我会在下面附上。

csrMVP

/**
 * Stores on vector y the product of matrix M and vector x:
 *   y = Mx
 */
void csrMVP(CSRMatrix *m, int x[], double *y){
    printf("\n\n\nValue of Y: \n ");

    unsigned int i,j,end;
    double *val = m->values;
    unsigned int *col = m->col_indices;
    unsigned int *ptr = m->row_ptr;
    end = 0;
    for(i = 0; i < m->row_count; i++){
        y[i] = 0.0;
        j = end;
        end = ptr[i+1];
        for( ; j < end; j++){
            y[i] += val[j] * x[col[j]];
            printf("%.f\t",  y[i]);
        }
    }printf("\n");

}

loadCSRMatrix

/**
 * Loads the matrix
 */
void loadCSRMatrix(CSRMatrix *m, FILE *f){
    unsigned int pairIndex = 0;
    for (int row_index = 0; row_index < m->row_count; row_index++) {
        for (unsigned int column_index = 0; column_index < m->row_count; column_index++) {
            double value;
            if(fscanf(f, "%lg", &value) != EOF){
                m->values[pairIndex] = value;
                m->col_indices[pairIndex] = column_index+1;
                //printf("Column: %d\t", matrixCSR.col_indices[pairIndex]);
                printf("%.f\t", m->values[pairIndex]);
                pairIndex++;
            }
            else {
                m->values[pairIndex] = 0;
                m->col_indices[pairIndex] = column_index+1;
                //printf("Column: %d\t", matrixCSR.col_indices[pairIndex]);
                printf("%.1f\t", m->values[pairIndex]);
                pairIndex++;
            }
        }
        if(pairIndex != 0) {
            m->row_ptr[row_index + 1] = pairIndex;
        }
        //printf("%zu\n", matrixCSR.row_indices[row_index]);
        printf("\n");
    }
}

0 个答案:

没有答案