title: Segmentation fault while writing to outputfile
body: <p>In the following program, I am writing some data into a file named "griddata.dat". I am getting a segmentation fault during the function</p>
  
  <blockquote>
    <p>write_grid_file</p>
  </blockquote>
  
  <p>The function is partly executed. After running the programming, the last line of the file griddata.dat is "The thresholds are as follows. A threshold of 0 means that no houses are located at that point."</p>
  
  <p>The number of characters/lines/bytes of this file is dependent of the input by the user in earlier executed functions.</p>
  
  <p>Code:</p>
  
  <pre><code># include &lt;iostream&gt;
  # include &lt;fstream&gt;
  # include &lt;random&gt;  // int_distribution
  # include &lt;cmath&gt;   // sqrt
  # include &lt;algorithm&gt;   // max function
  # include &lt;vector&gt;
  
  using namespace std ;
  
  
  void make_grid(int&amp; width, int&amp; height, int&amp; length, int&amp; steps){}
  
  void assign_noise_levels_to_aircrafts(int&amp; nr_of_aircrafts, double&amp; max_noise_level, double&amp; min_noise_level, vector&lt;double&gt;&amp; 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&lt;double&gt; 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&lt;vector&lt;vector&lt;double&gt;&gt;&gt;&amp; noise_pollution, vector&lt;double&gt; noise_level){}
  
  void put_houses(int runway_ending_left, int runway_ending_right, int grid_mid_height, int grid_height, int grid_width, vector&lt;vector&lt;int&gt;&gt;&amp; nr_of_houses){}
  
  int count_housing_locations(vector&lt;vector&lt;int&gt;&gt; nr_of_houses, int grid_height, int grid_width){}
  
  void complete_data (int grid_height, int grid_width, int&amp; min_nr_of_flights, double&amp; const_threshold, vector&lt;vector&lt;int&gt;&gt; nr_of_houses, vector&lt;vector&lt;double&gt;&gt; threshold)
  {
      cout &lt;&lt; "Enter the threshold value of noise pollution at each housing location: " ;
      cin &gt;&gt; const_threshold ;
  
      for (int i=0 ; i &lt; grid_height ; i++)
      {
          vector&lt;double&gt; row ;
          for (int j=0 ; j &lt; 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 &lt;&lt; "Enter the number of flights that should at least be scheduled: " ;
      cin &gt;&gt; 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&lt;double&gt; noise_level, vector&lt;vector&lt;vector&lt;double&gt;&gt;&gt; noise_pollution, vector&lt;vector&lt;int&gt;&gt; nr_of_houses, vector&lt;vector&lt;double&gt;&gt; threshold)
  {
      ofstream gridfile ;
      gridfile.open ("griddata.dat") ;
  
      gridfile &lt;&lt; "Width of the grid is " &lt;&lt; width &lt;&lt; "km.\n"
           &lt;&lt; "Height of the grid is " &lt;&lt; height &lt;&lt; "km.\n"
           &lt;&lt; "Length of the runway is " &lt;&lt; length &lt;&lt; "km.\n"
           &lt;&lt; "There are " &lt;&lt; steps &lt;&lt; " measure points per km. \n\n"
           &lt;&lt; "Therefore, the grid is a matrix with " &lt;&lt; grid_height &lt;&lt; " rows and " &lt;&lt; grid_width &lt;&lt; " columns.\n"
           &lt;&lt; "The midpoint of the grid is [" &lt;&lt; grid_mid_height &lt;&lt; "," &lt;&lt; grid_mid_width &lt;&lt; "].\n"
           &lt;&lt; "The runway ranges from [" &lt;&lt; grid_mid_height &lt;&lt; "," &lt;&lt; runway_ending_left &lt;&lt; "] to [" &lt;&lt; grid_mid_height &lt;&lt; "," &lt;&lt; runway_ending_right &lt;&lt;"].\n\n" ;
  
      for(int i=0 ; i &lt; nr_of_aircrafts ; i++)
          gridfile &lt;&lt; "The noise level of aircraft " &lt;&lt; i+1 &lt;&lt; " is " &lt;&lt; noise_level[i] &lt;&lt; endl ;
      gridfile &lt;&lt; endl ;
  
      for(int aircraft=0 ; aircraft &lt; nr_of_aircrafts ; aircraft++)
      {
          gridfile &lt;&lt; "The noise pollution of aircraft " &lt;&lt; aircraft+1 &lt;&lt; " is given by:" &lt;&lt; endl ;
          for(int i=0 ; i &lt; grid_height ; i++)
          {
              for(int j=0 ; j &lt; grid_width ; j++)
                  gridfile &lt;&lt; noise_pollution[aircraft][i][j] &lt;&lt; "    " ;
              gridfile &lt;&lt; endl ;
          }
          gridfile &lt;&lt; endl ;
      }
      gridfile &lt;&lt; endl ;
  
      gridfile &lt;&lt; "The houses are located as follows:" &lt;&lt; endl ;
      for(int i=0 ; i &lt; grid_height ; i++)
      {   for(int j=0 ; j &lt; grid_width ; j++)
              gridfile &lt;&lt; nr_of_houses[i][j] &lt;&lt; " " ;
          gridfile &lt;&lt; endl ;
      }
      gridfile &lt;&lt; endl ;
  
      gridfile &lt;&lt; "The number of housing locations is " &lt;&lt; count_housing_locations(nr_of_houses, grid_height, grid_width)
           &lt;&lt; endl &lt;&lt; endl ;
  
      gridfile &lt;&lt; "The thresholds are as follows. A threshold of 0 means that no houses are located at that point. " &lt;&lt; endl &lt;&lt; endl ;
  // segmentation fault here
      for(int i=0 ; i &lt; grid_height ; i++)
      {   for(int j=0 ; j &lt; grid_width ; j++)
              gridfile &lt;&lt; threshold[i][j] &lt;&lt; "    " ;
          gridfile &lt;&lt; endl ;
      }
      gridfile &lt;&lt; endl &lt;&lt; endl
           &lt;&lt; "The number of flights that must at least be scheduled is " &lt;&lt; min_nr_of_flights &lt;&lt; endl ;
  
      gridfile.close() ;
  }
  
  void write_scip_file (int grid_height, int grid_width, int nr_of_aircrafts, int min_nr_of_flights, vector&lt;vector&lt;int&gt;&gt; nr_of_houses, vector&lt;vector&lt;double&gt;&gt; threshold, vector&lt;vector&lt;vector&lt;double&gt;&gt;&gt; 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&lt;double&gt; 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&lt;vector&lt;vector&lt;double&gt;&gt;&gt; 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&lt;vector&lt;int&gt;&gt; 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&lt;vector&lt;double&gt;&gt; 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 &lt;&lt; endl &lt;&lt; "Done!" &lt;&lt; endl &lt;&lt; endl ;
  
   return 0 ;
  }
  </code></pre>