3 Replies - 292 Views - Last Post: 07 September 2012 - 10:42 PM Rate Topic: -----

#1 lincoln  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 08-April 12

C++ SDL UpperBlit Segfault

Posted 07 September 2012 - 04:23 PM

After seeing braid, for the sake of curiosity I wanted to play around and see if I can code a similar time changing mechanic. I set this little program up quick and dirty just for the purpose of writing this one feature, so I apologize the code isn't incredibly clean. It returns a segfault right away. Here's the code:
#include "SDL/SDL.h"

int main(int argc, char** args) 
{
	SDL_Init(SDL_INIT_EVERYTHING);
	
	SDL_Surface* screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
	SDL_Surface* better = SDL_LoadBMP("test.bmp");
	SDL_Rect offsets;
	offsets.x = 30.5;
	offsets.y = 30;
	SDL_BlitSurface(better, NULL, screen, NULL);
	Uint8* keystates = SDL_GetKeyState(NULL);
		
	SDL_Event event;
	bool quit = false;
	
	int a = 0;
	int b = 0;
	
	int xslot[a];
	int yslot[b];
	
	while(quit == false)
	{
		while(SDL_PollEvent(&event))
		{
			switch(event.type)
			{
				case SDL_QUIT:
					quit = true;
				break;
			}
		}
		
		xslot[a] = offsets.x;
		yslot[b] = offsets.y;
		a++;
		b++;
		
		if(keystates[SDLK_LEFT])
		{
			SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
			offsets.x -= 1;
			
		}
		if(keystates[SDLK_RIGHT])
		{
			SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
			offsets.x += 1;
		}
		if(keystates[SDLK_UP]) {
			SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
			offsets.y -= 1;
		}
		if(keystates[SDLK_DOWN]) {
			SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
			offsets.y += 1;
		}
		if(keystates[SDLK_LSHIFT])
		{
	
			SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
			offsets.x = xslot[a];
			offsets.y = yslot[b];
			a--;
			b--;
		}
		SDL_BlitSurface(better, NULL, screen, &offsets);
		SDL_Flip(screen);
	}
	SDL_FreeSurface(better);
	SDL_FreeSurface(screen);
	SDL_Quit();
}



Here's what the gdb gave me:

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7b71ce7 in SDL_UpperBlit ()
   from /usr/lib/x86_64-linux-gnu/libSDL-1.2.so.0



What's going on here?

Is This A Good Question/Topic? 0
  • +

Replies To: C++ SDL UpperBlit Segfault

#2 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 988
  • View blog
  • Posts: 3,436
  • Joined: 19-February 09

Re: C++ SDL UpperBlit Segfault

Posted 07 September 2012 - 06:02 PM

Hi, this looks as if you are creating two arrays of zero size.

18	    int a = 0;
19	    int b = 0;
20	     
21	    int xslot[a];
22	    int yslot[b];


Was This Post Helpful? 0
  • +
  • -

#3 lincoln  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 8
  • Joined: 08-April 12

Re: C++ SDL UpperBlit Segfault

Posted 07 September 2012 - 06:37 PM

Zero counts as the first slot in an array. So, the x offsets of a player would be put into slot 0, then the next time around the loop it would go to 1 for the next set of offsets to be stored.
Was This Post Helpful? 0
  • +
  • -

#4 Salem_c  Icon User is offline

  • void main'ers are DOOMED
  • member icon

Reputation: 1418
  • View blog
  • Posts: 2,681
  • Joined: 30-May 10

Re: C++ SDL UpperBlit Segfault

Posted 07 September 2012 - 10:42 PM

You don't have any useful arrays at all.
21      int xslot[a];
22      int yslot[b];


is just
21      int xslot[0];
22      int yslot[0];


Every subscript (even 0) is out of range.

Then there is this:
38	        a++;
39	        b++;

Where is this range checked?
What stops this incrementing to oblivion?
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1