【发布时间】:2016-01-11 03:15:56
【问题描述】:
我正在做一个学校项目,我需要从文件中获取矩阵信息(高度、宽度、单元格状态)。像这样的:
30 40 /*height and width*/
3 /*nr of lines to read from the file*/
10 11 1 /*coordinates, and cell status (0,1,2)*/
10 12 1
10 13 2
由于某种原因,它一直在调试器中给我 SIGABRT 或损坏的双链表。我知道代码并不完美,但我开始修改它,看看是否能找到问题所在。
这是我的代码:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
int ***getFileLoad(char *fileName)
{
char strLines[5], strColumns[5], strCells[5], strTemp[3];
int i = 0, j = 0, k = 0, l = 0, lines = 0, columns = 0, cells = 0, intTemp = 0;
int ***array = NULL;
FILE *file = NULL;
file = fopen(fileName, "r"); /*opens file*/
if (file == NULL)
{
printf("Error opening file!\n");
exit(EXIT_FAILURE);
}
fscanf(file, "%s %s %s", &strLines, &strColumns, &strCells);
lines = atoi(strLines);
if (lines <= 0) /*lines*/
{
printf("Invalid value, lines!");
exit(EXIT_FAILURE);
}
columns = atoi(strColumns);
if (columns <= 0) /*columns*/
{
printf("Invalid value, columns!");
exit(EXIT_FAILURE);
}
cells = atoi(strCells);
if (cells <= 0) /*cells*/
{
printf("Invalid value, cells!");
exit(EXIT_FAILURE);
}
array = (int ***)malloc(sizeof(int **) * lines); /*allocating lines*/
if (array == NULL)
{
printf("No memory!");
exit(EXIT_FAILURE);
}
for (i = 0; i < columns; i++) /*allocating columns*/
{
array[i] = (int **)malloc(sizeof(int *) * columns);
if (array[i] == NULL)
{
printf("No memory!");
for (j = 0; j < i; j++)
{
free(array[j]);
}
free(array);
array = NULL;
exit(EXIT_FAILURE);
}
}
for (i = 0; i < lines; i++) /*allocating nr of cells*/
{
for (j = 0; j < columns; j++)
{
array[i][j] = (int *)malloc(sizeof(int) * cells);
if (array[i][j] == NULL)
{
printf("No memory!");
for (k = 0; k < i; k++)
{
for (l = 0; l < j; l++)
{
free(array[k][l]);
}
}
for (k = 0; k < i; k++)
{
free(array[k]);
}
free(array);
array = NULL;
exit(EXIT_FAILURE);
}
}
}
array[0][0][0] = lines;
array[0][0][1] = columns;
array[0][0][2] = cells;
for (i = 0; i < 1; i++) /*nr arrays*/
{
for (j = 1; j < cells + 1; j++) /*nr cells*/
{
for (k = 0; k < 4; k++) /*values from file*/
{
if (k == 3) /*getting to the next line*/
{
intTemp = fgetc(file);
if (intTemp == '\n' || intTemp == EOF)
{
continue;
}
else
{
while (intTemp != '\n' || intTemp != EOF)
{
intTemp = fgetc(file);
if (intTemp == '\n' || intTemp == EOF)
{
break;
}
}
}
}
else
{
fscanf(file, "%s", strTemp);
if (isdigit(strTemp))
{
intTemp = atoi(strTemp);
if (k == 0) /*accepting lines with values between1->lines*/
{
if (!(intTemp >= 1 && intTemp < lines))
{
printf("Invalid value!");
exit(EXIT_FAILURE);
}
else
{
array[i][j][k] = intTemp;
}
}
else if (k == 1) /*accepting columns with values between 1->columns*/
{
if (!(intTemp >= 1 && intTemp < columns))
{
printf("Invalid value!");
exit(EXIT_FAILURE);
}
else
{
array[i][j][k] = intTemp;
}
}
else if (k == 2) /*accepting cells with values between 0->2*/
{
if (!(intTemp >= 0 && intTemp < 3))
{
printf("Invalid value!");
exit(EXIT_FAILURE);
}
else
{
array[i][j][k] = intTemp;
}
}
}
}
}
}
}
intTemp = fgetc(file); /*checking for EOF*/
if (intTemp != EOF)
{
printf("Impossible reading every value!");
exit(EXIT_FAILURE);
}
fclose(file);
return array;
}
【问题讨论】:
-
请告诉我们更多关于您在调试器中发现的内容。例如,调试器应该告诉您在哪一行代码中检测到问题。
-
如果您一直在调试器中运行它(这很好),您必须知道 SIGABRT 发生在哪里。您的标题提到了“损坏的双链表”,但这里没有使用链表的代码。这可能意味着您对分配的内存处理不当,写入越界,并践踏
malloc()等人用来跟踪分配和释放内存的控制数据。 -
不要忘记以新行结束打印操作。最好在错误消息中包含您未能转换的值并将错误打印为标准错误:
fprintf(stderr, "Invalid value '%s' for number of cells!\n", strCells);。 -
这里的主要问题是你写了一个函数,而你应该写十个左右。
标签: c function multidimensional-array malloc