Thread: getting an Illegal Instruction error

  1. #1
    Registered User
    Join Date
    Apr 2008
    Posts
    18

    getting an Illegal Instruction error

    Hi, I have a two dimensional array of characters that I am separating into a one dimensional array. That one dimensional array is broken down into 5 sets of character arrays. I'm trying to assign multiple one dimensional arrays to the same part of the two dimensional array, it complies but when i got to run it it throws me an "Illegal Instruction" error. An excerpt of the code can be seen below:

    h is incremented each time through a loop

    Code:
            opcode[0]=str1[h][0];
    	opcode[1]=str1[h][1];
    	opcode[2]=str1[h][2];
    	opcode[3]=str1[h][3];
    	opcode[4]=str1[h][4];
    	opcode[5]=str1[h][5];
    	opcode[6] = '\0';
    	
    	rs[0]=str1[h][6];
    	rs[1]=str1[h][7];
    	rs[2]=str1[h][8];
    	rs[3]=str1[h][9];
    	rs[4]=str1[h][10];
    	rs[5]= '\0';
    	
    	rt[0]=str1[h][11];
    	rt[1]=str1[h][12];
    	rt[2]=str1[h][13];
    	rt[3]=str1[h][14];
    	rt[4]=str1[h][15];
    	rt[5]='\0';
    
           //Second set of one dimensional arrays
    
            opcodeD[0]=str1[h][0];
    	opcodeD[1]=str1[h][1];
    	opcodeD[2]=str1[h][2];
    	opcodeD[3]=str1[h][3];
    	opcodeD[4]=str1[h][4];
    	opcodeD[5]=str1[h][5];
    	opcodeD[6]='\0';
    	
    	rsD[0]=str1[h][6];
    	rsD[1]=str1[h][7];
    	rsD[2]=str1[h][8];
    	rsD[3]=str1[h][9];
    	rsD[4]=str1[h][10];
    	rsD[5]='\0';
    	
    	rtD[0]=str1[h][11];
    	rtD[1]=str1[h][12];
    	rtD[2]=str1[h][13];
    	rtD[3]=str1[h][14];
    	rtD[4]=str1[h][15];
    	rtD[5]='\0';

    mind you that the size of the program is 1344 lines long, I'm not sure if this has anything to do with it.

  2. #2
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Could you type the exact error? And is it a run-time or compile-time error? If run-time, can I see the declaration of all of those arrays, please. If its a compile-time error, can you post your whole source (as an attachment) and identify your compiler, please.

    But off hand, is h going out of bounds?

  3. #3
    Registered User
    Join Date
    Apr 2008
    Posts
    18
    it run-time error:

    The exact error is: Illegal instruction

    Code:
    char str1[1000][36];			//Strings are created to hold the values of the input string
    char str[36];
    char opcode [7];		//the opcode, rs, rt, func, address, imm, rd, and shampt
    char rs[6];
    char rt[6];
    char func[7];
    char address[27];
    char imm[17];
    char rd[6];
    char shampt[6];
    
    for( h = 8; h<= 3; h++)
    	{
    	
    	opcode[0]=str1[h][0];
    	opcode[1]=str1[h][1];
    	opcode[2]=str1[h][2];
    	opcode[3]=str1[h][3];
    	opcode[4]=str1[h][4];
    	opcode[5]=str1[h][5];
    	opcode[6] = '\0';
    	
    	rs[0]=str1[h][6];
    	rs[1]=str1[h][7];
    	rs[2]=str1[h][8];
    	rs[3]=str1[h][9];
    	rs[4]=str1[h][10];
    	rs[5]= '\0';
    	
    	rt[0]=str1[h][11];
    	rt[1]=str1[h][12];
    	rt[2]=str1[h][13];
    	rt[3]=str1[h][14];
    	rt[4]=str1[h][15];
    	rt[5]='\0';
    	
    	rd[0]=str1[h][16];
    	rd[1]=str1[h][17];
    	rd[2]=str1[h][18];
    	rd[3]=str1[h][19];
    	rd[4]=str1[h][20];
    	rd[5]='\0';
    	
    	shampt[0]=str1[h][21];
    	shampt[1]=str1[h][22];
    	shampt[2]=str1[h][23];
    	shampt[3]=str1[h][24];
    	shampt[4]=str1[h][25];
    	shampt[5]='\0';
    	
    	address[0]=str1[h][6];
    	address[1]=str1[h][7];
    	address[2]=str1[h][8];
    	address[3]=str1[h][9];
    	address[4]=str1[h][10];
    	address[5]=str1[h][11];
    	address[6]=str1[h][12];
    	address[7]=str1[h][13];
    	address[8]=str1[h][14];
    	address[9]=str1[h][15];
    	address[10]=str1[h][16];
    	address[11]=str1[h][17];
    	address[12]=str1[h][18];
    	address[13]=str1[h][19];
    	address[14]=str1[h][20];
    	address[15]=str1[h][21];
    	address[16]=str1[h][22];
    	address[17]=str1[h][23];
    	address[18]=str1[h][24];
    	address[19]=str1[h][25];
    	address[20]=str1[h][26];
    	address[21]=str1[h][27];
    	address[22]=str1[h][28];
    	address[23]=str1[h][29];
    	address[24]=str1[h][30];
    	address[25]=str1[h][31];
    	address[26]='\0';
    	
    	func[0] = str1[h][26];
    	func[1] = str1[h][27];
    	func[2] = str1[h][28];
    	func[3] = str1[h][29];
    	func[4] = str1[h][30];
    	func[5] = str1[h][31];
    	func[6] = '\0';
    	
    	imm[0] = str1[h][16];
    	imm[1] = str1[h][17];
    	imm[2] = str1[h][18];
    	imm[3] = str1[h][19];
    	imm[4] = str1[h][20];
    	imm[5] = str1[h][21];
    	imm[6] = str1[h][22];
    	imm[7] = str1[h][23];
    	imm[8] = str1[h][24];
    	imm[9] = str1[h][25];
    	imm[10] = str1[h][26];
    	imm[11] = str1[h][27];
    	imm[12] = str1[h][28];
    	imm[13] = str1[h][29];
    	imm[14] = str1[h][30];
    	imm[15] = str1[h][31];
    	imm[16] = '\0';
    
           //Second set of one dimensional arrays
    
            char opcodeD[7];		
    	char rsD[6];
    	char rtD[6];
    	char funcD[7];
    	char addressD[27];
    	char immD[17];
    	char rdD[6];
    	char shamptD[6];
    
    
            opcodeD[0]=str1[h][0];
    	opcodeD[1]=str1[h][1];
    	opcodeD[2]=str1[h][2];
    	opcodeD[3]=str1[h][3];
    	opcodeD[4]=str1[h][4];
    	opcodeD[5]=str1[h][5];
    	opcodeD[6] = '\0';
    
           rsD[0]=str1[h][6];
    	rsD[1]=str1[h][7];
    	rsD[2]=str1[h][8];
    	rsD[3]=str1[h][9];
    	rsD[4]=str1[h][10];
    	rsD[5]='\0';
    	
    	rtD[0]=str1[h][11];
    	rtD[1]=str1[h][12];
    	rtD[2]=str1[h][13];
    	rtD[3]=str1[h][14];
    	rtD[4]=str1[h][15];
    	rtD[5]='\0';
    	
    	rdD[0]=str1[h][16];
    	rdD[1]=str1[h][17];
    	rdD[2]=str1[h][18];
    	rdD[3]=str1[h][19];
    	rdD[4]=str1[h][20];
    	rdD[5]='\0';
    	
    	shamptD[0]=str1[h][21];
    	shamptD[1]=str1[h][22];
    	shamptD[2]=str1[h][23];
    	shamptD[3]=str1[h][24];
    	shamptD[4]=str1[h][25];
    	shamptD[5]='\0';
    	
    	addressD[0]=str1[h][6];
    	addressD[1]=str1[h][7];
    	addressD[2]=str1[h][8];
    	addressD[3]=str1[h][9];
    	addressD[4]=str1[h][10];
    	addressD[5]=str1[h][11];
    	addressD[6]=str1[h][12];
    	addressD[7]=str1[h][13];
    	addressD[8]=str1[h][14];
    	addressD[9]=str1[h][15];
    	addressD[10]=str1[h][16];
    	addressD[11]=str1[h][17];
    	addressD[12]=str1[h][18];
    	addressD[13]=str1[h][19];
    	addressD[14]=str1[h][20];
    	addressD[15]=str1[h][21];
    	addressD[16]=str1[h][22];
    	addressD[17]=str1[h][23];
    	addressD[18]=str1[h][24];
    	addressD[19]=str1[h][25];
    	addressD[20]=str1[h][26];
    	addressD[21]=str1[h][27];
    	addressD[22]=str1[h][28];
    	addressD[23]=str1[h][29];
    	addressD[24]=str1[h][30];
    	addressD[25]=str1[h][31];
    	addressD[26]='\0';
    	
    	funcD[0] = str1[h][26];
    	funcD[1] = str1[h][27];
    	funcD[2] = str1[h][28];
    	funcD[3] = str1[h][29];
    	funcD[4] = str1[h][30];
    	funcD[5] = str1[h][31];
    	funcD[6] = '\0';
    	
    
    	immD[0] = str1[h][16];	
    	immD[1] = str1[h][17];
    	immD[2] = str1[h][18];
    	immD[3] = str1[h][19];
    	immD[4] = str1[h][20];
    	immD[5] = str1[h][21];
    	immD[6] = str1[h][22];
    	immD[7] = str1[h][23];
    	immD[8] = str1[h][24];
    	immD[9] = str1[h][25];
    	immD[10] = str1[h][26];
    	immD[11] = str1[h][27];
    	immD[12] = str1[h][28];
    	immD[13] = str1[h][29];
    	immD[14] = str1[h][30];
    	immD[15] = str1[h][31];
    	immD[16] = '\0';
      
         }
    If I delete comments or lines of code not needed for this part of the program it does not give me an Illegal instruction error at run-time, and runs like it is supposed to. Almost like its running out of memory or something

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    18
    Looking at the above code that makes no sense h will never by <= to 3 but somehow it is falling out of this loop. Im taking a closer look at the code hold on
    Last edited by FJRRulz; 04-26-2008 at 03:23 PM.

  5. #5
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,793
    is this compiled as C89, C99 or C++?
    why not to use memmove, i suppose it may make the code a little bit more compact and readable
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    David J. Wheeler

  6. #6
    Registered User
    Join Date
    Apr 2008
    Posts
    18
    Im pretty sure its C99, ive gotten an error before that references C99 mode. Definitely not C++. I fixed the loop problem but still getting the Illegal instruction error

  7. #7
    Registered User
    Join Date
    Apr 2008
    Posts
    18
    Ok I've narrowed it down to closing the input file. By placing a printf I know it is completing the loop and the only thing outside of the loop is the

    fclose(pFile);

    I just don't understand why though because it works fine when i don't try to copy the two dimensional array into 2 one dimensional arrays.

    I know I'm opening the input file correctly because I am printing values from the input file
    Last edited by FJRRulz; 04-26-2008 at 04:14 PM.

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,398
    You are overflowing an array. This is corrupting a return address, which is sending your code to la-la land (where all those "illegal instructions" exist)

    mind you that the size of the program is 1344 lines long, I'm not sure if this has anything to do with it.
    You're absolutely right -- all programs which are exactly 1344 lines long have this issue.

  9. #9
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    I don't spot the problem in this code, actually. And us C programmers are suspicious folk. Your line of code starts with a 13 as brewbuck noted. You need to write another 66 lines of code or else you will have 7 years of bad luck.

    Seriously though, post your whole code. There is obviously a buffer overflow somewhere. To explain the meaning of this as it pertains to a run-time error. The CPU is doing something illegal. Since your compiler was published after the scrutiny of bug testing, its safe to say your compiler didn't generate illegal opcodes. However, if your CPU is trying to execute your array, which as far as your CPU is concerned is just random non-sense, then yes, you will encounter plenty of illegal instructions.
    Last edited by master5001; 04-26-2008 at 06:37 PM.

  10. #10
    Banned master5001's Avatar
    Join Date
    Aug 2001
    Location
    Visalia, CA, USA
    Posts
    3,685
    Actually, assuming all things are equal here, you probably never correctly propagated the original array you are copying from. Thus you are suffering the woes of printf("&#37;s", my_invalid_pointer) with an invalid pointer.

    On a side note, ditch the 2d arrays. If you can't handle the math, just ask for help. I am always willing to aid someone trying to obsolete their 2d arrays.

  11. #11
    Registered User
    Join Date
    Apr 2008
    Posts
    18
    i wish I was overloading, I got it to stop throwing the error for awhile then after adding any kind of code (printf's, fprintf's, anything) it gives me the Illegal instruction error at run-time again. Sometimes it will throw a compilation error reading:

    1080:54: warning: no newline at end of file
    MIPS_CONV.c: In function 'main':
    MIPS_CONV.c:1080: error: syntax error at end of input

    and when it doesnt throw that error it throws the Illegal instruction error during run-time

  12. #12
    Registered User
    Join Date
    Apr 2008
    Posts
    18
    I'll admit im not using very good programing etiquette here but heres the program, it may be near impossible to figure out

  13. #13
    Registered User
    Join Date
    Apr 2008
    Posts
    18
    you'll need an input file

  14. #14
    Registered User
    Join Date
    Apr 2008
    Posts
    18
    It feels like I'm reaching some kind of boundary. It may be coincidence but if the program is 31KB it compiles and runs fine but as soon as I add another line of code and it reaches 32KB in size it starts throwing the errors.

  15. #15
    Registered User
    Join Date
    Apr 2008
    Posts
    18
    Ok I may have figured it out for now at least. I'm using my school's server and compiling it remotely. If I delete other programs from the server it no longer gives me the Illegal instruction error, I don't know if I exceeded some kind of limit on the server but that seems to have fixed it for now. I'll be back if it decides to throw me errors again.

    Thanks for the help, hopefully it wont happen again

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. failure to import external C libraries in C++ project
    By nocturna_gr in forum C++ Programming
    Replies: 3
    Last Post: 12-02-2007, 03:49 PM
  2. Crazy errors caused by class, never seen before..
    By Shamino in forum C++ Programming
    Replies: 2
    Last Post: 06-10-2007, 11:54 AM
  3. Dikumud
    By maxorator in forum C++ Programming
    Replies: 1
    Last Post: 10-01-2005, 06:39 AM
  4. Problem with Visual C++ Object-Oriented Programming Book.
    By GameGenie in forum C++ Programming
    Replies: 9
    Last Post: 08-29-2005, 11:21 PM
  5. Stupid compiler errors
    By ChrisEacrett in forum C++ Programming
    Replies: 9
    Last Post: 11-30-2003, 05:44 PM