?
Solved

Easy:: delete

Posted on 2004-11-18
6
Medium Priority
?
254 Views
Last Modified: 2010-04-01
if I have:

char* test= new char[29];

What is the right (better) way to do:

if(test!=NULL)
{
delete test;
test=NULL;
}

OR
if(test){
delete test;
test=NULL;
}

OR if(test)
delete test;
0
Comment
Question by:pbavishi
  • 4
  • 2
6 Comments
 
LVL 30

Expert Comment

by:Axter
ID: 12618194
Better way is

delete[] test;
test=NULL;
0
 
LVL 30

Expert Comment

by:Axter
ID: 12618221
You don't have to test if the value is NULL.

According to the C++ standard, the delete operator will do nothing if the value is NULL.
That means it's safe to delete a pointer with a NULL value.

Also, since you created the instance using new[] operator, you need to use delete[] instead of delete.

You should always set any variable you delete to NULL.

I always do this, even if it's in a destructor.
0
 
LVL 2

Accepted Solution

by:
pb_india earned 200 total points
ID: 12618501
Using this:

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

is the best practice. Always set the pointer to NULL after deletion. So when you use next time,

if(test) will always fail and avoid re-deletion of the memory.

Use this macro:

#ifndef safeDelete
#      define safeDelete(obj) {if (obj) delete (obj); obj=NULL;}
#endif
0
Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
LVL 2

Expert Comment

by:pb_india
ID: 12618504
And. in case of char*

#ifndef safeDeleteVector
#      define safeDeleteVector(ar) {if (ar) delete [] (ar); ar=NULL;}
#endif
0
 
LVL 30

Expert Comment

by:Axter
ID: 12618540
>>if(test) will always fail and avoid re-deletion of the memory.

The if(test) condition will *NOT* avoid re-deletion of memory.

It's not needed at all, and will not help you to avoid re-deletion.
0
 
LVL 30

Expert Comment

by:Axter
ID: 12618591
//Don't perform if condition
delete[] test;
test=NULL;//Set to NULL so as to avoid re-deletion of object

Setting the value to NULL will avoid re-deletion of the object.

0

Featured Post

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

C++ Properties One feature missing from standard C++ that you will find in many other Object Oriented Programming languages is something called a Property (http://www.experts-exchange.com/Programming/Languages/CPP/A_3912-Object-Properties-in-C.ht…
Article by: evilrix
Looking for a way to avoid searching through large data sets for data that doesn't exist? A Bloom Filter might be what you need. This data structure is a probabilistic filter that allows you to avoid unnecessary searches when you know the data defin…
The goal of the video will be to teach the user the difference and consequence of passing data by value vs passing data by reference in C++. An example of passing data by value as well as an example of passing data by reference will be be given. Bot…
The viewer will be introduced to the technique of using vectors in C++. The video will cover how to define a vector, store values in the vector and retrieve data from the values stored in the vector.

864 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question