Jump to content
  • Advertisement
Sign in to follow this  
radioact1ve

Returning a Pointer (beginner question)

This topic is 4430 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

Hey everyone! Just a quick question on returning pointers, confused as usual. I'll just use my SDL example, though it has nothing to do with SDL. SDL_Surface *load_image( string filename ) { SDL_Surface *myImg = NULL: // load image stuff // myImg = pic.png return myImg } From what I have been taught and read up on, this shouldn't work, right? When the function is done, aren't the local variables for the function done as well. I know that by using 'new' or static should work, but I know that I'm missing something b/c the way mentioned in the above code IS working for me. Can't wait to see how wrong I am. 8)

Share this post


Link to post
Share on other sites
Advertisement
The variable myImg is of type (SDL_Surface *) a pointer to a memory address that marks the beginning of the type SDL_Surface. So, when you are loading your image, you want to use the new keyword.

For example:


SDL_Surface *myImage = new SDL_Surface;




That code creates a new memory allocation of an SDL_Surface inside inside the heap, which is not local to any function or method. The only way to free this memory up, once the new keyword is used, is to use the delete keyword.

For example:


// Is the pointer valid (not NULL)
if(my_Image)
{
delete SDL_Surface; // Free the memory
SDL_Surface = NULL;
}

////////
// OR //
////////

SDL_Surface *My_New_Surface = load_image("tile.bmp");

// Do stuff with image

// On shut down

if(My_New_Surface)
{
delete My_New_Surface;
My_New_Surface = NULL;
}



Since you are holding a pointer to the object, which is just a memory address where the actual object is located, when you return the pointer value you are only returning the memory address. There are no ties to any variables inside of the load_image() function now. You've just copied the address to a new pointer, which now points to the SDL_Surface object that you've just created. Hope that helps to clear things up a bit.

Share this post


Link to post
Share on other sites
Daishim, that code you put there is exactly what I was thinking. That what I thought was the right way. Thanks.

However, I'm not sure I understood your last sentence. Since i created the myImg within Load_img(), doesn't myImg get destroyed when the function is done thus retuning a dangling pointer? A pointer to nothing if you will.

Share this post


Link to post
Share on other sites
Quote:
Original post by radioact1ve
Daishim, that code you put there is exactly what I was thinking. That what I thought was the right way. Thanks.

However, I'm not sure I understood your last sentence. Since i created the myImg within Load_img(), doesn't myImg get destroyed when the function is done thus retuning a dangling pointer? A pointer to nothing if you will.


Not if Load_img() uses new to assign the block of memory - which, as you can see, it does. [smile]
However this also means you'll have to free the memory later; if you lose track of the pointer which had dynamically allocated memory you get a memory leak.

Share this post


Link to post
Share on other sites
Quote:
Original post by Frequency
Not if Load_img() uses new to assign the block of memory - which, as you can see, it does. [smile]
However this also means you'll have to free the memory later; if you lose track of the pointer which had dynamically allocated memory you get a memory leak.


Exactly, so if you look at my original code at the top, I don't use new. My question is, if I DON'T use new, how come the function DOES work as intended? [smile]

Share this post


Link to post
Share on other sites
The actual values in memory may still be there, since your program may not have requested any new memory or written over top of it yet. So, you're pointer points to that location in memory, but that location is not actually tied to a variable, so it can be reassigned and changed at any time without your knowledge. That may explain the lingering appearance of that code still working, however, at some point in time, it will fail.

Share this post


Link to post
Share on other sites
When dealing with other libraries, it's a bad idea to call delete on any object that you didn't create yourself. For example, when you're finished with a surface created by SDL_CreateRGBSurface(), you should call SDL_FreeSurface() on it. An SDL_Surface may contain pointers to other objects (like SDL_PixelFormat), and only SDL knows how to free everything properly.

Also, C libraries use malloc/free instead of C++'s new/delete, and mixing them is not guaranteed to work. However when using the memory it doesn't matter how or where it is allocated. As long as you control its lifetime properly.

I hope that answers everything :)

Share this post


Link to post
Share on other sites
Quote:
Original post by Daishim
That may explain the lingering appearance of that code still working, however, at some point in time, it will fail.


Well said. I'm pretty sure I understand this now. Thanks everyone!!

Share this post


Link to post
Share on other sites
Quote:
Original post by hh10k
When dealing with other libraries, it's a bad idea to call delete on any object that you didn't create yourself. For example, when you're finished with a surface created by SDL_CreateRGBSurface(), you should call SDL_FreeSurface() on it. An SDL_Surface may contain pointers to other objects (like SDL_PixelFormat), and only SDL knows how to free everything properly.

Also, C libraries use malloc/free instead of C++'s new/delete, and mixing them is not guaranteed to work. However when using the memory it doesn't matter how or where it is allocated. As long as you control its lifetime properly.

I hope that answers everything :)


Ah yes, I should have mentioned that I'm not familiar with SDL. But it looks like that's the missing piece. :)

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.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!