处理一些使用矩阵的C代码。我已经创建了一个Matrix结构,以便更快更高效地创建它的许多实例,但我不确定为什么我的代码正在泄漏内存。目前我有以下代码:无效的写入/读取大小为8
typedef struct
{
size_t rows;
size_t cols;
double ** value;
}
*Matrix;
Matrix matCreate(size_t rows, size_t cols)
{
if(rows<=0 || cols<=0)
{
return NULL;
}
Matrix mat = (Matrix) malloc(sizeof(Matrix));
mat->rows = rows;
mat->cols = cols;
mat->value = (double **) malloc(rows*sizeof(double*));
for (int i=0; i< rows;i++)
{
mat->value[i] = (double *) malloc(cols * sizeof(double));
for(int j=0; j< cols;j++)
{
mat->value[i][j] = 0;
if(rows == cols && i==j)
{
mat->value[i][j] = 1;
}
}
}
return mat;
}
而且我运行valgrind后出现以下内存泄漏。运行代码时,它完全无错地编译,并仍然输出正确的输出。
==23609== Invalid write of size 8
==23609== at 0x400800: matCreate
==23609== by 0x4010E2: main
==23609== Address 0x5203048 is 0 bytes after a block of size 8 alloc'd
==23609== at 0x4C2DB8F: malloc
==23609== by 0x4007E8: matCreate
==23609== by 0x4010E2: main
==23609==
==23609== Invalid write of size 8
==23609== at 0x40081B: matCreate
==23609== by 0x4010E2: main
==23609== Address 0x5203050 is 8 bytes after a block of size 8 alloc'd
==23609== at 0x4C2DB8F:
==23609== by 0x4007E8: matCreate
==23609== by 0x4010E2: main
==23609==
==23609== Invalid read of size 8
==23609== at 0x40082F: matCreate
==23609== by 0x4010E2: main
==23609== Address 0x5203050 is 8 bytes after a block of size 8 alloc'd
==23609== at 0x4C2DB8F: malloc
==23609== by 0x4007E8: matCreate
==23609== by 0x4010E2: main
尝试使用'-g'进行编译,所以'valgrind'会向您显示访问发生的确切路线...... – BadZen
您的代码中没有矩阵(又名二维数组)。指针不是数组!如果你需要矩阵使用二维数组。一般不要将'malloc'和朋友或'void *'结果。 – Olaf
学习以下内存分配习语:'T p = malloc(sizeof * p)'或'T p = malloc(N * sizeof * p)',其中'T'是一些指针类型。即'sizeof'下没有类型名称,也没有投射'malloc'的结果。这样可以避免你在这样的失误中遇到困难。 – AnT