0

运行以下代码时出现分段错误:

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 行,然后出现段错误。任何想法为什么会这样?

4

2 回答 2

1

看起来你要超出系数数组的范围,看起来应该与其他人一起声明为静态浮点系数[ALLSENSORS][6];

于 2013-08-28T14:09:41.260 回答
1

您的数组coefficient只有 SENSORS (65) 个元素,但到第 102 行时,您已将 102 个元素读入其中。(您row在每一行之后递增)

你需要一些方法在它读取超过 SENSORS 元素之前停止循环。

于 2013-08-28T14:09:42.483 回答