title: Segmentation Fault 139 (core dumped) C++ after changing operating system
body: <p>I had a working program to analyse data in C++ that has produced something like 35 successful data files to date.  I was working on Scientific Linux in Code:Blocks when it was working and barring some small errors involving very large grid sizes (1000x1000+) it worked perfectly and produced exactly what I was looking for.</p>
  
  <p>I recently switched to Ubuntu and expected it to work fine and it doesn't.  It accepts initial input (the first particle switch) but then crashes immediately with a segmentation fault 139.  I have tried to run it in Windows instead with my dual boot but it doesn't seem to recognise the local filing system so I'm forced to ask for help.</p>
  
  <p>It's a long program so I'll reproduce the whole thing.  I apologise in advance.</p>
  
  <pre><code>// This program converts the column output of a 1D PIC code into a workable solution
  
  #include &lt;iostream&gt;
  #include &lt;fstream&gt;
  #include &lt;math.h&gt;
  using namespace std;
  
  double calculateXMaximum(double arraymax[], int size)
  {
      double maximum = 0;
      for (int k = 1; k &lt; size/2; k++)
      {
          if(arraymax[2*k] &gt; maximum)
          {
              maximum = arraymax[2*k];
          }
      }
      return maximum;
  }
  
  double calculateXMinimum(double arraymin[], int size)
  {
      double minimum = 0;
      for (int k = 1; k &lt; size/2; k++)
      {
          if(arraymin[2*k] &lt; minimum)
          {
              minimum = arraymin[2*k];
          }
      }
      return minimum;
  }
  
  double calculatePXMaximum(double arraymax[], int size)
  {
      double maximum = 0;
      for (int k = 1; k &lt; size/2; k++)
      {
          if(arraymax[2*k+1] &gt; maximum)
          {
              maximum = arraymax[2*k+1];
          }
      }
      return maximum;
  }
  
  double calculatePXMinimum(double arraymin[], int size)
  {
      double minimum = 0;
      for (int k = 1; k &lt; size/2; k++)
      {
          if(arraymin[2*k+1] &lt; minimum)
          {
              minimum = arraymin[2*k+1];
          }
      }
      return minimum;
  }
  
  int main()
  {
  // Variables settable before running program - will set up initialisation later.
  double xmin = 0;
  double xmax = 0;
  double pmin = 0;
  double pmax = 0;
  
  int xni = 0;
  double xntemp = 0;
  double deltax = 0;
  int xi; // X interpolates, defined from console for resolution of diagram
  
  int pnj = 0;
  double pntemp = 0;
  double deltap = 0;
  int pi;
  int type;
  double modifier;
  
  // Determines momentum modifier!
  
  cout &lt;&lt; "For particle type, enter 1 (e-) or 2 (p+)" &lt;&lt; endl;
  cout &lt;&lt; "Particle type:  ";
  cin &gt;&gt; type;
  
  if (type == 2)
  {
      modifier = 1836;
  }
  else
  {
      modifier = 1;
  }
  
  ifstream inputFile;
  ofstream outputFile;
  
  inputFile.open ("/home/Nick/fi020000.dat");
  outputFile.open ("/home/Nick/fi20k.dat");
  
  int dataformat[2];
  for(int rd = 0; rd &lt; 2; rd++)
  {
      dataformat[rd] = 0;
      inputFile &gt;&gt; dataformat[rd];
  }
  
  int records = dataformat[1] + 2;
  double data[records];
  cout &lt;&lt; "Number of particles: " &lt;&lt; dataformat[1]/2 &lt;&lt; endl;
  
  
  // Introduction of data from input data file loop.  Produces records.
  for (int count = 0; count &lt; records; count++)
  {
      inputFile &gt;&gt; data[count];
  }
  
  // Calling functions for xmin and xmax.  May streamline later
  
  xmax = calculateXMaximum(data, records) * 1.1;
  cout &lt;&lt; "Maximum x value: " &lt;&lt; xmax &lt;&lt; endl;
  xmin = calculateXMinimum(data, records) * 1.1;
  cout &lt;&lt; "Minimum x value: " &lt;&lt; xmin &lt;&lt; endl;
  pmax = calculatePXMaximum(data, records) * 1.1 / modifier;
  cout &lt;&lt; "Maximum p value: " &lt;&lt; pmax &lt;&lt; endl;
  pmin = calculatePXMinimum(data, records) * 1.1 / modifier;
  cout &lt;&lt; "Minimum p value: " &lt;&lt; pmin &lt;&lt; endl;
  
  // Definition of bin size
  
  cout &lt;&lt; "Entire desired number of x bins: ";
  cin &gt;&gt; xi;
  const int xip = xi;
  cout &lt;&lt; "Enter desired number of p bins: ";
  cin &gt;&gt; pi;
  const int pip = pi;
  cout &lt;&lt; "Grid is " &lt;&lt; xip &lt;&lt; " x " &lt;&lt; pip &lt;&lt; endl;
  
  // Calculate DELTA X and DELTA P
  deltax = (xmax - xmin)/(xip);
  deltap = (pmax - pmin)/(pip);
  
  cout &lt;&lt; "Resolution of x:  " &lt;&lt; deltax &lt;&lt; endl;
  cout &lt;&lt; "Resolution of p:  " &lt;&lt; deltap &lt;&lt; endl;
  
  int phaseSpace [xip][pip];
  for(int i=0; i&lt;xip; i++)
  {
      for(int j=0; j&lt;pip; j++)
      {
          phaseSpace[i][j] = 0;
      }
  }
  
  for (int phasecount=1; phasecount &lt; (records/2)-1; phasecount++)
  {
      xntemp = (data[2*phasecount] - xmin)/deltax;
      xni = floor(xntemp);
      pntemp = ((data[(2*phasecount)+1] / modifier) - pmin)/deltap;
      pnj = floor(pntemp);
      phaseSpace[xni][pnj] = phaseSpace[xni][pnj] + 1;
  }
  
  for (int xoutcount = 0; xoutcount &lt; xip; xoutcount++)
  {
      for (int poutcount = 0; poutcount &lt; pip; poutcount++)
      {
          outputFile &lt;&lt; xmin+((xoutcount+0.5)*deltax) &lt;&lt; " " &lt;&lt; pmin+((poutcount+0.5)*deltap) &lt;&lt; " "&lt;&lt; phaseSpace[xoutcount][poutcount] &lt;&lt; endl;
      }
      outputFile &lt;&lt; endl;
  }
  
  
  cout &lt;&lt; "Program complete" &lt;&lt; endl;
  
  return 0;
  
  }
  </code></pre>
  
  <p>My intent was to finish a 30 page report by this weekend and now the program that I was using to do this has completely fallen apart.  I am not a computer scientist - I'm a physicist and I learned C++ less than a month ago.  As such, I have no clue what's going on.  I know this topic has been seen a lot but I can't really understand the advice.</p>
  
  <p>EDIT:  Stack trace is:</p>
  
  <pre><code>#0 0x4010af     ??     ()     (??:??)
  #1 0x7ffff7215ea5       __libc_start_main() (/lib/x86_64-linux-gnu/libc.so.6:??)
  #2 0x4017f1 ??    () (??:??)
  </code></pre>
  
  <p>EDIT2:  Valgrind results</p>
  
  <pre><code>==4089== Using Valgrind-3.8.1 and LibVEX; rerun with -h for copyright info
  ==4089== Command: ./Analysis
  ==4089== 
  For particle type, enter 1 (e-) or 2 (p+)
  Particle type:  2
  ==4089== Warning: client switching stacks?  SP change: 0x7fefff9c0 --&gt; 0x7fe6d7118
  ==4089==          to suppress, use: --max-stackframe=9603240 or greater
  ==4089== Invalid write of size 8
  ==4089==    at 0x4010AF: ??? (in /home/paladin/Contour/Analysis/bin/Release/Analysis)
  ==4089==    by 0x5673EA4: (below main) (libc-start.c:260)
  ==4089==  Address 0x7fe6d7118 is on thread 1's stack
  ==4089== 
  ==4089== 
  ==4089== Process terminating with default action of signal 11 (SIGSEGV)
  ==4089==  Access not within mapped region at address 0x7FE6D7118
  ==4089==    at 0x4010AF: ??? (in /home/paladin/Contour/Analysis/bin/Release/Analysis)
  ==4089==  If you believe this happened as a result of a stack
  ==4089==  overflow in your program's main thread (unlikely but
  ==4089==  possible), you can try to increase the size of the
  ==4089==  main thread stack using the --main-stacksize= flag.
  ==4089==  The main thread stack size used in this run was 8388608.
  ==4089== 
  ==4089== Process terminating with default action of signal 11 (SIGSEGV)
  ==4089==  Access not within mapped region at address 0x7FE6D7111
  ==4089==    at 0x4A256A0: _vgnU_freeres (in /usr/lib/valgrind/vgpreload_core-amd64-linux.so)
  ==4089==  If you believe this happened as a result of a stack
  ==4089==  overflow in your program's main thread (unlikely but
  ==4089==  possible), you can try to increase the size of the
  ==4089==  main thread stack using the --main-stacksize= flag.
  ==4089==  The main thread stack size used in this run was 8388608.
  ==4089== 
  ==4089== HEAP SUMMARY:
  ==4089==     in use at exit: 17,520 bytes in 4 blocks
  ==4089==   total heap usage: 4 allocs, 0 frees, 17,520 bytes allocated
  ==4089== 
  ==4089== LEAK SUMMARY:
  ==4089==    definitely lost: 0 bytes in 0 blocks
  ==4089==    indirectly lost: 0 bytes in 0 blocks
  ==4089==      possibly lost: 0 bytes in 0 blocks
  ==4089==    still reachable: 17,520 bytes in 4 blocks
  ==4089==         suppressed: 0 bytes in 0 blocks
  ==4089== Rerun with --leak-check=full to see details of leaked memory
  ==4089== 
  ==4089== For counts of detected and suppressed errors, rerun with: -v
  ==4089== ERROR SUMMARY: 1 errors from 1 contexts (suppressed: 2 from 2)
  Segmentation fault (core dumped)
  </code></pre>
  
  <p>The fault occurs at the ifstream inputFile statement.</p>
  
  <p>EDIT3:  As requested, console session:</p>
  
  <pre><code>paladin@paladin:~/Programming/Contour Constructor 2/bin/Debug$ ./Contour\ Constructor\ 2 
  For particle type, enter 1 (e-) or 2 (p+)
  Particle type:  2
  Segmentation fault (core dumped)
  </code></pre>
  
  <p>There are 1200402 lines in the input file, corresponding to 600200 particles in the PIC code plus 2 descriptive lines.</p>
  
  <p>EDIT4:  <strong>Complete shot in the dark here</strong> but I originally compiled this under GCC 4.4.7 on Scientific Linux.  I am now using the latest Ubuntu version (4.8.1).  Has something changed in the interim which would invalidate the size of the file I'm using?</p>