# Newbie question

**URL:** <https://discourse.libsdl.org/t/newbie-question/5971>\
**Category:** SDL Development\
**Created:** [February 5, 2002, 4:52pm UTC](https://discourse.libsdl.org/t/newbie-question/5971 "2002-02-05T16:52:01Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![Thiago\_Silva\_Conceic](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/t/87869e/32.png) [@Thiago\_Silva\_Conceic](https://discourse.libsdl.org/u/Thiago_Silva_Conceic)\
**Post date:** [February 5, 2002, 4:52pm UTC](https://discourse.libsdl.org/t/newbie-question/5971/1 "2002-02-05T16:52:01Z")

</div>

Hi,

```
I have been playing around with SDL for a few days, trying out some 

```

examples and modifying it. But I am stuck at an error msg that happens  
when I run the program:

$ gcc -Wall main.c -o teste -lSDL -lpthread  
$ ./teste  
Fatal signal: Segmentation Fault (SDL Parachute Deployed)

```
I saw on other messages in the mailing lists about putting a lot of 

```

printfs to see if the functions return some error msg, I did it but even  
so it does not show anything besides that message.

```
Can anyone help me out?

```

Thiago

//--------------------------------------------------------- The Code

#include \<SDL/SDL.h\>  
#include \<stdio.h\>  
#include \<stdlib.h\>

SDL\_Surface \*screen, \*scene, \*doll;

// Vari?veis da personagem  
SDL\_Rect \*pers;  
short int doll\_xvel = 0, doll\_yvel = 0;

// Vari?veis de controle  
SDL\_Event event;  
short int quit = 0;

int game\_init()  
{  
if ((SDL\_Init(SDL\_INIT\_VIDEO)) != 0)  
{  
fprintf(stderr, “N?o foi poss?vel inicializar o SDL: %s.\n”,  
SDL\_GetError());  
exit(1);  
}

if ((screen = SDL\_SetVideoMode(800, 600, 24, SDL\_HWSURFACE |  
SDL\_FULLSCREEN | SDL\_DOUBLEBUF)) == 0)  
fprintf(stderr, “Erro ao setar a v?deo: %s.\n”, SDL\_GetError());

return(0);  
}

int game\_update()  
{

// Carregando o cen?rio

if ((scene = SDL\_LoadBMP(“cenario.bmp”)) == NULL)  
fprintf(stderr, “Erro ao carregar o cen?rio: %s.\n”, SDL\_GetError());

if ((SDL\_BlitSurface(scene, NULL, screen, NULL)) \< 0)  
fprintf(stderr, “Erro ao blit o cen?rio: %s.\n”, SDL\_GetError());  
SDL\_UpdateRect(screen, 0, 0, scene-\>w, scene-\>h);

// Carregando a personagem

if ((doll = SDL\_LoadBMP(“pers.bmp”)) == NULL)  
fprintf(stderr, “Erro ao carregar a personagem: %s.\n”, SDL\_GetError());

pers-\>x = (screen-\>w / 2) - 80;  
pers-\>y = (screen-\>h / 2) - 60;  
pers-\>w = doll-\>w;  
pers-\>h = doll-\>h;

if ((SDL\_BlitSurface(doll, NULL, screen, pers)) \< 0)  
fprintf(stderr, “Erro ao blit a personagem: %s.\n”, SDL\_GetError());  
SDL\_UpdateRect(screen, pers-\>x, pers-\>y, pers-\>w, pers-\>h);

return(0);  
}

int game\_quit()  
{  
SDL\_FreeSurface(screen);  
SDL\_FreeSurface(scene);  
SDL\_FreeSurface(doll);  
SDL\_Quit();  
return(0);  
}

int main()  
{  
game\_init();  
game\_update();  
game\_quit();  
return(0);  
}

---

<div class="post-metadata">

**Author:** ![Terry\_Grantges](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/t/b782af/32.png) [@Terry\_Grantges](https://discourse.libsdl.org/u/Terry_Grantges)\
**Post date:** [February 5, 2002, 5:56pm UTC](https://discourse.libsdl.org/t/newbie-question/5971/2 "2002-02-05T17:56:01Z")

</div>

You are trying to set a value to members of the SDL\_Rect \*pers when you have  
not allocated memory for pers.On Tuesday 05 February 2002 07:49 pm, you wrote:

> Hi,
> 
> ```
> I have been playing around with SDL for a few days, trying out some
> 
> ```
> 
> examples and modifying it. But I am stuck at an error msg that happens  
> when I run the program:
> 
> $ gcc -Wall main.c -o teste -lSDL -lpthread  
> $ ./teste  
> Fatal signal: Segmentation Fault (SDL Parachute Deployed)
> 
> ```
> I saw on other messages in the mailing lists about putting a lot of
> 
> ```
> 
> printfs to see if the functions return some error msg, I did it but even  
> so it does not show anything besides that message.
> 
> ```
> Can anyone help me out?
> 
> ```
> 
> Thiago
> 
> //--------------------------------------------------------- The Code
> 
> #include \<SDL/SDL.h\>  
> #include \<stdio.h\>  
> #include \<stdlib.h\>
> 
> SDL\_Surface \*screen, \*scene, \*doll;
> 
> // Vari?veis da personagem  
> SDL\_Rect \*pers;  
> short int doll\_xvel = 0, doll\_yvel = 0;
> 
> // Vari?veis de controle  
> SDL\_Event event;  
> short int quit = 0;
> 
> int game\_init()  
> {  
> if ((SDL\_Init(SDL\_INIT\_VIDEO)) != 0)  
> {  
> fprintf(stderr, “N?o foi poss?vel inicializar o SDL: %s.\n”,  
> SDL\_GetError());  
> exit(1);  
> }
> 
> if ((screen = SDL\_SetVideoMode(800, 600, 24, SDL\_HWSURFACE |  
> SDL\_FULLSCREEN | SDL\_DOUBLEBUF)) == 0)  
> fprintf(stderr, “Erro ao setar a v?deo: %s.\n”, SDL\_GetError());
> 
> return(0);  
> }
> 
> int game\_update()  
> {
> 
> // Carregando o cen?rio
> 
> if ((scene = SDL\_LoadBMP(“cenario.bmp”)) == NULL)  
> fprintf(stderr, “Erro ao carregar o cen?rio: %s.\n”, SDL\_GetError());
> 
> if ((SDL\_BlitSurface(scene, NULL, screen, NULL)) \< 0)  
> fprintf(stderr, “Erro ao blit o cen?rio: %s.\n”, SDL\_GetError());  
> SDL\_UpdateRect(screen, 0, 0, scene-\>w, scene-\>h);
> 
> // Carregando a personagem
> 
> if ((doll = SDL\_LoadBMP(“pers.bmp”)) == NULL)  
> fprintf(stderr, “Erro ao carregar a personagem: %s.\n”,  
> SDL\_GetError());
> 
> pers-\>x = (screen-\>w / 2) - 80;  
> pers-\>y = (screen-\>h / 2) - 60;  
> pers-\>w = doll-\>w;  
> pers-\>h = doll-\>h;
> 
> if ((SDL\_BlitSurface(doll, NULL, screen, pers)) \< 0)  
> fprintf(stderr, “Erro ao blit a personagem: %s.\n”, SDL\_GetError());  
> SDL\_UpdateRect(screen, pers-\>x, pers-\>y, pers-\>w, pers-\>h);
> 
> return(0);  
> }
> 
> int game\_quit()  
> {  
> SDL\_FreeSurface(screen);  
> SDL\_FreeSurface(scene);  
> SDL\_FreeSurface(doll);  
> SDL\_Quit();  
> return(0);  
> }
> 
> int main()  
> {  
> game\_init();  
> game\_update();  
> game\_quit();  
> return(0);  
> }
> 
> * * *
> 
> SDL mailing list  
> SDL at [libsdl.org](http://libsdl.org)  
> [http://www.libsdl.org/mailman/listinfo/sdl](http://www.libsdl.org/mailman/listinfo/sdl)

---

<div class="post-metadata">

**Author:** ![Rainer\_Deyke](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/r/a9a28c/32.png) [@Rainer\_Deyke](https://discourse.libsdl.org/u/Rainer_Deyke)\
**Post date:** [February 5, 2002, 5:57pm UTC](https://discourse.libsdl.org/t/newbie-question/5971/3 "2002-02-05T17:57:01Z")

</div>

“Thiago S. Concei??o” wrote in message  
news:mailman.1012956722.5191.sdl at [libsdl.org](http://libsdl.org)…

> pers-\>x = (screen-\>w / 2) - 80;  
> pers-\>y = (screen-\>h / 2) - 60;  
> pers-\>w = doll-\>w;  
> pers-\>h = doll-\>h;

‘pers’ isn’t pointing to anything, so you can’t modify the pointed-to  
object. Either allocate it statically:

SDL\_Rect r;  
pers = &r;

or dynamically:

pers = malloc(sizeof(SDL\_Rect)); // Don’t forget to ‘free’.

> SDL\_FreeSurface(screen);

Don’t do that either.–  
Rainer Deyke | root at [rainerdeyke.com](http://rainerdeyke.com) | [http://rainerdeyke.com](http://rainerdeyke.com)

---

<div class="post-metadata">

**Author:** ![Udo\_Philipps](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/u/9f8e36/32.png) [@Udo\_Philipps](https://discourse.libsdl.org/u/Udo_Philipps)\
**Post date:** [February 6, 2002, 12:36am UTC](https://discourse.libsdl.org/t/newbie-question/5971/4 "2002-02-06T00:36:01Z")

</div>

> int game\_quit()  
> {  
> SDL\_FreeSurface(screen);  
> SDL\_FreeSurface(scene);  
> SDL\_FreeSurface(doll);  
> SDL\_Quit();  
> return(0);  
> }

don’t free the screen

---

<div class="post-metadata">

**Author:** ![Udo\_Philipps](https://discourse.libsdl.org/letter_avatar_proxy/v4/letter/u/9f8e36/32.png) [@Udo\_Philipps](https://discourse.libsdl.org/u/Udo_Philipps)\
**Post date:** [February 6, 2002, 12:38am UTC](https://discourse.libsdl.org/t/newbie-question/5971/5 "2002-02-06T00:38:01Z")

</div>

> SDL\_Rect \*pers;

> int main()  
> {  
> game\_init();  
> game\_update();  
> game\_quit();  
> return(0);  
> }

pers is a pointer, you have to malloc it manually

i.e. pers = (SDL\_Rect\*)malloc(sizeof(SDL\_Rect));
