Jump to content
  • Advertisement
Sign in to follow this  
Liuqahs15

How to check if dynamic memory is there before deleting?

This topic is 2031 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 sorry if this question has been asked before, but I'm struggling with figuring out how to google it right. I don't know how to pose the question without an example.

 

Say I have this code:

 

 

int main(){

   int * ptr = NULL;

   int input = 0;

   cin >> input;

   if(input > 5)
      ptr = new int;

  /*Apparently this doesn't work..*/
   if(ptr)
      delete ptr;

   return 0;
}

 

That's basically the way I've been checking if a pointer has been allocated dynamic memory or not, but I've just learned that my solution is wrong. So what might be one smart way to go about it? I just made the above code off the top of my head, so please don't criticize it for stuff that's off-topic from the question. Thanks a lot.

Edited by Shaquil

Share this post


Link to post
Share on other sites
Advertisement
Your solution is not wrong, but you may have misunderstood why it's not useful to check for null before deleting. It is not that your code won't work. the pointer is initially null, and if you allocate something, then the pointer is not null and it will be deleted. Likewise, if you don't allocate anything, then the pointer remains null and it won't delete it.

 

But that's not the issue. The "problem" is that calling delete on a null pointer is perfectly fine, so the if-statement is useless. Not incorrect, just useless. If ptr is null, then delete does nothing.

 

Oh that's probably the worst answer I could've gotten. Not because it's a bad answer, but because now I'm even more confused. Thank you for explaining that, though.

Share this post


Link to post
Share on other sites
but because now I'm even more confused.


'delete' is just a function that looks different than other functions. Inside the delete function, it does the if(ptr == NULL) check for you.

 

Deleting NULL does nothing, so no harm is done. Deleting the same (non-NULL) memory twice is bad though. Deleting memory that was never allocated is also bad.

Share this post


Link to post
Share on other sites
Brother Bob is correct. What was it that made you think "I've just learned that my solution is wrong."?

Share this post


Link to post
Share on other sites

I'll agree with Madhed on this one, you should have a basic understanding of how dynamic memory works under the hood but there's very few occasions that would call for custom memory managed systems.  The Standard Template Library or STL (std:: classes) provide a wide range of easy to use and trustworthy dynamic memory wrappers.

Share this post


Link to post
Share on other sites

Wanting to delete memory and not being sure if you actually should delete it is the root of all evil. Try to impose as many rules as you can on your own code and make it as close to impossible as possible for others using your classes to do something stupid. One such an idea would be to enforce the creation and deletion of your object to stay in your own code (private constructors/destructors and friend class manager?). Anyhow, it's a tricky subject that cannot be taught in a few posts. Learn the basics of allocation/deletion very well, experiment a lot and grow wiser. At one point, you will feel confidant enough to answer your own question (and, from time to time, you will fail tongue.png, but keep it up ). You will know that you have the right implementation the moment you will KNOW that you MUST delete some memory and not wonder anymore.

Edited by LordJulian

Share this post


Link to post
Share on other sites

If memory is not there, then you can't even allocate memory, otherwise if you have succesfully allocated memory with new then there is reserved memory size of the data type. I think you should perhaps think otherway around, how to ensure that there is memory before allocating or if program can't allocate memory then how to catch exceptions and what are actions to avoid useless deletions.

Share this post


Link to post
Share on other sites
I guess the problem you were experiencing was double-deleting memory. One solution is to set the pointer to null immediately after deleting it, if you plan on reusing it later.
That's not much of a solution. We have had this controversy before in these forums, and to me it feels wrong to set the pointer to null, and it might give you a false sense of security. Double-deleting memory tends to happen when you have two different pointers to the same data and the programmer is confused about ownership. Setting pointers to null is a bit like rearranging the deck chairs on the Titanic.
However, you should also look into the standard library containers and smart pointers. (std::vector, std::list, std::shared_ptr, std::weak_ptr, etc.) They are the basics of modern, idiomatic c++ and in many cases eliminate the need to manually manage your memory.
This, with strong emphasis on std::vector.

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!