Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Illegal Instruction When Programming C++ on Linux

Tags:

c++

gcc

g++

My program, which does exactly the same thing every time it runs (moves a point sprite into the distance) will randomly fail with the text on the terminal 'Illegal Instruction'. My googling has found people encountering this when writing assembly which makes sense because assembly throws those kinds of errors.

But why would g++ be generating an illegal instruction like this? It's not like I'm compiling for Windows then running on Linux (which even then, as long as both are on x86 shouldn't AFAIK cause an Illegal Instruction). I'll post the main file below.

I can't reliably reproduce the error. Although, if I make random changes (add a space here, change a constant there) that force a recompile I can get a binary which will fail with Illegal Instruction every time it is run, until I try setting a break point, which makes the illegal instruction 'dissapear'. :(

#include <stdio.h>
#include <stdlib.h> 
#include <GL/gl.h>
#include <GL/glu.h>

#include <SDL/SDL.h>

#include "Screen.h"  //Simple SDL wrapper
#include "Textures.h" //Simple OpenGL texture wrapper 
#include "PointSprites.h" //Simple point sprites wrapper


double counter = 0;
/* Here goes our drawing code */
int drawGLScene()
{
    /* These are to calculate our fps */
    static GLint T0     = 0;
    static GLint Frames = 0;

    /* Move Left 1.5 Units And Into The Screen 6.0 */
    glLoadIdentity();
 glTranslatef(0.0f, 0.0f, -6);
 glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT);

 glEnable(GL_POINT_SPRITE_ARB);
 glTexEnvi(GL_POINT_SPRITE, GL_COORD_REPLACE, GL_TRUE);
    glBegin( GL_POINTS );   /* Drawing Using Triangles */
 glVertex3d(0.0,0.0, 0);
 glVertex3d(1.0,0.0, 0);
 glVertex3d(1.0,1.0, counter);
 glVertex3d(0.0,1.0, 0);
    glEnd( );                           /* Finished Drawing The Triangle */

    /* Move Right 3 Units */


    /* Draw it to the screen */

    SDL_GL_SwapBuffers( );
    /* Gather our frames per second */
    Frames++;
    {
 GLint t = SDL_GetTicks();
 if (t - T0 >= 50) {
     GLfloat seconds = (t - T0) / 1000.0;
     GLfloat fps = Frames / seconds;
     printf("%d frames in %g seconds = %g FPS\n", Frames, seconds, fps);
     T0 = t;
     Frames = 0;
  counter -= .1;
 }
    }

    return 1;
}

GLuint objectID;
int main( int argc, char **argv )
{
 Screen screen;
 screen.init();
 screen.resize(800,600);


 LoadBMP("./dist/Debug/GNU-Linux-x86/particle.bmp");
 InitPointSprites();


 while(true){drawGLScene();} 
}
like image 444
user327406 Avatar asked Jun 12 '10 20:06

user327406


2 Answers

The compiler isn't generating illegal exceptions, with 99.99% probability. Almost certainly what's happening is that you have a bug in your program which is causing it to either a) overwrite parts of your executable code with garbage data, or b) use a function pointer that points into garbage. Try running your program under valgrind to diagnose the problem - http://valgrind.org/.

like image 131
JSBձոգչ Avatar answered Nov 20 '22 13:11

JSBձոգչ


The Illegal Instruction bug can also be a symptom of a faulty graphics card driver, or one that's mismatched to the hardware. Use lspci | grep VGA to confirm what your hardware actually is. Then try downloading the latest & greatest driver for your hardware model.

There is also a known bug when running code from inside NetBeans 6.8 on a multi-core 64-bit machine. The code crashes stochastically with Illegal Instruction based on race conditions in the profiler. Per cent of crashes varies from 1% or 5% for some code, 30% or 50%, up to around 95%+, depending on which libraries are being loaded. Graphics and threads code seems to increase this, but you can see it with a trivial Hello World main. If you get a 1% crash rate, you probably haven't noticed it before. Solution: run the executable straight from a terminal, if you can.

like image 34
DragonLord Avatar answered Nov 20 '22 12:11

DragonLord