title: Illegal Instruction 4 on Mac using C++
body: <p>I am running this code on my mac (10.8.2) through terminal and for certain n it terminates with "Illegal Instruction 4" does anyone know why, or how to stop it?</p>
  
  <p>I am confused as the code runs fine except for n=43, 45, 47 and 57.</p>
  
  <p>Also after n=60 it seems to happen for all n. Might it be a memory issue as it works fine for lower values of n.</p>
  
  <p>Note: anything to do with the integer c is purely to lessen the first while loop. Preferably I want the while loop to read b&lt;251 to give me 250 iterations, but I am instead running something like b&lt;51 for c=1,2,3,4,5 to get 5 different files with 50 results rather than 1 with 250.</p>
  
  <p>The code is as follows:</p>
  
  <pre><code>#include &lt;stdio.h&gt; 
  #include &lt;string.h&gt;
  #include &lt;math.h&gt;
  #include &lt;iostream&gt;
  #include &lt;iomanip&gt;
  #include &lt;fstream&gt;
  #include &lt;time.h&gt;
  #include &lt;stdlib.h&gt;
  #include &lt;sstream&gt;
  using namespace std;
  
  int main()
  {
  
    int a,b,c,f,i,j,k,m,n,s;
    double p,Time,Averagetime,Energy,energy,Distance,Length,DotProdForce,Forcemagnitude,
           ForceMagnitude[101],Force[101][4],E[10001],En[501],x[101][4],y[101][4];
  
    clock_t t1,t2;
    t1=clock();
  
    b=1;
    c=1;
    Time=0.0;
  
    while(b&lt;251){
  
      clock_t t3,t4;
      t3=clock();
  
      /*  set the number of points */
      n=45;
  
      /* check that there are no more than 100 points */
      if(n&gt;100){
        cout &lt;&lt; n &lt;&lt; " is too many points for me :-( \n";
        exit(0);
      }
  
      /* reset the random number generator */
      srand((unsigned)time(0));  
  
      for (i=1;i&lt;=n;i++){
        x[i][1]=((rand()*1.0)/(1.0*RAND_MAX)-0.5)*2.0;
        x[i][2]=((rand()*1.0)/(1.0*RAND_MAX)-0.5)*2.0;
        x[i][3]=((rand()*1.0)/(1.0*RAND_MAX)-0.5)*2.0;
  
        Length=sqrt(pow(x[i][1],2)+pow(x[i][2],2)+pow(x[i][3],2));
  
        for (k=1;k&lt;=3;k++){
          x[i][k]=x[i][k]/Length;
        }
      }
  
      /* calculate the energy */
      Energy=0.0;
  
      for(i=1;i&lt;=n;i++){
        for(j=i+1;j&lt;=n;j++){
          Distance=sqrt(pow(x[i][1]-x[j][1],2)+pow(x[i][2]-x[j][2],2)
                      +pow(x[i][3]-x[j][3],2));
  
          Energy=Energy+1.0/Distance;
        }
      }
  
      /* Save Original Points */
      for(i=1;i&lt;=n;i++){
        y[i][1]=x[i][1];
        y[i][2]=x[i][2];
        y[i][3]=x[i][3];
      }
  
      /* Loop for random points m times*/
      m=10;
  
      if (m&gt;100){
        cout &lt;&lt; "The m="&lt;&lt; m &lt;&lt; " loop is inefficient...lessen m \n";
        exit(0);
      }
  
      a=1;
  
      while(a&lt;m){
  
        /* assign random points */
        for (i=1;i&lt;=n;i++){
          x[i][1]=((rand()*1.0)/(1.0*RAND_MAX)-0.5)*2.0;
          x[i][2]=((rand()*1.0)/(1.0*RAND_MAX)-0.5)*2.0;
          x[i][3]=((rand()*1.0)/(1.0*RAND_MAX)-0.5)*2.0;
  
          Length=sqrt(pow(x[i][1],2)+pow(x[i][2],2)+pow(x[i][3],2));
  
          for (k=1;k&lt;=3;k++){
            x[i][k]=x[i][k]/Length;
          }
        }
  
        /* calculate the energy */
        energy=0.0;
  
        for(i=1;i&lt;=n;i++){
          for(j=i+1;j&lt;=n;j++){
            Distance=sqrt(pow(x[i][1]-x[j][1],2)+pow(x[i][2]-x[j][2],2)
                          +pow(x[i][3]-x[j][3],2));
  
            energy=energy+1.0/Distance;
          }
        }
  
        if(energy&lt;Energy)
          for(i=1;i&lt;=n;i++){
            for(j=1;j&lt;=3;j++){
              Energy=energy;
              y[i][j]=x[i][j];
            }
          }
        else
          for(i=1;i&lt;=n;i++){
            for(j=1;j&lt;=3;j++){
              energy=Energy;
              x[i][j]=y[i][j];
            }
          }
  
        a=a+1;
      }
  
      /* Create string stream 1 */
      ostringstream String1;
      String1 &lt;&lt; "Bestrandonpoints_" &lt;&lt; n;  //add number
  
      /* Output these points */
      ofstream File1 (String1.str().c_str());
      File1 &lt;&lt; "Energy=" &lt;&lt; Energy &lt;&lt; "\n";
      for(i=1;i&lt;=n;i++){
        File1 &lt;&lt; x[i][1] &lt;&lt; " " &lt;&lt;   x[i][2] &lt;&lt; " " &lt;&lt; x[i][3] &lt;&lt; "\n";
      }
      File1.close(); 
  
      /* For energy file later */
      E[0]=Energy;
  
      /* Start doing gradient flow approach */
      a=1;
      s=0;
      f=0;
      Forcemagnitude=1.0;
  
      if(n&gt;80)
        p=0.005;
      else if(n&gt;60)
        p=0.01;
      else if(n&gt;40)
        p=0.02;
      else
        p=0.05;
  
      while(Forcemagnitude&gt;0.00005){
  
        /* Reset initial force and energy change */
  
        for(i=1;i&lt;=n;i++){ 
          Force[i][1]=0.0; 
          Force[i][2]=0.0; 
          Force[i][3]=0.0; 
        } 
        /* Calculate force on each particle */
  
        for(i=1;i&lt;=n;i++){ 
          for(j=1;j&lt;i;j++){
  
            Distance=sqrt(pow(x[i][1]-x[j][1],2)+pow(x[i][2]-x[j][2],2)
                          +pow(x[i][3]-x[j][3],2));
            Force[i][1]=Force[i][1]+((x[i][1]-x[j][1])/(pow((Distance),3))); 
            Force[i][2]=Force[i][2]+((x[i][2]-x[j][2])/(pow((Distance),3))); 
            Force[i][3]=Force[i][3]+((x[i][3]-x[j][3])/(pow((Distance),3))); 
          } 
  
          for (j=i+1;j&lt;=n;j++){ 
  
            Distance=sqrt(pow(x[i][1]-x[j][1],2)+pow(x[i][2]-x[j][2],2)
                          +pow(x[i][3]-x[j][3],2));
            Force[i][1]=Force[i][1]+((x[i][1]-x[j][1])/(pow((Distance),3))); 
            Force[i][2]=Force[i][2]+((x[i][2]-x[j][2])/(pow((Distance),3))); 
            Force[i][3]=Force[i][3]+((x[i][3]-x[j][3])/(pow((Distance),3)));
          }
        }
  
        /* Add the force to my points */
        for(i=1;i&lt;=n;i++){
  
          DotProdForce=Force[i][1]*x[i][1]+Force[i][2]*x[i][2]+Force[i][3]*x[i][3];
  
          y[i][1]=x[i][1];
          y[i][2]=x[i][2];
          y[i][3]=x[i][3];
  
          Force[i][1]=Force[i][1]-DotProdForce*y[i][1];
          Force[i][2]=Force[i][2]-DotProdForce*y[i][2];
          Force[i][3]=Force[i][3]-DotProdForce*y[i][3];
  
          x[i][1] = y[i][1]+p*(Force[i][1]);
          x[i][2] = y[i][2]+p*(Force[i][2]);
          x[i][3] = y[i][3]+p*(Force[i][3]);
  
          /* Bring it back onto sphere */
  
          Length=sqrt(pow(x[i][1],2)+pow(x[i][2],2)+pow(x[i][3],2));
  
          for (j=1;j&lt;=3;j++){
            x[i][j]=x[i][j]/Length;
          }
        }
  
        /* Calculate the new energy */
  
        energy=0.0;
  
        for(i=1;i&lt;=n;i++){
          for(j=i+1;j&lt;=n;j++){
  
            Distance=sqrt(pow(x[i][1]-x[j][1],2)+pow(x[i][2]-x[j][2],2)
                          +pow(x[i][3]-x[j][3],2));
            energy=energy+1.0/Distance;
          }
        }
  
        E[a]=energy;
  
        /* Choose best energy and therefore best points */
        if (energy&lt;Energy)
          Energy=energy,s=s+1;
        else
          energy=Energy,f=f+1,p=(9.5*p)/10;
  
        for(i=1;i&lt;=n;i++){
          ForceMagnitude[i]=pow((pow(Force[i][1],2)+pow(Force[i][2],2)
                                 +pow(Force[i][3],2)),0.5);
        }
  
        for(i=1;i&lt;=n-1;i++){
          if(ForceMagnitude[i]&lt;ForceMagnitude[i+1])
            ForceMagnitude[i]=ForceMagnitude[i+1];
          else
            ForceMagnitude[i+1]=ForceMagnitude[i];
        }
  
        Forcemagnitude=ForceMagnitude[n];
  
        a=a+1;
      }
  
      En[b]=Energy;
  
      b=b+1;
  
      t4=clock();
      float diff ((float)t4-(float)t3);
      float seconds = diff / CLOCKS_PER_SEC;
  
      Time = Time + seconds;
  
    }
  
    Averagetime = Time/(b-1);
  
    cout &lt;&lt; fixed &lt;&lt; setprecision (4) &lt;&lt; "Average Time: " &lt;&lt; Averagetime &lt;&lt; "(s) \n";
    cout &lt;&lt; fixed &lt;&lt; setprecision(10) &lt;&lt; "Energy=" &lt;&lt; Energy &lt;&lt; "\n";
  
    t2=clock();
      float diff ((float)t2-(float)t1);
      float seconds = diff / CLOCKS_PER_SEC;
  
    /* Create string stream 2 */
    ostringstream String2;
    String2 &lt;&lt; "GFPoints_" &lt;&lt; n;  //add number
  
    /* Output these points */
    ofstream File2 (String2.str().c_str());
    for(i=1;i&lt;=n;i++){
      File2 &lt;&lt; x[i][1] &lt;&lt; " " &lt;&lt;   x[i][2] &lt;&lt; " " &lt;&lt; x[i][3] &lt;&lt; "\n";
    }
    File2.close(); 
  
     /* Create string stream 3 */
    ostringstream String3;
    String3 &lt;&lt; "GFEnergies_" &lt;&lt; n;  //add number
  
    /* Output these points */
    ofstream File3 (String3.str().c_str());
    for(i=1;i&lt;a;i++){
      File3 &lt;&lt; fixed &lt;&lt; setprecision (10) &lt;&lt; E[i] &lt;&lt; "\n";
    }
    File3.close(); 
  
    /* Output to help with gnuin.txt */
    ofstream File4 ("mypoints");
    for(i=1;i&lt;=n;i++){
      File4 &lt;&lt; x[i][1] &lt;&lt; " " &lt;&lt;   x[i][2] &lt;&lt; " " &lt;&lt; x[i][3] &lt;&lt; "\n";
    }
    File4.close(); 
  
    /* Create string stream 4 */
    ostringstream String4;
    String4 &lt;&lt; "GFInfo_" &lt;&lt; n;  //add number
  
    /* Output these points */
    ofstream File5 (String4.str().c_str());
    File5 &lt;&lt; "Iterations=" &lt;&lt; a-1 &lt;&lt; "\n";
    File5 &lt;&lt; "Successes=" &lt;&lt; s &lt;&lt; " Failures=" &lt;&lt; f &lt;&lt; "\n";
    File5 &lt;&lt; fixed &lt;&lt; setprecision(20) &lt;&lt; "Energy=" &lt;&lt; Energy &lt;&lt; "\n";
    File5 &lt;&lt; fixed &lt;&lt; setprecision(5) &lt;&lt; "Total run time: " &lt;&lt; seconds &lt;&lt; "(s) \n";
    File5 &lt;&lt; fixed &lt;&lt; setprecision(5) &lt;&lt; "Average run time: " &lt;&lt; Averagetime &lt;&lt; "(s) \n";
    File5.close(); 
  
    /* Create string stream 5 */
    ostringstream String5;
    String5 &lt;&lt; "GF%Energies_" &lt;&lt; n &lt;&lt; "_" &lt;&lt; c;  //add number
  
    /* Output these points */
    ofstream File6 (String5.str().c_str());
    for(i=1;i&lt;b;i++){
      File6 &lt;&lt; fixed &lt;&lt; setprecision(20) &lt;&lt; En[i] &lt;&lt; "\n";
    }
    File6.close();
  
    cout &lt;&lt; fixed &lt;&lt; setprecision(5) &lt;&lt; "Run time: " &lt;&lt; seconds &lt;&lt; "(s)" &lt;&lt; "\n";
    return 0;
  
  }
  </code></pre>
  
  <p>Thanks for any light you spread on the subject, A.</p>