Jump to content
  • Advertisement
Sign in to follow this  
Servant of the Lord

Pointer to a SDL_Surface*

This topic is 4332 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I'm having trouble trying to figure out this pointer problem. What I want is to be able to call one function, and store a pointer to the surface passed to it, which I want to be able to retrieve by call another function.
SDL_Surface **drawingSurface;
        
SDL_Surface *GetDrawingSurface()
{
    return *drawingSurface;
}
        
int ClampSurface(SDL_Surface *surface)
{
    if(surface == NULL) return -1;
    drawingSurface = &surface;
    return 0;
}
        
void UnclampSurface()
{
    drawingSurface = NULL;
}
SDL_Surface **drawingSurface should point to a SDL_Surface pointer, right? And ClampSurface() sets the pointer to the surface passed it. I can then get the surface **drawingSurface is pointing to, by calling GetDrawingSurface(). UnclampSurface() is uneccasary, I believe, and I'll probably just remove it later. I also wish to know whether I need to free drawingSurface. It's just a pointer, right? I need to free whatever SDL_Surfaces are passed it, as normal, but SDL_Surface **drawingSurface wouldn't need to be freed, would it?

Share this post


Link to post
Share on other sites
Advertisement
Function parameters are temporary, and go on the stack.

Therefore 'surface' is a temporary variable inside ClampSurface. Its value is the address of an SDL_Surface, but it's address is on the stack somewhere.

You then take a copy of that address, and assign it to drawingSurface.

Then you return from the function, and deallocate the stack. The address that drawingSurface contains is no longer under your control. When you call another function, that stack space will get re-used and possibly overwritten. So when you dereference drawingSurface, you go back to that space on the stack, and the value there is no longer what you originally passed in.

What you really need to do, is pass a pointer to the pointer in ClampSurface. ie. int ClampSurface(SDL_Surface** surface)... you pass the address of the pointer you want storing. ClampSurface then dereferences the pointer it was passed to get the actual pointer it needs, then it stores that in the global, which would just be a SDL_Surface*.

Share this post


Link to post
Share on other sites
Thanks, I understand I think. Another question though; do I need to free the drawingSurface pointer? It's now just a SDL_Surface* as you said, and it is just a pointer, but does SDL create a new set of data when I point drawingSurface at the surface passed, or do both pointers point to the same object, which only needs to be freed through one of the pointers?

Share this post


Link to post
Share on other sites
You don't free pointers, you free surfaces. The pointer just happens to be the mechanism by which you do this. With that in mind, there is no reason for you to free anything. Pointers are just memory addresses. Copying them does nothing to the underlying data. If it did, there'd be no point using them!

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!