Illegal Instruction 4 Mac

I am running a piece of code I have written and it looks as follows:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
#include <stdio.h> 
#include <string.h>
#include <math.h>
#include <iostream>
#include <iomanip>
#include <fstream>
#include <time.h>
#include <stdlib.h>
#include <sstream>
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<26){

    clock_t t3,t4;
    t3=clock();

    /*  set the number of points */
    n=45;

    /* check that there are no more than 100 points */
    if(n>100){
      cout << n << " is too many points for me :-( \n";
      exit(0);
    }
  
    /* reset the random number generator */
    srand((unsigned)time(0));  
  
    for (i=1;i<=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<=3;k++){
        x[i][k]=x[i][k]/Length;
      }
    }

    /* calculate the energy */
    Energy=0.0;
  
    for(i=1;i<=n;i++){
      for(j=i+1;j<=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<=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>100){
      cout << "The m="<< m << " loop is inefficient...lessen m \n";
      exit(0);
    }
  
    a=1;
  
    while(a<m){

      /* assign random points */
      for (i=1;i<=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<=3;k++){
          x[i][k]=x[i][k]/Length;
        }
      }

      /* calculate the energy */
      energy=0.0;
  
      for(i=1;i<=n;i++){
        for(j=i+1;j<=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<Energy)
        for(i=1;i<=n;i++){
          for(j=1;j<=3;j++){
            Energy=energy;
            y[i][j]=x[i][j];
          }
        }
      else
        for(i=1;i<=n;i++){
          for(j=1;j<=3;j++){
            energy=Energy;
            x[i][j]=y[i][j];
          }
        }
      
      a=a+1;
    }
  
    /* Create string stream 1 */
    ostringstream String1;
    String1 << "Bestrandonpoints_" << n;  //add number

    /* Output these points */
    ofstream File1 (String1.str().c_str());
    File1 << "Energy=" << Energy << "\n";
    for(i=1;i<=n;i++){
      File1 << x[i][1] << " " <<   x[i][2] << " " << x[i][3] << "\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>80)
      p=0.005;
    else if(n>60)
      p=0.01;
    else if(n>40)
      p=0.02;
    else
      p=0.05;

    while(Forcemagnitude>0.00005){

      /* Reset initial force and energy change */
  
      for(i=1;i<=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<=n;i++){ 
        for(j=1;j<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<=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<=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<=3;j++){
          x[i][j]=x[i][j]/Length;
        }
      }
      
      /* Calculate the new energy */
    
      energy=0.0;
  
      for(i=1;i<=n;i++){
        for(j=i+1;j<=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<Energy)
        Energy=energy,s=s+1;
      else
        energy=Energy,f=f+1,p=(9.5*p)/10;
 
      for(i=1;i<=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<=n-1;i++){
        if(ForceMagnitude[i]<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 << fixed << setprecision (4) << "Average Time: " << Averagetime << "(s) \n";
  cout << fixed << setprecision(10) << "Energy=" << Energy << "\n";

  t2=clock();
    float diff ((float)t2-(float)t1);
    float seconds = diff / CLOCKS_PER_SEC;

  /* Create string stream 2 */
  ostringstream String2;
  String2 << "GFPoints_" << n;  //add number

  /* Output these points */
  ofstream File2 (String2.str().c_str());
  for(i=1;i<=n;i++){
    File2 << x[i][1] << " " <<   x[i][2] << " " << x[i][3] << "\n";
  }
  File2.close(); 
  
   /* Create string stream 3 */
  ostringstream String3;
  String3 << "GFEnergies_" << n;  //add number

  /* Output these points */
  ofstream File3 (String3.str().c_str());
  for(i=1;i<a;i++){
    File3 << fixed << setprecision (10) << E[i] << "\n";
  }
  File3.close(); 
  
  /* Output to help with gnuin.txt */
  ofstream File4 ("mypoints");
  for(i=1;i<=n;i++){
    File4 << x[i][1] << " " <<   x[i][2] << " " << x[i][3] << "\n";
  }
  File4.close(); 
  
  /* Create string stream 4 */
  ostringstream String4;
  String4 << "GFInfo_" << n;  //add number

  /* Output these points */
  ofstream File5 (String4.str().c_str());
  File5 << "Iterations=" << a-1 << "\n";
  File5 << "Successes=" << s << " Failures=" << f << "\n";
  File5 << fixed << setprecision(20) << "Energy=" << Energy << "\n";
  File5 << fixed << setprecision(5) << "Total run time: " << seconds << "(s) \n";
  File5 << fixed << setprecision(5) << "Average run time: " << Averagetime << "(s) \n";
  File5.close(); 
  
  /* Create string stream 5 */
  ostringstream String5;
  String5 << "GF%Energies_" << n << "_" << c;  //add number

  /* Output these points */
  ofstream File6 (String5.str().c_str());
  for(i=1;i<b;i++){
    File6 << fixed << setprecision(20) << En[i] << "\n";
  }
  File6.close();
  
  cout << fixed << setprecision(5) << "Run time: " << seconds << "(s)" << "\n";
  return 0;

}

The problem is for certain n (43,45,47,57) the code returns : Illegal instruction 4. I have no idea what this is or why it is happening.

The number of points in defined by n on line 32, and the number of times I want the code to run is defined by the while loop on line 26 (if I remove this loop it runs fine).

Does anyone know what Illegal Instruction 4 is and how to fix it?

Thanks, A.

Note: Preferably I want the while loop to be for b<251 but sometimes this produces a seg fault so I have lessened it to 26 and run the code for c= 1 to 10 instead to get 250 iterations.

Note: Is there a way I can add an attachment here instead of copying and pasting a large block of code and using word count? Thanks.
It looks like you haven't quite got the hang of for loops, you are probably going out of bounds in one of them.
If I remove the while loop at the beginning it works fine, I just put this while loop around the rest of the code at the end to get it to do x iterations of the same thing. That is when the illegal instruction started happening which makes me think it is to do with that rather than the for loops.

Why does it look like I don't have the hang of for loops, what is the issue with them??

Thanks, A.
Why does it look like I don't have the hang of for loops, what is the issue with them??
mainly it is the starting at 1 and ending with a <= comparison.

I have had a second to run your code, the for loop on line 237 blows up as n has a value 1083030737, I'm assuming that somewhere else is going out of bounds an writing garbage into the location of n.

I'm at work so am a bit limited as to how much time I can look at it.
Last edited on
Firstly, thanks so much for responding, especially being at work, it is really appreciated.

Secondly, I don't understand what you mean when you say: as n has a value 1083030737.

n is defined by me at the start of the code and doesn't alter. I only need results for n from 2 to 100.
Illegal instruction 4 (or signal 4: Illegal instruction) would be that the op code for a instruction is not supported by the processor. This can result from an uninitialised (or corrupted) function pointer (or vtable in C++).

Secondly, I don't understand what you mean when you say: as n has a value 1083030737.
at some point in the execution of the program the memory location the stores the value for n is overwritten.
Illegal instruction 4 (or signal 4: Illegal instruction) would be that the op code for a instruction is not supported by the processor. This can result from an uninitialised (or corrupted) function pointer (or vtable in C++).


When googling illegal instruction 4 it says stuff about Lion for mac that I didn't really understand. Might it be an issue with my laptop? I have a MacBook Pro (Mid 2010) with 2.4GHz Processor and 4Gb RAM running OSX 10.8.2.

at some point in the execution of the program the memory location the stores the value for n is overwritten.


How would I find out where this is happening?

Thanks, A.
This is what I think is happening:

You have while(Forcemagnitude>0.00005){ and each time to loop around you are incremental a. It looks like a is getting too large when you come to E[a]=energy; and as you go out of bounds of E[] you start corrupting you other data and eventual you overwrite n and so on.

So start by adding a condition to the while loop to stop a from getting too large.
Last edited on
That's it! Thanks!

I can obviously just temporarily stop this from happening by increasing the array size in the scope (double E[100000000001]) or something (which I did and stops the error), but if I wanted a better solution how would I go about that?

Can I create an array which grows with the data to a certain point?

So start by adding a condition to the while loop to stop a from getting too large.


I could do this but it would limit the accuracy of my results. Forcemagnitude<0.00005 was chosen to give me a certain accuracy of the Energy.

Also, how did you figure this out? I guess if I had sat long enough you can spot the issue with a growing too large, but how did you know about the issues with corrupt data?

Thanks, A.
Can I create an array which grows with the data to a certain point?

You could look at vectors
http://www.cplusplus.com/reference/vector/vector/
NB: I have not looked to closely at what the aim of the code is but vectors would be a good starting point.

but you should still always look at ways of putting the breaks on to prevent runaway code.

Also, how did you figure this out? I guess if I had sat long enough you can spot the issue with a growing too large, but how did you know about the issues with corrupt data?

That's kind of hard to answer. In part it is twenty-odd years of experience but mainly it is starting at the point where the program fails and working you way back to the cause of the failure; in this regard knowing how to use a good debugger comes in handy.
Last edited on
How might I go about gaining that knowledge?? Also will I have to purchase a debugger for my mac or can I just download one online??

Thanks again, you have been a great help especially with explanation. As someone new to this kind of thing it is sometimes difficult to follow solutions and jargon but all your answers have been thorough and eloquently put.

A.
It may be beneficial to watch some of the Stanford University lectures on programming paradigms. Lecture 3 has some info on arrays but it is probably worth starting at lecture 2. Don't bother with lecture 1, it is admin stuff for the course.

This should help you get a good mental image of how variables are stored in memory.

Lecture 2 | Programming Paradigms (Stanford)
http://www.youtube.com/watch?v=jTSvthW34GU

Lecture 3 | Programming Paradigms (Stanford)
http://www.youtube.com/watch?v=H4MQXBF6FN4

Also available through iTunes U

As to the debugger, you should already have one with your compiler/IDE (probable GDB).
I had to install XCode to compile my code but have never used it. I just type out the code I want in text wrangler and then run it through terminal commands. Would this mean XCode will have the debugger within it? If so can I access it's functionality without using Xcode, just by terminal commands?

Thanks for the links, I shall have a look into it. Being recently self taught I very much struggle with mental images and so am just really regurgitating things I have learnt online which is why the code is quite technically poor (starting arrays at 1 instead of 0 and not using functions at all).

These kind of things really help though, and when I have the time I shall spend it clearing up the code. The problem is I really need the data so since it runs I tend to always put the neatening on the bottom of the priority list. It shall get done at some point though!
If you want to do command line compiling, you should be able to open xcode, go to preferences, in there you should see a tab for downloads. Under components there is an option for command line tools.

xcode comes with GDB and LLDB.
Quick start GDB tutorial: http://www.cs.cmu.edu/~gilpin/tutorial/
Last edited on
Awesome, I shall look into that, thank you!
You're welcome.
Last edited on
Topic archived. No new replies allowed.