Confusion about Pointers :S (and other dilemmas)

Hi there,
I have a couple of questions I'd like to ask about the usage of pointers. I wanted to experiment a bit with SDL but noticed right away that the SDL_Surfaces for example are created as pointers :S

1.: If I construct the Image with a SDL_Surface that goes out of scope earlier than my Image itself, what happens to surface_? (Because I think it would still point to a memory address but probably not the one of the original surface I created the Image with, right?)

Or a similar thing I think would be if I saved a surface I got with getSurface() (from an Image image) as SDL_Surface *surface. If this surface went out of scope before image would, what happens to surface_?

2.: Finally the methods I marked with 2. would cause memory leaks if surface_ was previously assigned to something else, right? Would I first free the previous surface_(if there was one)?

I'd also like to ask an additional question about makefiles:
If I have the following structure:
FloatRect.cpp includes FloatRect.h
Image.cpp includes Image.h which includes FloatRect.h
main.cpp includes Image.h

But I am first of all not sure whether I'm supposed to put the Header files into the prerequisites. Also if I changed FloatRect.h the Image.o target wouldn't be compiled again. Is this supposed to happen? Can I still improve this makefile? (because I'm just not sure whether I am writing them in the proper way >.<)

I would appreciate it very much if anyone could help me clear up these matters :D

//EDIT:
Oh and by the way is there a way for me to write programs with SDL while still having a console to write to because at the moment I am writing all my testing stuff in the caption of the window ^^

In your first example, your constructor takes the surface as a parameter, therefore it is the responsibility of the external code to properly destruct it - you are just given access to it, so you should not destroy it without permission.

@1. That's not a problem your class needs to be concerned with (as you have it currently), but yes, your class would have a angling pointer.
@2. Make up your mind: does your class manage the surface, or does it just have access to it?

In this code snippet I would probably need to create a destructor because the surface_ is a pointer, right? What would this destructor consist of? Calling SDL_FreeSurface(surface_)?

If you don't know the semantics of your own class, then how are we supposed to know?

If I construct the Image with a SDL_Surface that goes out of scope earlier than my Image itself, what happens to surface_? (Because I think it would still point to a memory address but probably not the one of the original surface I created the Image with, right?)

Wrong. The pointer goes out of scope, but this does absolutely nothing to the memory being pointed at. It doesn't change the copy of the pointer you're holding either.

2.: Finally the methods I marked with 2. would cause memory leaks if surface_ was previously assigned to something else, right? Would I first free the previous surface_(if there was one)?

You leak memory if you don't free up resources you create. So it depends on how your class is supposed to behave - if your Image object are supposed to be the sole owners of the surface, then yes you'd leak memory there. If the Image is just supposed to reference that surface, and the owner is something else (for example a resource manager) then not necessarily.

Oh and by the way is there a way for me to write programs with SDL while still having a console to write to because at the moment I am writing all my testing stuff in the caption of the window ^^

I still can't cout to the console then though (I think it has to do with SDLmain?).

If you don't know the semantics of your own class, then how are we supposed to know?

My class is supposed to pair a SDL_Surface up with a FloatRect because it seems hard to do time-based (instead of frame-based) movement with the int rectangles SDL provides.

Also with my question I was just trying to reinforce what I read in http://www.cplusplus.com/forum/general/8767/ namely when exactly I need a destructor. I find your wording to come across as a bit insulting, but I guess that may be my fault for getting offended rather easily by stuff people say on the internet.

You still need to make up your mind: does your Image class take full responsibility for the SDL_Surface, or does it just get permission to access someone else's SDL_Surface? Your class is currently a mix.

The class is supposed to own the SDL_Surface (so, yes, take responsibility). Still I'd like to be able to blit the surface owned by the Image outside of the Image itself (thus I would have to have a getter for the surface, which apparently complicates things).

I read up a bit on deep copy vs shallow copy and such now but I guess that doesn't apply to my issue? (I don't think a SDL_Surface can be deep copied?)

Also: Should I consider using/learning smart pointers? I've read that shared pointers and such may help with the aforementioned ownership issues? I didn't use boost/smart pointers yet so I would probably have to learn a bit of new stuff in that area first :S

Since you want your Image class to own the SDL_Surface, it should construct it itself in its ctor, rather than take a pointer to it as a parameter.

Shouldn't an SDL_Surface be deep-copy-able? You would make a new one and copy settings and content to it from the old one.

You will indeed want to look into smart pointers. If SDL_Surface needs to be freed by calling some other function than default delete, you will need to give that information to the smart pointer so it can get deleted properly.

> it should construct it itself in its ctor, rather than take a pointer to it as a parameter.
But there are several ways to construct a surface, and it's the client that should decide.

> I read up a bit on deep copy vs shallow copy and such now but I guess that doesn't apply to my issue?
You do need to release the surface.
A shallow copy would attempt a double release (error)
SDL_surface provides a counter for shallow copy and a clone function `ConvertSurface()' for deep copy.
You could provide no copy mechanism too.

But there are several ways to construct a surface, and it's the client that should decide.

In that case, he either needs to account for alternative ways to destruct & reconstruct the SDL_Surface (it could be allocated with new, new[], malloc, some sort of SDL_Create_Surface, placement new, etc.) or he needs to place the lifetime responsibility of the SDL_Surface outside of his class. (I'd opt for the second option)