Visit the LQ Articles and Editorials section
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


Search this Thread
Old 04-12-2011, 06:12 PM   #1
Registered: Nov 2010
Location: Bristol, UK
Distribution: Slackware, FreeBSD
Posts: 278

Rep: Reputation: 75
C++/SDL segfault when using SDL_Flip()

Hello, i have recently decided to start learning some c++ as with previous languages, i feel its easier to understand "how things work" by creating small games or animations.

I have come across this tutorial on SDL here:

And here is the code i have put together after following that: (for the sake of syntax highlighting )

And compiled with:
g++ main.cpp -o main `sdl-config --libs --cflags`
Now for the issue, i can't seem to fix this error:
% ./main                                           
SDL Drawing Test!
zsh: segmentation fault  ./main
When i run it under gdb, the output shows:
% gdb main 
GNU gdb (GDB) 7.2
Copyright (C) 2010 Free Software Foundation, Inc.
License GPLv3+: GNU GPL version 3 or later <>
This is free software: you are free to change and redistribute it.
There is NO WARRANTY, to the extent permitted by law.  Type "show copying"
and "show warranty" for details.
This GDB was configured as "x86_64-unknown-linux-gnu".
For bug reporting instructions, please see:
Reading symbols from /home/ricky/Devel/cpp-sdl/main...(no debugging symbols found)...done.
(gdb) run
Starting program: /home/ricky/Devel/cpp-sdl/main 
[Thread debugging using libthread_db enabled]
SDL drawing test started!
[New Thread 0x7fffee678700 (LWP 23754)]
[Thread 0x7fffee678700 (LWP 23754) exited]

Program received signal SIGSEGV, Segmentation fault.
0x00007ffff7b719f8 in SDL_Flip () from /usr/lib/
The only place i have SDL_Flip() mentioned is inside DrawScene(), i tried commenting out that line, obviously the application only then shows a black screen.

Any help explaining why this is segfaulting would be awesome, thanks.
Old 04-12-2011, 06:16 PM   #2
Registered: Nov 2010
Location: Bristol, UK
Distribution: Slackware, FreeBSD
Posts: 278

Original Poster
Rep: Reputation: 75
Oh wow i found out i redeclared:
SDL_Surface *screen;
in main()

Removed it and now it works!

Solved i guess.
Old 04-12-2011, 06:41 PM   #3
Senior Member
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 111Reputation: 111
GCC can warn you whenever you "shadow" a variable like this I forget the exact keyword, but it's included with -Wall -Werror options I always compile with (it's got plenty of other bug-catching goodies too).
1 members found this post helpful.
Old 04-12-2011, 06:59 PM   #4
Registered: Nov 2010
Location: Bristol, UK
Distribution: Slackware, FreeBSD
Posts: 278

Original Poster
Rep: Reputation: 75
I think i'll be using that all the time from now on:
% g++ main.cpp -o main `sdl-config --libs --cflags` -Wshadow
main.cpp: In function ‘int main(int, char**)’:
main.cpp:109:15: warning: declaration of ‘screen’ shadows a global declaration
main.cpp:29:14: warning: shadowed declaration is here
Very Helpful, Cheers.


cpp, sdl, segfault

Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
SDL is installed, included and linked, but will not compile SDL code mansizerooster Programming 10 05-31-2006 04:18 AM
Sourcecompiling Problem...The sdl-config script installed by SDL could not be found. deepclutch Debian 1 12-15-2005 12:15 PM
Trying to program an SDL application but cannot find the SDL.h file:SuSE 9.2&KDevelop pujolasdf Linux - Newbie 4 03-13-2005 07:50 AM
SDL_Flip() Vs SDL_UpdateRects() MadCactus Programming 1 10-10-2004 04:26 PM
SDL Error: /dev/snd/pcmC0D0p - "busy" = segfault tireseas Linux - Software 0 07-18-2004 11:35 AM

All times are GMT -5. The time now is 05:58 AM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration