矩阵行列式计算C++代码无限循环问题排查求助
矩阵行列式计算程序无限循环问题排查
我编写的矩阵行列式计算C++程序运行时出现无限循环问题,怀疑问题出在main.c函数中的指针数组初始化环节,但无法准确定位。以下是完整的项目代码文件:
main.c
#include <stdio.h> #include <string.h> #include "matrix.h" int main(){ //Prompt the user for the size of matrix to be calculated. printf("Welcome to the matrix determinant calculator!\n\n"); printf("Please select the matrix size you would like to input: \n"); printf("\t (A): 2x2 matrix\n"); printf("\t (B): 3x3 matrix\n\n"); char selection; //Stores matrix size selection scanf(" %c", &selection); int size; //Size of matrix //Uses selection from user to determine value to assign to 'size' if (selection == 'A' || selection == 'a'){ size = 2; } else if (selection == 'B' || selection == 'b'){ size = 3; } else{ printf("Your selection is invalid. Please start over.\n"); return 0; } printf("\nYou have selected a %dx%d matrix.\n\n", size, size); //Initialize pointer array int** matrix = (int**)malloc(size * sizeof(int*)); for (int i = 0; i < size; i++){ matrix[i] = (int*)malloc(size * sizeof(int)); } readMatrix(matrix, size); //Sets up matrix by taking input from user int calc = determinant(matrix, size); //Calculates determinant printf("The %dx%d matrix is: \n\n", size, size); //Displays the matrix on the console for (int row = 0; row < size; row++){ for (int col = 0; col < size; col++){ printf("%d\t", matrix[row][col]); } printf("\n"); } //Deletes stored data for (int i = 0; i < size; i++){ free(matrix[i]); } free(matrix); printf("\nThe determinant of the matrix is: %d\n", calc); return 0; }
determinant.c
#include <iostream> #include <string> #include "matrix.h" #include "determinant.h" using namespace std; int determinant(int** matrix, int size){ int detm_calc; //Determinant calculation variable //Determine which formula to use - 2x2 or 3x3 matrix. if (size == 2){ //2x2 case int a = matrix[0][0]; int b = matrix[0][1]; int c = matrix[1][0]; int d = matrix[1][1]; detm_calc = (a*d) - (b*c); } else{ //3x3 case int a = matrix[0][0]; int b = matrix[0][1]; int c = matrix[0][2]; int d = matrix[1][0]; int e = matrix[1][1]; int f = matrix[1][2]; int g = matrix[2][0]; int h = matrix[2][1]; int i = matrix[2][2]; detm_calc = a*(e*i - f*h) - b*(d*i - f*g) + c*(d*h - e*g); } return detm_calc; }
determinant.h
#ifndef DETERMINANT_H #define DETERMINANT_H #include <string> #include <iostream> #include "matrix.h" int determinant(int**, int); #endif
matrix.c
#include <stdio.h> #include <stdlib.h> #include "matrix.h" #include "determinant.h" void readMatrix(int** matrix, int size){ for (int i = 0; i < size; i++){ for (int j = 0; j < size; j++){ printf("Please enter the integer for row %d column %d:\t", i+1, j+1); scanf("%d", &matrix[i][j]); } printf("\n"); } }
matrix.h
#ifndef MATRIX_H #define MATRIX_H #include <string> #include <iostream> #include "determinant.h" void readMatrix(int**, int); #endif
Makefile
determinant: main.o determinant.o matrix.o g++ main.o determinant.o matrix.o -o determinant.out main.o: main.c g++ -c main.c determinant.o: determinant.c determinant.h g++ -c determinant.c matrix.o: matrix.c matrix.h g++ -c matrix.c
问题排查与解决方案
1. 头文件循环引用导致编译阶段无限循环
代码中存在头文件循环引用问题:matrix.h包含determinant.h,而determinant.h又包含matrix.h。预处理器处理头文件时会陷入无限递归,表现为编译过程卡住,易被误认为是运行时无限循环。
修复方案:移除不必要的头文件引用,仅保留函数声明所需依赖:
- 修改
matrix.h,删除冗余引用:
#ifndef MATRIX_H #define MATRIX_H // 仅声明函数,无需额外依赖 void readMatrix(int**, int); #endif
- 修改
determinant.h,删除冗余引用:
#ifndef DETERMINANT_H #define DETERMINANT_H // 仅声明函数,无需额外依赖 int determinant(int**, int); #endif
- 各源文件按需保留标准头引用,比如
determinant.c保留<iostream>,matrix.c保留<stdio.h>和<stdlib.h>。
2. 输入非整数导致运行时无限循环
若用户输入矩阵元素时输入非整数(如字符、浮点数),scanf("%d", ...)会执行失败,返回0且无效字符留在输入缓冲区。后续循环中scanf反复读取无效字符,导致readMatrix函数无限执行。
修复方案:处理scanf返回值并清除无效输入:
修改matrix.c中的readMatrix函数:
void readMatrix(int** matrix, int size){ for (int i = 0; i < size; i++){ for (int j = 0; j < size; j++){ printf("Please enter the integer for row %d column %d:\t", i+1, j+1); // 检查scanf是否成功读取整数 while (scanf("%d", &matrix[i][j]) != 1) { // 清除输入缓冲区的无效字符 while (getchar() != '\n'); printf("Invalid input! Please enter an integer: "); } } printf("\n"); } }
3. 指针数组初始化的潜在问题
指针数组初始化代码无语法错误,但缺少malloc失败检查。若内存分配失败,matrix或matrix[i]会为NULL,后续访问会引发未定义行为,可能表现为异常循环或崩溃。
优化方案:添加内存分配检查:
//Initialize pointer array int** matrix = (int**)malloc(size * sizeof(int*)); if (matrix == NULL) { printf("Memory allocation failed!\n"); return 1; } for (int i = 0; i < size; i++){ matrix[i] = (int*)malloc(size * sizeof(int)); if (matrix[i] == NULL) { printf("Memory allocation failed!\n"); // 释放已分配的内存 for (int k = 0; k < i; k++) { free(matrix[k]); } free(matrix); return 1; } }
内容的提问来源于stack exchange,提问作者Enoch Shen
相关产品推荐
相关产品推荐

