How to check if dynamic memory is there before deleting?

Started by
18 comments, last by JTippetts 11 years, 3 months ago

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.

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.

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.

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.

Brother Bob is correct. What was it that made you think "I've just learned that my solution is wrong."?

In c++ you can not test if a raw pointer actually points to allocated memory. You have to keep track of that yourself. 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.

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.

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.

Dan Mayor

Professional Programmer & Hobbyist Game Developer

Seeking team for indie development opportunities, see my classifieds post

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.

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.

See my game dev blog: http://gamedev4hobby.blogspot.fi/
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.

This topic is closed to new replies.

Advertisement