Seg fault despite Extern screen

Hi
I have some C experience but am also a gcc/SDL/gp2x/CodeBlocks NOOB. I am
trying to setup a GP2X dev environment on windows. I found a ready to go
gp2x toolchain including CB and am able to build a simple SDL demo app for
windows (and gp2x). This runs OK

I am trying to mix in some SDL filebrowser code that I found. It
compiles/links but seg faults when the filebrowser code tries to access
fields in the screen pointer object.

The filebrowser code does a :- extern SDL_Surface *screen;
(like it should)

so the screen pointer declared (statically) in the main file (and
initialised) should be accessible in the filebrowser code. But in the
debugger I see that the screen pointer is undefined while in the filebrowser
code.

How can this be?

Thanks
Peter Cutting

I found the bug. The demo declared screen twice. Once statically and once in
main(). So the one in main was initialised but the static one (used by the
filebrowser code) was not.
PeterOn Feb 19, 2008 2:09 PM, Peter Cutting <@Peter_Cutting> wrote:

Hi
I have some C experience but am also a gcc/SDL/gp2x/CodeBlocks NOOB. I am
trying to setup a GP2X dev environment on windows. I found a ready to go
gp2x toolchain including CB and am able to build a simple SDL demo app for
windows (and gp2x). This runs OK

I am trying to mix in some SDL filebrowser code that I found. It
compiles/links but seg faults when the filebrowser code tries to access
fields in the screen pointer object.

The filebrowser code does a :- extern SDL_Surface *screen;
(like it should)

so the screen pointer declared (statically) in the main file (and
initialised) should be accessible in the filebrowser code. But in the
debugger I see that the screen pointer is undefined while in the filebrowser
code.

How can this be?

Thanks
Peter Cutting

So the toolchain you’re using is based on gcc? If so, you can enable
warnings to let you know ahead of time about such things:

— foo.c —
#include <SDL.h>

SDL_Surface *screen;

int main(void)
{
SDL_Surface *screen;
return 0;
}
— end —

$ gcc -c foo.c sdl-config --cflags
(no output)

vs.

$ gcc -Wshadow -c foo.c sdl-config --cflags
foo.c: In function ‘main’:
foo.c:7: warning: declaration of ‘screen’ shadows a global declaration
foo.c:3: warning: shadowed declaration is here

Of course you wouldn’t know ahead of time that -Wshadow would tell you
about your specific problem, but if that’s the kind of thing that
periodically gives you trouble, you may want to leave that flag on.
The gcc man page has info about that flag, and many others you may
want to consider using during development.

-MikeOn 2/19/08, Peter Cutting <peter.cutting at gmail.com> wrote:

I found the bug. The demo declared screen twice. Once statically and once in
main(). So the one in main was initialised but the static one (used by the
filebrowser code) was not.