| adrem7 (27) | |||
I am running a piece of code I have written and it looks as follows:
| |||
|
|
|||
| adrem7 (27) | |
|
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. | |
|
|
|
| Grey Wolf (3232) | |
| It looks like you haven't quite got the hang of for loops, you are probably going out of bounds in one of them. | |
|
|
|
| adrem7 (27) | |
|
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. | |
|
|
|
| Grey Wolf (3232) | ||
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
|
||
| adrem7 (27) | |
|
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. | |
|
|
|
| Grey Wolf (3232) | |
|
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++). | |
|
|
|
| Grey Wolf (3232) | ||
| ||
|
|
||
| adrem7 (27) | |||
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.
How would I find out where this is happening? Thanks, A. | |||
|
|
|||
| Grey Wolf (3232) | |
|
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
|
|
| adrem7 (27) | ||
|
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?
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. | ||
|
|
||
| Grey Wolf (3232) | |||
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.
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
|
|||
| adrem7 (27) | |
|
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. | |
|
|
|
| Grey Wolf (3232) | |
|
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). | |
|
|
|
| adrem7 (27) | |
|
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! | |
|
|
|
| Grey Wolf (3232) | |
|
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
|
|
| adrem7 (27) | |
| Awesome, I shall look into that, thank you! | |
|
|
|
| Grey Wolf (3232) | |
| You're welcome. | |
|
Last edited on
|
|