Jump to content
  • Advertisement
Sign in to follow this  
Sabonis

Pointers

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

Is it absolutely necessary to release ALL pointers? And if so, how? My book shows how to create them but im not sure if I should be releasing them. Thanks for any comments!

Share this post


Link to post
Share on other sites
Advertisement
Pointers *point* to memory blocks. You don't release pointers, you release the memory they point to, if that memory was dynamically allocated. Basically, each time you "new" something somewhere, you have to "delete" it before the program ends.

Share this post


Link to post
Share on other sites
ok so if I were to create a pointer:

int *pi;

and if I were to point it to the a variable location:

int a = 7;
pi = &a;

then when I was done with it, I would have to go

pi->Release();

Is this correct?
Thanks

Share this post


Link to post
Share on other sites
no, for one thing, int is a primitive, your treating it as an object. Your probably thinking of delete, but you only have to delete something if you new it.

Share this post


Link to post
Share on other sites
I was just making guesses... But the book im using is by skanksholm. Thanks for your replies. So from here on I have to assume I need to release whatever is created with the new operator...

Share this post


Link to post
Share on other sites
Seriously consider looking at another book.
[edit]Didn't read your last post. You are perfectly right about new/delete.[/edit]
To answer your question, in C/C++ you only need to release pointer that you allocated memory for, e.g.
malloc() → free()
new, new[] → delete, delete[]

Since you didn't ask about the functions and operators I assume you didn't allocate any memory. In this case there is no need to release it as the pointer is only, well, pointing [smile] to an existing memory location and thus is not different from any other variable on the stack. It will be invalid once it goes out of scope.


HTH,
Pat.

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!