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.

Quoth icsiwtf , on 2011-01-20 20:31:14 -0800:

I did it in two lines:

ptr_to_mainstruct = malloc(sizeof (mainstruct_type));

ptr_to_mainstruct -> ptr_to_SDL_Surface = malloc(sizeof (SDL_Surface));

No, you can’t malloc an SDL_Surface yourself. You can only get
surface pointers out of the SDL library. Use SDL_Create*Surface calls
to create new surfaces, and SDL_FreeSurface to free them. malloc does
not pair with SDL_FreeSurface.

—> Drake Wilson

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.

Drake Wilson wrote:

Quoth icsiwtf <@icsiwtf>, on 2011-01-20 20:31:14 -0800:

I did it in two lines:

ptr_to_mainstruct = malloc(sizeof (mainstruct_type));

ptr_to_mainstruct -> ptr_to_SDL_Surface = malloc(sizeof (SDL_Surface));

No, you can’t malloc an SDL_Surface yourself. You can only get
surface pointers out of the SDL library. Use SDL_Create*Surface calls
to create new surfaces, and SDL_FreeSurface to free them. malloc does
not pair with SDL_FreeSurface.

—> Drake Wilson


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Thanks, I am new and unfamiliar with SDL_Create. I will have to read more about it.

@ken, thanks! I am using the IMG_Load() function from the SDL_image extension. I was trying to do a similar method before but my debugger kept saying the filename was at “0x0” and I knew something funky happened. I can try it again when I get home.

Since the Surfaces don’t need to be malloced, I can assume the SDL library does the mallocing at some point?

Quoth icsiwtf , on 2011-01-20 20:59:24 -0800:

Thanks, I am new and unfamiliar with SDL_Create. I will have to read more about it.

Sorry, let me be a little clearer. I was using the * as a wildcard
since I didn’t remember whether there were multiple such functions.
See SDL_CreateRGBSurface and/or SDL_CreateRGBSurfaceFrom. There’s
some other functions that implicitly allocate surfaces, such as
SDL_ConvertSurface, but don’t worry about those unless you need them.

I’m assuming you’re using SDL 1.2 here. SDL_SetVideoMode also returns
a surface pointer which is for the framebuffer.

Since the Surfaces don’t need to be malloced, I can assume the SDL
library does the mallocing at some point?

In a sense, in that the memory is allocated by the SDL library, but
that’s all internal to SDL and not something you can rely on having
happen in any particular way inside. Don’t directly mix any of this
with malloc/free; that way lies madness. All you get to see at the
interface boundary is “this is now a valid surface pointer which I can
use with surface functions” and “this is no longer a valid surface
because it was freed with SDL_FreeSurface”, plus I think a few public
structure fields.

—> Drake Wilson

Thanks! I will dive into some documentation~ it looks pretty promising.

You can use the code snippet I provided you with the IMG_Load() function
too.

Yes, SDL allocates any memory needed internally. Software surfaces would
allocate memory in RAM, but hardware display surfaces will put the image in
the video memory. Either way SDL still allocates the size of the
SDL_Surface structure for you.

KenFrom: sdl-bounces@lists.libsdl.org [mailto:sdl-bounces at lists.libsdl.org] On
Behalf Of icsiwtf
Sent: Thursday, January 20, 2011 10:59 PM
To: sdl at lists.libsdl.org
Subject: Re: [SDL] sizeof SDL_Surface?

Drake Wilson wrote:

Quoth icsiwtf <>, on 2011-01-20 20:31:14 -0800:

Quote:

I did it in two lines:

ptr_to_mainstruct = malloc(sizeof (mainstruct_type));

ptr_to_mainstruct -> ptr_to_SDL_Surface = malloc(sizeof (SDL_Surface));

No, you can’t malloc an SDL_Surface yourself. You can only get
surface pointers out of the SDL library. Use SDL_Create*Surface calls
to create new surfaces, and SDL_FreeSurface to free them. malloc does
not pair with SDL_FreeSurface.

—> Drake Wilson


SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Thanks, I am new and unfamiliar with SDL_Create. I will have to read more
about it.

@ken, thanks! I am using the IMG_Load() function from the SDL_image
extension. I was trying to do a similar method before but my debugger kept
saying the filename was at “0x0” and I knew something funky happened. I can
try it again when I get home.

Since the Surfaces don’t need to be malloced, I can assume the SDL library
does the mallocing at some point?

Thanks! Everything is working perfect. I am going to test if SW or HW rendering is faster for my program. In any case, this problem has been solved. Thanks a lot!