Skip to main content
Tweeted twitter.com/#!/StackGameDev/status/178218112519380992
edited title
Link
user1430
user1430

When I add a variable to my code, Why do I get a segmentation fault on launch with this code?

#ifdef __cplusplus #include <cstdlib> #else #include <stdlib.h> #endif #ifdef __APPLE__ #include <SDL/SDL.h> #else #include <SDL.h> #endif #include "SDL/SDL_image.h" #include "SDL/SDL_framerate.h" #include "checkcol.h" Uint8 *keystates = SDL_GetKeyState( NULL ); int main ( int argc, char** argv ) {  // initialize SDL video  if ( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )  {   printf( "Unable to init SDL: %s\n", SDL_GetError() );   return 1;  }  atexit(SDL_Quit);// make sure SDL cleans up before exit  // create a new window  SDL_Surface* screen = SDL_SetVideoMode(640, 480, 16,SDL_HWSURFACE|SDL_DOUBLEBUF);  if ( !screen )  {   printf("Unable to set 640x480 video: %s\n", SDL_GetError());   return 1;  }  SDL_Surface* left = IMG_Load("left.png");  SDL_Surface* right = IMG_Load("right.png");  SDL_Surface* ball = IMG_Load("ball.png");  if (!left||!right||!ball)  {   printf("Unable to load a png: %s\n", SDL_GetError());   return 1;  }  // centre the bitmap on screen  SDL_Rect leftr;  leftr.x = 0;  leftr.y = (screen->h - left->h) / 2;  SDL_Rect rightr;  rightr.x = (screen->w - right->w);  rightr.y = (screen->h - right->h) / 2;  SDL_Rect ball1;  ball1.x = ((screen->w - ball->w) / 2)+50;  ball1.y = (screen->h - ball->h) / 2;  SDL_Rect ball2;  ball2.x = ((screen->w - ball->w) / 2)-50;  ball2.y = (screen->h - ball->h) / 2;  bool b1xm=0;//describes ball 1's movement in x  bool b1ym=0;  bool b2xm=1;  bool b2ym=1;  unsigned short lscore=0;  unsigned short rscore=0;  FPSmanager * manex;  SDL_initFramerate( manex );  // you want the top framerate to be 60 for example  SDL_setFramerate( manex, 60 );  // program main loop  bool done = false;  while (!done)  {   //this is where my main code is  } // end main loop  // free loaded images  SDL_FreeSurface(left);  SDL_FreeSurface(right);  SDL_FreeSurface(ball);  // all is well  printf("Exited cleanly\n");  return 0; } 

}

#ifdef __cplusplus #include <cstdlib> #else #include <stdlib.h> #endif #ifdef __APPLE__ #include <SDL/SDL.h> #else #include <SDL.h> #endif #include "SDL/SDL_image.h" #include "SDL/SDL_framerate.h" #include "checkcol.h" Uint8 *keystates = SDL_GetKeyState( NULL ); int main ( int argc, char** argv ) { // initialize SDL video if ( SDL_Init( SDL_INIT_EVERYTHING ) < 0 ) { printf( "Unable to init SDL: %s\n", SDL_GetError() ); return 1; } atexit(SDL_Quit);// make sure SDL cleans up before exit // create a new window SDL_Surface* screen = SDL_SetVideoMode(640, 480, 16,SDL_HWSURFACE|SDL_DOUBLEBUF); if ( !screen ) { printf("Unable to set 640x480 video: %s\n", SDL_GetError()); return 1; } SDL_Surface* left = IMG_Load("left.png"); SDL_Surface* right = IMG_Load("right.png"); SDL_Surface* ball = IMG_Load("ball.png"); if (!left||!right||!ball) { printf("Unable to load a png: %s\n", SDL_GetError()); return 1; } // centre the bitmap on screen SDL_Rect leftr; leftr.x = 0; leftr.y = (screen->h - left->h) / 2; SDL_Rect rightr; rightr.x = (screen->w - right->w); rightr.y = (screen->h - right->h) / 2; SDL_Rect ball1; ball1.x = ((screen->w - ball->w) / 2)+50; ball1.y = (screen->h - ball->h) / 2; SDL_Rect ball2; ball2.x = ((screen->w - ball->w) / 2)-50; ball2.y = (screen->h - ball->h) / 2; bool b1xm=0;//describes ball 1's movement in x bool b1ym=0; bool b2xm=1; bool b2ym=1; unsigned short lscore=0; unsigned short rscore=0; FPSmanager * manex; SDL_initFramerate( manex ); // you want the top framerate to be 60 for example SDL_setFramerate( manex, 60 ); // program main loop bool done = false; while (!done) { //this is where my main code is } // end main loop // free loaded images SDL_FreeSurface(left); SDL_FreeSurface(right); SDL_FreeSurface(ball); // all is well printf("Exited cleanly\n"); return 0; 

}

#ifdef __cplusplus #include <cstdlib> #else #include <stdlib.h> #endif #ifdef __APPLE__ #include <SDL/SDL.h> #else #include <SDL.h> #endif #include "SDL/SDL_image.h" #include "SDL/SDL_framerate.h" #include "checkcol.h" Uint8 *keystates = SDL_GetKeyState( NULL ); int main ( int argc, char** argv ) {  // initialize SDL video  if ( SDL_Init( SDL_INIT_EVERYTHING ) < 0 )  {   printf( "Unable to init SDL: %s\n", SDL_GetError() );   return 1;  }  atexit(SDL_Quit);// make sure SDL cleans up before exit  // create a new window  SDL_Surface* screen = SDL_SetVideoMode(640, 480, 16,SDL_HWSURFACE|SDL_DOUBLEBUF);  if ( !screen )  {   printf("Unable to set 640x480 video: %s\n", SDL_GetError());   return 1;  }  SDL_Surface* left = IMG_Load("left.png");  SDL_Surface* right = IMG_Load("right.png");  SDL_Surface* ball = IMG_Load("ball.png");  if (!left||!right||!ball)  {   printf("Unable to load a png: %s\n", SDL_GetError());   return 1;  }  // centre the bitmap on screen  SDL_Rect leftr;  leftr.x = 0;  leftr.y = (screen->h - left->h) / 2;  SDL_Rect rightr;  rightr.x = (screen->w - right->w);  rightr.y = (screen->h - right->h) / 2;  SDL_Rect ball1;  ball1.x = ((screen->w - ball->w) / 2)+50;  ball1.y = (screen->h - ball->h) / 2;  SDL_Rect ball2;  ball2.x = ((screen->w - ball->w) / 2)-50;  ball2.y = (screen->h - ball->h) / 2;  bool b1xm=0;//describes ball 1's movement in x  bool b1ym=0;  bool b2xm=1;  bool b2ym=1;  unsigned short lscore=0;  unsigned short rscore=0;  FPSmanager * manex;  SDL_initFramerate( manex );  // you want the top framerate to be 60 for example  SDL_setFramerate( manex, 60 );  // program main loop  bool done = false;  while (!done)  {   //this is where my main code is  } // end main loop  // free loaded images  SDL_FreeSurface(left);  SDL_FreeSurface(right);  SDL_FreeSurface(ball);  // all is well  printf("Exited cleanly\n");  return 0; } 
Source Link
earboxer
  • 121
  • 1
  • 3

When I add a variable to my code, I get a segmentation fault on launch

I am currently writing a pong clone in C++ with SDL on top. Currently, I have hit a roadblock. If I add a new variable, my game won't launch, and will give "segmentation fault" in the debug terminal.

When I comment out a variable, the following code gives no errors, but otherwise, it just returns "Segmentation Fault" on the debug terminal after flashing the frame the game would normally appear in.

#ifdef __cplusplus #include <cstdlib> #else #include <stdlib.h> #endif #ifdef __APPLE__ #include <SDL/SDL.h> #else #include <SDL.h> #endif #include "SDL/SDL_image.h" #include "SDL/SDL_framerate.h" #include "checkcol.h" Uint8 *keystates = SDL_GetKeyState( NULL ); int main ( int argc, char** argv ) { // initialize SDL video if ( SDL_Init( SDL_INIT_EVERYTHING ) < 0 ) { printf( "Unable to init SDL: %s\n", SDL_GetError() ); return 1; } atexit(SDL_Quit);// make sure SDL cleans up before exit // create a new window SDL_Surface* screen = SDL_SetVideoMode(640, 480, 16,SDL_HWSURFACE|SDL_DOUBLEBUF); if ( !screen ) { printf("Unable to set 640x480 video: %s\n", SDL_GetError()); return 1; } SDL_Surface* left = IMG_Load("left.png"); SDL_Surface* right = IMG_Load("right.png"); SDL_Surface* ball = IMG_Load("ball.png"); if (!left||!right||!ball) { printf("Unable to load a png: %s\n", SDL_GetError()); return 1; } // centre the bitmap on screen SDL_Rect leftr; leftr.x = 0; leftr.y = (screen->h - left->h) / 2; SDL_Rect rightr; rightr.x = (screen->w - right->w); rightr.y = (screen->h - right->h) / 2; SDL_Rect ball1; ball1.x = ((screen->w - ball->w) / 2)+50; ball1.y = (screen->h - ball->h) / 2; SDL_Rect ball2; ball2.x = ((screen->w - ball->w) / 2)-50; ball2.y = (screen->h - ball->h) / 2; bool b1xm=0;//describes ball 1's movement in x bool b1ym=0; bool b2xm=1; bool b2ym=1; unsigned short lscore=0; unsigned short rscore=0; FPSmanager * manex; SDL_initFramerate( manex ); // you want the top framerate to be 60 for example SDL_setFramerate( manex, 60 ); // program main loop bool done = false; while (!done) { //this is where my main code is } // end main loop // free loaded images SDL_FreeSurface(left); SDL_FreeSurface(right); SDL_FreeSurface(ball); // all is well printf("Exited cleanly\n"); return 0; 

}