title: Large array, stack overflow, and dynamically allocated
body: <p>I am wondering how could I create and initiate this large array without resorting to vector. Basically, I have been trying to create a large 1D array whose each element is a cell of a cubic with side L=NGRID cells. NGRID is read from a parameter file.  </p>
  
  <pre><code>#include &lt;stdio.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;sstream&gt;
  #include &lt;fstream&gt;
  #include &lt;iostream&gt;
  #include &lt;string&gt;
  #include &lt;memory&gt;
  
  
  #define pow3(x) ((x)*(x)*(x))
  #define at(ix,iy,iz) ((ix*NGRID*NGRID) + (iy*NGRID) + (iz))
  
  std::string output_file="halo_snapshot_z0.txt"; // name of the output file
  std::ofstream myoutput;
  
  double SBOX;    // size of the box
  int NGRID;      // number of grid cells
  
  int main(int argc, char *argv[])
  {
      std::ostringstream input_file;
      std::ostringstream para_file;
  
      input_file &lt;&lt; argv[1];
      para_file &lt;&lt; argv[2];
      std::string input_file_string = input_file.str();
      std::string para_file_string = para_file.str();
  
      FILE* pfile = std::fopen(para_file_string.c_str(),"r");
      if (!pfile) {
          std::cout &lt;&lt; "Parameter file not found!" &lt;&lt; std::endl;
      }
      std::fscanf (pfile, "%lf", &amp;SBOX);
      std::cout &lt;&lt; SBOX &lt;&lt; std::endl;
      std::fscanf (pfile, "%d", &amp;NGRID);
      std::cout &lt;&lt; NGRID &lt;&lt; std::endl;
      double SGRID = SBOX/NGRID;
  
      // Initiate the halo density array
      int* densh;
      densh = (int*) malloc(pow3(NGRID) * sizeof(int));
      std::uninitialized_fill_n(densh, pow3(NGRID), 0);
  
      // Read the halo catalogue
      std::ifstream ifs(input_file_string.c_str(),std::ifstream::in); // in C++, the string variable &lt;filename&gt; cannot be used as a parameter, instead one must use filename.c_str()
      std::string iline;
  
      while(std::getline(ifs, iline)) // Read one entire line from ifs
      {
          std::istringstream iss(iline); // Access the line as a stream
          // Take in only the first six columns
          long int id;
          long int host;
          int sub_no;
          double mass;
          int part_no;
          double x; // x coordinate in kpc
          double y; // y coordinate in kpc
          double z; // z coordinate in kpc
          double vx;
          double vy;
          double vz;
          double rvir;
          double rmax;
          float dummy1;
          float dummy2;
          float dummy3;
          float dummy4;
          float dummy5;
          float dummy6;
          float dummy7;
          float dummy8;
          float dummy9;
          float dummy10;
          float dummy11;
          float dummy12;
          float dummy13;
          float dummy14;
          float dummy15;
          float dummy16;
          float dummy17;
          float dummy18;
          float dummy19;
          float dummy20;
          float dummy21;
          float dummy22;
          float dummy23;
          int   dummy24;
          float dummy25;
          float dummy26;
          float dummy27;
          float dummy28;
          float dummy29;
          float dummy30;
  
  
          iss &gt;&gt; id &gt;&gt; host &gt;&gt; sub_no &gt;&gt; mass &gt;&gt; part_no &gt;&gt; x &gt;&gt; y &gt;&gt; z &gt;&gt; vx &gt;&gt; vy &gt;&gt; vz &gt;&gt; rvir &gt;&gt; rmax &gt;&gt; dummy1 &gt;&gt;  dummy2 &gt;&gt;  dummy3 &gt;&gt;  dummy4 &gt;&gt;  dummy5 &gt;&gt;  dummy6 &gt;&gt;  dummy7 &gt;&gt;  dummy8 &gt;&gt;  dummy9 &gt;&gt;  dummy10 &gt;&gt;  dummy11 &gt;&gt;  dummy12 &gt;&gt;  dummy13 &gt;&gt;  dummy14 &gt;&gt;  dummy15 &gt;&gt;  dummy16 &gt;&gt;  dummy17 &gt;&gt;  dummy18 &gt;&gt;  dummy19 &gt;&gt;  dummy20 &gt;&gt;  dummy21 &gt;&gt;  dummy22 &gt;&gt;  dummy23 &gt;&gt;  dummy24 &gt;&gt;  dummy25 &gt;&gt;  dummy26 &gt;&gt;  dummy27 &gt;&gt;  dummy28 &gt;&gt;  dummy29 &gt;&gt;  dummy30 ; // read the entire line
  
          // Select only host halos and assign them onto the grid
  
          if (host==0) {
              //NGP mass assignment
              float gx = x/SGRID-1.;
              float gy = y/SGRID-1.;
              float gz = z/SGRID-1.;
  
              int ix = (int)(gx+0.5);
              int iy = (int)(gy+0.5);
              int iz = (int)(gz+0.5);
  
              densh[at(ix,iy,iz)] += 1;
          }
      }
  
      myoutput.open (output_file.c_str(),std::ofstream::out);
      for (int i=0;i&lt;pow3(NGRID);++i) {
          myoutput &lt;&lt; densh[i] &lt;&lt; '\n';
      }
      myoutput.close();
      myoutput.clear();
  
      return 0;
  }
  </code></pre>
  
  <p>This gives me segmentation fault when I try to work with NGRID=250.
  Thank you very much!</p>
  
  <p><strong>EDIT:</strong> edited to include the full code, sorry if it looks messy, thanks for your time!</p>