   Error in my app at runtime :(
  Hi there! I compiled this code. It compiled fine but when I want to run it.. it just says that windows has detected a problem and it must close :( Anyone knows why it happened?This is the code:#include <SDL/SDL.h>#include <SDL/SDL_image.h>#include <string>const int SCREEN_WIDTH = 640;const int SCREEN_HEIGHT = 480;const int SCREEN_BPP = 32;SDL_Surface * screen = NULL;SDL_Surface * image = NULL;SDL_Surface * message = NULL;unsigned short int * x = NULL;unsigned short int * y = NULL;unsigned short int * updposX = NULL;unsigned short int * updposY = NULL;SDL_Event event;SDL_Surface *CargarImagen( std::string filename ){    SDL_Surface * imagenCargada = NULL;    SDL_Surface * imagenOptimizada = NULL;   imagenCargada = IMG_Load( filename.c_str() );   if ( imagenCargada != NULL )    {   imagenOptimizada = SDL_DisplayFormat( imagenCargada ); SDL_FreeSurface( imagenCargada );    }   return imagenOptimizada;}void AplicarSurface( int x, int y, SDL_Surface *source, SDL_Surface *destino ){    SDL_Rect offset;   offset.x = x;    offset.y = y;   SDL_BlitSurface( source, NULL, destino, &offset );}bool Init(){    if ( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )    {   return false;    }   screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );   if ( screen == NULL )    {   return false;    }   SDL_WM_SetCaption( "Test 1", NULL );   return true;}bool LoadFiles(){    image = CargarImagen( "background.bmp" );    message = CargarImagen( "hello_world.bmp" );   if ( image == NULL || message == NULL )    {   return false;    }   return true;}bool UpdateScreen(){    if ( *x == *updposX && *y == *updposY )    {   return false;    }    else    {   *updposX = *x;   *updposY = *y;    }   return true;}void CleanUp(){    SDL_FreeSurface( image );   SDL_Quit();}int main( int argc, char* args[] ){    bool quit = false;   *x = 180;    *y = 140;    *updposX = *x;    *updposY = *y; if ( Init() == false )    {   return 1;    }   if ( LoadFiles() == false )    {   return 1;    }   while ( quit == false )    {   if ( UpdateScreen() )   {  AplicarSurface( 0, 0, image, screen );  AplicarSurface( *updposX, *updposY, message, screen ); if ( SDL_Flip( screen ) == -1 )  { return 1;  }   }    SDL_PollEvent( &event );   switch ( event.type )   {  case SDL_QUIT:quit = true;break;  case SDL_KEYDOWN:switch ( event.key.keysym.sym ){    case SDLK_UP:    *y = *y + 3;    break;    case SDLK_DOWN:    *y = *y - 3;    break;    case SDLK_LEFT:    *x = *x - 3;    break;    case SDLK_RIGHT:    *x = *x + 3;    break;}break;   }    }    CleanUp();   return 0;} It must be an awful code.. but I want to experiment with SDL. Don't laugh of me :P Thanks!   
  OK, without going any further, the first thing that must be addressed is all your pointers.Why are you using so many?  I don't see any reason why x, y, updposX, nor updposY should be pointers.By using pointers in the fashion you're doing, there's a chance that you're assigning to incorrect adresses (which it appears from given a quick glance).My general rule, if you don't need pointers, don't use them.  They're good for lots of things, but they can be confusing, and almost always, are the major reason for bugs.*Quick Edit*Just trying to clean this up some, I don't have a compiler here at the time, so this code won't be perfect, but should be a little better.#include <SDL/SDL.h>#include <SDL/SDL_image.h>#include <string>const int SCREEN_WIDTH = 640;const int SCREEN_HEIGHT = 480;const int SCREEN_BPP = 32;SDL_Surface * screen = NULL;SDL_Surface * image = NULL;SDL_Surface * message = NULL;unsigned short int x = NULL;unsigned short int y = NULL;unsigned short int updposX = NULL;unsigned short int updposY = NULL;SDL_Event event;SDL_Surface *CargarImagen( std::string filename ){    SDL_Surface * imagenCargada = NULL;    SDL_Surface * imagenOptimizada = NULL;   imagenCargada = IMG_Load( filename.c_str() );   if ( imagenCargada != NULL )    {   imagenOptimizada = SDL_DisplayFormat( imagenCargada ); SDL_FreeSurface( imagenCargada );    }   return imagenOptimizada;}void AplicarSurface( int nx, int ny, SDL_Surface *source, SDL_Surface *destino ){    SDL_Rect offset;   offset.x = nx;    offset.y = ny;   SDL_BlitSurface( source, NULL, destino, &offset );}bool Init(){    if ( SDL_Init( SDL_INIT_EVERYTHING ) == -1 )    {   return false;    }   screen = SDL_SetVideoMode( SCREEN_WIDTH, SCREEN_HEIGHT, SCREEN_BPP, SDL_SWSURFACE );   if ( screen == NULL )    {   return false;    }   SDL_WM_SetCaption( "Test 1", NULL );   return true;}bool LoadFiles(){    image = CargarImagen( "background.bmp" );    message = CargarImagen( "hello_world.bmp" );   if ( image == NULL || message == NULL )    {   return false;    }   return true;}bool UpdateScreen(){    if ( x == updposX && y == updposY )    {   return false;    }    else    {   updposX = x;   updposY = y;    }   return true;}void CleanUp(){    SDL_FreeSurface( image );   SDL_Quit();}int main( int argc, char* args[] ){    bool quit = false;   x = 180;    y = 140;    updposX = x;    updposY = y; if ( Init() == false )    {   return 1;    }   if ( LoadFiles() == false )    {   return 1;    }   while ( quit == false )    {   if ( UpdateScreen() )   {  AplicarSurface( 0, 0, image, screen );  AplicarSurface( updposX, updposY, message, screen ); if ( SDL_Flip( screen ) == -1 )  { return 1;  }   }    SDL_PollEvent( &event );   switch ( event.type )   {  case SDL_QUIT:quit = true;break;  case SDL_KEYDOWN:switch ( event.key.keysym.sym ){    case SDLK_UP:    y = y + 3;    break;    case SDLK_DOWN:    y = y - 3;    break;    case SDLK_LEFT:    x = x - 3;    break;    case SDLK_RIGHT:    x = x + 3;    break;}break;   }    }    CleanUp();   return 0;} ;  Excellent advice from Nytegard.Also, I just have to comment on this. It appears that you are writing part of this code in Spansih and the other part in English. Am I just being stupid, or do you have good reasoning for this?CargarImagen sure sounds Spanish to me, and I know that CleanUp() is English.   ;  Try to put something like this every few lines:std::cout << "Got to: 1" << std::endl;And then change 1 everytime you put this.SO when your program quits, you know exactly where it quit.Or you can use a debugger.   ;  Thank u! i put some words in english because some of them are easier to read and smaller. Sorry, i should put them in english next time, i forgot it!Now the app runs ok! I should read about pointers a little more :) and I'll put some "cout's" to know where the app crashes too!thank u guys for your advices!   ;Quote:Original post by GutyGuThank u! i put some words in english because some of them are easier to read and smaller. Sorry, i should put them in english next time, i forgot it!Now the app runs ok! I should read about pointers a little more :) and I'll put some "cout's" to know where the app crashes too!thank u guys for your advices!Using a debugger is better. It will point you at the exact line your code fails at.And if for some reason you *have* to use the print statement method, try this instead:#define TRACE() { std::cout << "Got to line " << __LINE__ << std::endl; }Now you dont have to bother with changing the number the whole time...eg:TRACE();// stuffTRACE();// more stuff...   