写入输出文件时出现分段错误

时间:2014-09-11 11:24:02

标签: c++ file-io segmentation-fault

在下面的程序中,我将一些数据写入名为" griddata.dat"的文件中。我在函数

期间遇到了分段错误
  

write_grid_file

该功能部分执行。运行编程后,文件griddata.dat的最后一行是"阈值如下。阈值为0表示此时没有房屋。"

此文件的字符数/行数/字节数取决于用户在先前执行的函数中的输入。

代码:

# include <iostream>
# include <fstream>
# include <random>  // int_distribution
# include <cmath>   // sqrt
# include <algorithm>   // max function
# include <vector>

using namespace std ;


void make_grid(int& width, int& height, int& length, int& steps){}

void assign_noise_levels_to_aircrafts(int& nr_of_aircrafts, double& max_noise_level, double& min_noise_level, vector<double>& noise_level){}

double dist(int point_1[2], int point_2[2], int steps){}

double noise_pollution_at_point(int aircraft, int point[2], vector<double> noise_level, int steps, int runway_ending_left, int grid_width, int grid_mid_height){}

void calculate_noise_pollution(int nr_of_aircrafts, int grid_height, int grid_width, int grid_mid_height, int steps, int runway_ending_left, vector<vector<vector<double>>>& noise_pollution, vector<double> noise_level){}

void put_houses(int runway_ending_left, int runway_ending_right, int grid_mid_height, int grid_height, int grid_width, vector<vector<int>>& nr_of_houses){}

int count_housing_locations(vector<vector<int>> nr_of_houses, int grid_height, int grid_width){}

void complete_data (int grid_height, int grid_width, int& min_nr_of_flights, double& const_threshold, vector<vector<int>> nr_of_houses, vector<vector<double>> threshold)
{
    cout << "Enter the threshold value of noise pollution at each housing location: " ;
    cin >> const_threshold ;

    for (int i=0 ; i < grid_height ; i++)
    {
        vector<double> row ;
        for (int j=0 ; j < grid_width ; j++)
        {
            if(nr_of_houses[i][j]==0)
                row.push_back(0) ;
            else
                row.push_back(const_threshold) ;
        }
    threshold.push_back(row) ;
    }
    cout << "Enter the number of flights that should at least be scheduled: " ;
    cin >> min_nr_of_flights ;
}

void write_grid_file (int width, int height, int length, int steps, int grid_height, int grid_width, int grid_mid_height, int grid_mid_width, int runway_ending_left, int runway_ending_right, int nr_of_aircrafts, int min_nr_of_flights, vector<double> noise_level, vector<vector<vector<double>>> noise_pollution, vector<vector<int>> nr_of_houses, vector<vector<double>> threshold)
{
    ofstream gridfile ;
    gridfile.open ("griddata.dat") ;

    gridfile << "Width of the grid is " << width << "km.\n"
         << "Height of the grid is " << height << "km.\n"
         << "Length of the runway is " << length << "km.\n"
         << "There are " << steps << " measure points per km. \n\n"
         << "Therefore, the grid is a matrix with " << grid_height << " rows and " << grid_width << " columns.\n"
         << "The midpoint of the grid is [" << grid_mid_height << "," << grid_mid_width << "].\n"
         << "The runway ranges from [" << grid_mid_height << "," << runway_ending_left << "] to [" << grid_mid_height << "," << runway_ending_right <<"].\n\n" ;

    for(int i=0 ; i < nr_of_aircrafts ; i++)
        gridfile << "The noise level of aircraft " << i+1 << " is " << noise_level[i] << endl ;
    gridfile << endl ;

    for(int aircraft=0 ; aircraft < nr_of_aircrafts ; aircraft++)
    {
        gridfile << "The noise pollution of aircraft " << aircraft+1 << " is given by:" << endl ;
        for(int i=0 ; i < grid_height ; i++)
        {
            for(int j=0 ; j < grid_width ; j++)
                gridfile << noise_pollution[aircraft][i][j] << "    " ;
            gridfile << endl ;
        }
        gridfile << endl ;
    }
    gridfile << endl ;

    gridfile << "The houses are located as follows:" << endl ;
    for(int i=0 ; i < grid_height ; i++)
    {   for(int j=0 ; j < grid_width ; j++)
            gridfile << nr_of_houses[i][j] << " " ;
        gridfile << endl ;
    }
    gridfile << endl ;

    gridfile << "The number of housing locations is " << count_housing_locations(nr_of_houses, grid_height, grid_width)
         << endl << endl ;

    gridfile << "The thresholds are as follows. A threshold of 0 means that no houses are located at that point. " << endl << endl ;
// segmentation fault here
    for(int i=0 ; i < grid_height ; i++)
    {   for(int j=0 ; j < grid_width ; j++)
            gridfile << threshold[i][j] << "    " ;
        gridfile << endl ;
    }
    gridfile << endl << endl
         << "The number of flights that must at least be scheduled is " << min_nr_of_flights << endl ;

    gridfile.close() ;
}

void write_scip_file (int grid_height, int grid_width, int nr_of_aircrafts, int min_nr_of_flights, vector<vector<int>> nr_of_houses, vector<vector<double>> threshold, vector<vector<vector<double>>> noise_pollution){}


int main()
{
// design the grid
    int steps ;
    int width ;
    int height ;
    int length ;

    make_grid(width, height, length, steps) ;

// define aircrafts by calculating their noise levels
    int nr_of_aircrafts ;
    double max_noise_level ;
    double min_noise_level ;
    vector<double> noise_level ;

    assign_noise_levels_to_aircrafts(nr_of_aircrafts, max_noise_level, min_noise_level, noise_level) ;

// calculate noise pollution for every aircraft
    int grid_width = (width*steps) + 1 ;
    int grid_height = (height*steps) + 1 ;

    int grid_mid_width = 0.5*width*steps ;
    int grid_mid_height = 0.5*height*steps ;
    int runway_ending_left = grid_mid_width - 0.5*length*steps ;
    int runway_ending_right = grid_mid_width + 0.5*length*steps ;

    vector<vector<vector<double>>> noise_pollution ;

    calculate_noise_pollution(nr_of_aircrafts, grid_height, grid_width, grid_mid_height, steps, runway_ending_left, noise_pollution, noise_level) ;

// put one house on the grid points at random
    vector<vector<int>> nr_of_houses ;

    put_houses(runway_ending_left, runway_ending_right, grid_mid_height, grid_height, grid_width, nr_of_houses) ;

// completing the data (b,k)
    vector<vector<double>> threshold ;
    double const_threshold ;
    int min_nr_of_flights ;

    complete_data (grid_height, grid_width, min_nr_of_flights, const_threshold, nr_of_houses, threshold) ;

// write data into two files, one for people, one for scip
    write_grid_file(width, height, length, steps, grid_height, grid_width, grid_mid_height, grid_mid_width, runway_ending_left, runway_ending_right, nr_of_aircrafts, min_nr_of_flights, noise_level, noise_pollution, nr_of_houses, threshold) ;

    write_scip_file (grid_height, grid_width, nr_of_aircrafts, min_nr_of_flights, nr_of_houses, threshold, noise_pollution) ;

// test if the whole program is run
    cout << endl << "Done!" << endl << endl ;

 return 0 ;
}

1 个答案:

答案 0 :(得分:1)

complete_data按值threshold获取,因此不会修改主threshold。但是write_grid_file使用[]运算符索引到这个空向量,导致未定义的行为。 (可能还有其他错误,这个错误通过阅读您的代码而突出了我。)

首先,重新设计代码,以便在迭代向量时,实际使用其size属性或其自己的开始/结束迭代器迭代向量。请勿使用grid_height作为此其他向量的维度。

此外,如果对您的索引有任何疑问,请使用.at()代替[],以便您可以在失败时抛出异常

由于函数参数很多,您的代码很难遵循。相反,你应该使用面向对象的设计。使您的网格由类表示,然后您的函数只需要将该类的一个对象作为参数,可能有一个或两个其他参数。

最后,学会使用调试器查看段错误的来源。