the ptr to the SDL Surface doesn’t need to be malloc’d unless you need an
array of them. Assuming you are get the SDL_Surface from a function like
IMG_Load() you would just do this:
struct mainstruct_type
{
// This would be whatever variables you need for your struct
int x,y;
SDL_Surface *ptr_SDL_Surface;
};
ptr_to_mainstruct = malloc(sizeof (mainstruct_type));
ptr_to_mainstruct->ptr_SDL_Surface = SDL_LoadBMP( “test.bmp” );
// Use your pointer here to BLIT it, etc.
// Free the SDL_Surface here
if (ptr_to_mainstruct->ptr_SDL_Surface )
{
SDL_FreeSurface( ptr_to_mainstruct->ptr_SDL_Surface );
ptr_to_mainstruct->ptr_SDL_Surface = NULL;
}From: sdl-bounces@lists.libsdl.org [mailto:sdl-bounces at lists.libsdl.org] On
Behalf Of icsiwtf
Sent: Thursday, January 20, 2011 10:31 PM
To: sdl at lists.libsdl.org
Subject: [SDL] sizeof SDL_Surface?
I am trying to malloc a pointer to a structure which contains a pointer to a
SDL_Surface.
I did it in two lines:
ptr_to_mainstruct = malloc(sizeof (mainstruct_type));
ptr_to_mainstruct -> ptr_to_SDL_Surface = malloc(sizeof (SDL_Surface));
( Is that correct? Could this be done in a single malloc? )
Everything seems to be working fine, but when I try to apply my image to the
main surface, it’s not displaying after I flip the surface. Furthermore,
when I try to SDL_FreeSurface(ptr_to_mainstruct -> ptr_to_SDL_Surface))
issues a segfault. I have a feeling the malloc is wrong.