英文:
Segmentation Fault Issue in Matrix GDB Project
问题
我在为课程做一个GDB项目,我在一段读取文件并创建输入矩阵的代码中遇到了段错误的问题。GDB将我带到了printMatrix()中的"printf("%.2d\t", mat[row][col]);",但我似乎无法理解出错的具体原因。
int main(int argc, char* argv[])
{
FILE* fp = fopen(argv[1], "r");
int size = 0;
int **mat = readFile(fp, &size);
printMatrix(mat, size);
return 0;
}
int** readFile(FILE* fp, int *size)
{
fscanf(fp, "%d", size);
int num = *size;
int index = 0;
int** mat = (int**)malloc(num * sizeof(int*));
for(index = 0; index < num; index++)
mat[index] = (int*)malloc(num * sizeof(int));
int row = 0;
int col = 0;
for(; row < num; row++)
{
for(; col < num; col++)
{
fscanf(fp, "%d", &mat[row][col]);
}
}
return mat;
}
void printMatrix (int** mat, int num)
{
int row = 0;
int col = 0;
for(row = 0; row < num; row++)
{
for(col = 0; col < num; col++)
{
printf("%.2d\t", mat[row][col]); /* gdb在这里指示段错误 */
}
printf("\n");
}
}
我尝试调整for循环,但似乎无法获得期望的输出。
英文:
I'm working on a GDB project for class an I'm finding issues with a segfault portion in a piece of code that reads a file and creates a matrix with the input. GDB brings me to the "printf("%.2d\t", mat[row][col]);" in printMatrix() but I can't seem to grasp what exactly goes wrong.
int main(int argc, char* argv[])
{
FILE* fp = fopen(argv[1], "r");
int size = 0;
int **mat = readFile(fp, &size);
printMatrix(mat, size);
return 0;
}
int** readFile(FILE* fp, int *size)
{
fscanf(fp, "%d", size);
int num = *size;
int index = 0;
int** mat = (int**)malloc(num * sizeof(int));
for(index = 0; index < num; index++)
mat[index] = (int*)malloc(num * sizeof(int));
int row = 0;
int col = 0;
for(; row < num; row++)
{
for(; col < num; col++)
{
fscanf(fp, "%d", &mat[row][col]);
}
}
return mat;
}
void printMatrix (int** mat, int num)
{
int row = 0;
int col = 0;
for(row = 0; row < num; row++)
{
for(col = 0; col < num; col++)
{
printf("%.2d\t", mat[row][col]); /* gdb indicates segfault here */
}
printf("\n");
}
}
I tried tinkering with the for loop but can't seem to get the expected output.
答案1
得分: 0
int** mat = malloc(num * sizeof(int*));
或者更好的写法是
int** mat = malloc(num * sizeof(*mat));
同时,对于大小,请使用 size_t
而不是 int
。
个人建议使用指向数组的指针,而不是指针数组,以消除一级间接性(难以进行分配/释放,性能损失)。
始终检查 malloc
和 scanf
的结果。
void *readFile(FILE* fp, size_t *size)
{
if(fscanf(fp, "%zu", size) != 1) { /* 处理错误 */}
int (*mat)[*size] = malloc(*size * sizeof(*mat));
if(mat)
{
for(size_t row = 0; row < *size; row++)
{
for(size_t col = 0; col < *size; col++)
{
if(fscanf(fp, "%d", &mat[row][col]) != 1) { /* 处理错误 */}
}
}
}
return mat;
}
void printMatrix (size_t num, int (*mat)[num])
{
if(mat)
{
for(size_t row = 0; row < num; row++)
{
for(size_t col = 0; col < num; col++)
{
printf("%.2d\t", mat[row][col]); /* gdb 在这里指示段错误 */
}
printf("\n");
}
}
}
英文:
int** mat = (int**)malloc(num * sizeof(int));
It should be
int** mat = malloc(num * sizeof(int *));
or better
int** mat = malloc(num * sizeof(*mat));
Also use site_t
for sizes not int
.
I personally would use pointer to array not array of pointers to remove one level of indirection (difficult (de)/allocation , performance penalty)
Always check the result of malloc
and scanf
void *readFile(FILE* fp, size_t *size)
{
if(fscanf(fp, "%d", size) != 1) { /* handle error */}
int (*mat)[*size] = malloc(*size * sizeof(*mat));
if(mat)
{
for(size_t row = 0; row < *size; row++)
{
for(size_t col = 0; col < *size; col++)
{
if(fscanf(fp, "%d", &mat[row][col]) != 1) { /* handle error */}
}
}
}
return mat;
}
void printMatrix (size_t num, int (*mat)[num])
{
if(mat)
{
for(size_t row = 0; row < num; row++)
{
for(size_t col = 0; col < num; col++)
{
printf("%.2d\t", mat[row][col]); /* gdb indicates segfault here */
}
printf("\n");
}
}
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论