Skip to main content
GameDev.net gamedev.net
🔒 Locked

Using "delete" before "new"

Started by v0dKA Apr 14, 2006 at 7:43 AM 10 replies 3.2k views
Original Post
v0dKA
v0dKA
I'm working on writing a class which allocates memory dynamically using new and delete. My question is, if a certain pointer variable might have been used to allocate memory with new, how can I make sure it's cleared before using it to allocate something else? I thought before that there was no harm in using delete if it had been used once already, but I'm getting an error when I try. I suppose it's significant to mention that I'm using delete[] for an array, not a scalar.
.:-v0d[KA]-:. <<>>
Fruny
Fruny
Set the pointer to zero (or NULL) after deleting: deleting a null pointer is not an error.
Of course, if you are passing that pointer by value to a function that deletes it, it won't work since setting the local variable to 0 won't affect the original pointer (pass by reference instead). On the other hand, if the pointer is a member variable of your class then modifying it directly would be OK.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
senelebe
senelebe
You could put the new statement in the constructor, and the delete in the destructor, this would ensure that the pointer is created and destroyed properly, I don't know if that fits your scheme of things though.
NoMonkey
NoMonkey
The best way to ensure you won't end up with a read/write error is to always set a pointer to 0 or NULL. Any time a function is recieving a pointer, it should always check it's validity before using it:

if( ptr )
delete ptr;

You can also come up with a class to encapsulate the original data and count any reference pointing to it. This way you can let the encapsulating class free the pointer when nothing else is using it and you can detect when pointers aren't released.

Hope this helps.
Fruny
Fruny
Quote:
Original post by NoMonkey
Any time a function is recieving a pointer, it should always check it's validity before using it:

if( ptr )
delete ptr;


As I pointed out, that's unnecessary: deleting a null pointer is not an error.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
NoMonkey
NoMonkey
But trying to use a null pointer is an error. From reading his initial post, he appears to be unaware of how to check a pointer for validity.

My point was that if he always sets the pointer to NULL and checks it's validity, there's shouldn't be a problem.
Fruny
Fruny
I suspect he's got a double-delete problem rather than a null pointer dereference.

Your advice is good, but the example you used is the one case where it's unneeded. [rolleyes]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
NoMonkey
NoMonkey
Quote:
Original post by Fruny
Your advice is good, but the example you used is the one case where it's unneeded. [rolleyes]


Whatever you say.
Deranged
Deranged
Quote:
Original post by Fruny
Quote:
Original post by NoMonkey
Any time a function is recieving a pointer, it should always check it's validity before using it:

if( ptr )
delete ptr;


As I pointed out, that's unnecessary: deleting a null pointer is not an error.


A little Dev-x tip of the day, for anyone who doesn't beleive you.

Quote:

C++ guarantees that operator delete checks its argument for null-ness. If the argument is 0, the delete expression has no effect. In other words, deleting a null pointer is a safe (yet useless) operation. There is no need to check the pointer for null-ness before passing it to delete:


if (p) // useless; delete already checks for a null value
delete(p);
simon10k
simon10k
Fruny is right in what he is saying I had a similar problem, an object can be in a fully contructed way or it might be "empty" where all data members are null. you dont want to add lots of if statements where you dont need them, its not nice to look at.
-----------------------------Language: C++API: Win32, DirectXCompiler: VC++ 2003
Squirm
Squirm
Decide, the instant you allocate that memory, precisely what owns it. That thing is responsible for deleting it. Anyone else deleting it is a programming error. Any circumstance in which it can fail to delete it is a programming error. You can pass ownership, but it can never not be owned and it can never be owned by more than one object.

you may find auto_ptr and boost::shared_ptr help with that, especially if you are new to the general problem :)
Cocalus
Cocalus
Make sure that your pointer either contains 0 (NULL) or a properlly newed data block. It's best to do this when you define the pointer (so always add "= 0" or "= new somthing" when you define a pointer, or even better is within a constructor)

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.