运行以下代码时出现分段错误:
const int SENSORS = 65;
static float coefficient[SENSORS][6];
const int NUMSUBSYSTEM = 6;
const int ALLSENSORS = SENSORS * NUMSUBSYSTEM;
using namespace std;
int row = 0;
static int outputError = -1; //static to retain value
ifstream equationFile("equation.txt");
static string sensorNameEquation[ALLSENSORS];
static float coefficientOverride[ALLSENSORS][6]; //static to keep large array off stack
static string dependantSensor[ALLSENSORS]; //static to keep large array off stack
static float baseTemp[ALLSENSORS]; //static to keep large array off stack
printf("Total sensors: %d\n", ALLSENSORS);
row = 0;
if(equationFile)
{
while( equationFile >>
sensorNameEquation[row] >>
coefficientOverride[row][0] >> coefficientOverride[row][1] >>
coefficientOverride[row][2] >> coefficientOverride[row][3] >>
coefficient[row][4] >> oefficient[row][5] >>
dependantSensor[row] >> baseTemp[row])
{
row++;
printf("sensors: %d\n", row);
}
equationFile.close();//done reading from file...close it
}
它到达方程文件的第102行然后是seg错误。任何想法为什么会这样?
答案 0 :(得分:1)
看起来你要走出系数数组的边界,看起来它应该与其他人一起声明为静态浮动系数[ALLSENSORS] [6];
答案 1 :(得分:1)
您的数组coefficient
只有SENSORS(65)元素,但是第102行您已经读取了102个元素。 (您在每行之后递增row
)
在读取多于SENSORS元素之前,您需要一些方法来停止循环。