title: SDL_SetPaletteColors crashes program
body: <p>The program is supposed take pixel data from a uni dimensional array and display it.  The pixel data is supposed to be 1 byte per pixel, which is supposed to result in a gray scale image.</p>
  
  <p>The result is supposed to look like this:</p>
  
  <p><img src="https://i.stack.imgur.com/vDYr2.jpg" alt="GRADIENT PATTERN">
  <br></p>
  
  <p>Th problem i run into is that the "SDL_SetPaletteColors" command crashes the program.</p>
  
  <p>What am I doing wrong here?</p>
  
  <p>Here is the code:</p>
  
  <pre><code>#include &lt;SDL.h&gt;
  #include &lt;stdio.h&gt;
  
  //Screen dimension constants
  const int SCREEN_WIDTH = 100;
  const int SCREEN_HEIGHT = 100;
  
  char* pixels;
  int icnt,icnt2;
  
  
  
  //Starts up SDL and creates window
  bool init();
  
  //Frees media and shuts down SDL
  void close();
  
  //The window we'll be rendering to
  SDL_Window* gWindow = NULL;
  
  //The surface contained by the window
  SDL_Surface* gScreenSurface = NULL;
  
  //The image we will load and show on the screen
  //SDL_Surface* gHelloWorld = NULL;
  SDL_Surface* gHelloWorld = NULL;
  
  
  
  
  bool init()
  {
      //Initialization flag
      bool success = true;
  
      //Initialize SDL
      if( SDL_Init( SDL_INIT_VIDEO ) &lt; 0 )
      {
          printf( "SDL could not initialize! SDL_Error: %s\n", SDL_GetError() );
          success = false;
      }
      else
      {
          //Create window
          gWindow = SDL_CreateWindow( "SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN );
          if( gWindow == NULL )
          {
              printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
              success = false;
          }
          else
          {
              //Get window surface
              gScreenSurface = SDL_GetWindowSurface( gWindow );
          }
      }
  
      return success;
  }
  
  
  
  void close()
  {
      //Deallocate surface
      SDL_FreeSurface( gHelloWorld );
      gHelloWorld = NULL;
  
      //Destroy window
      SDL_DestroyWindow( gWindow );
      gWindow = NULL;
  
      //Quit SDL subsystems
      SDL_Quit();
  }
  
  int main( int argc, char* args[] )
  {
      pixels = new char[10000];      //pixel array
      icnt2=0;
      SDL_Color colors[256];
  
      for(icnt=0;icnt&lt;10000;icnt++)  //gradient test image generator
      {
        pixels[icnt]=(char)icnt2;
        icnt2++;
        if(icnt2 == 99){icnt2 =0;}
      }
  
  
  
      //Start up SDL and create window
      if( !init() )
      {
          printf( "Failed to initialize!\n" );
      }
      else
      {
  
              gHelloWorld = SDL_CreateRGBSurfaceFrom((void*)pixels,
              100,
              100,
              8,
              100,
              0x000000FF,
              0x0000FF00,
              0x00FF0000,
              0);
  
              for(icnt = 0; icnt &lt; 255; icnt++)
              {
                  colors[icnt].r = colors[icnt].g = colors[icnt].b = icnt;
              }
  
              //program crashes here   ------------------
              SDL_SetPaletteColors(gHelloWorld-&gt;format-&gt;palette, colors, 0, 255);   //program crashes here
              //-----------------------------------------
  
              //Apply the image
              SDL_BlitSurface( gHelloWorld, NULL, gScreenSurface, NULL );
  
              //Update the surface
              SDL_UpdateWindowSurface( gWindow );
  
              //Wait two seconds
              SDL_Delay( 2000 );
          //}
      }
  
      //Free resources and close SDL
      close();
  
      return 0;
  }
  </code></pre>