gcc显示0错误,但valgrind显示内存泄漏,我无法执行我的程序,因为分段错误(核心转储)任何建议? 我单独检查分配,它的精细valgrind显示0内存泄漏。文件操作中的某个地方有问题,但我找不到。
#include <stdio.h>
#include <stdlib.h>
int* arrsize(FILE *plik)
{
if (plik == NULL) {
printf("Error: file pointer is null.");
return 0;
}
int* size;
fscanf(plik, "%d", &size);
return size;
}
int main()
{
int i = 0, j = 0, k = 0, a = 0;
FILE *fp;
if ((fp = fopen("matrix.txt", "r")) == NULL)
{
printf("Error: file pointer is null.\n");
return 1;
}
int size = arrsize(fp);
printf("Array size is %d x %d ", size, size);
double **arr = (double **)malloc(size * sizeof(double *));
for (i; i < size; i++)
arr[i] = (double *)malloc(size * sizeof(double));
for (j; j < size; j++) {
fscanf(fp, "%d", &arr[i][j]);
}
for (i; i < size; i++)
{
for (j = 0; j < size; j++)
{
printf("%lf\n", &arr[i][j]);
}
}
for (k; k < size; k++) {
free(arr[k]);
}
free(arr);
fclose(fp);
return 0;
}
答案 0 :(得分:2)
将arrsize更改为实际返回int
,而不是int*
:
int arrsize(FILE *plik)
{
if (plik == NULL) {
printf("Error: file pointer is null.");
return 0;
}
int size;
fscanf(plik, "%d", &size);
return size;
}