Easy:: delete

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;
pbavishiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

AxterCommented:
Better way is

delete[] test;
test=NULL;
0
AxterCommented:
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
pb_indiaCommented:
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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Get expert help—faster!

Need expert help—fast? Use the Help Bell for personalized assistance getting answers to your important questions.

pb_indiaCommented:
And. in case of char*

#ifndef safeDeleteVector
#      define safeDeleteVector(ar) {if (ar) delete [] (ar); ar=NULL;}
#endif
0
AxterCommented:
>>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
AxterCommented:
//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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
C++

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.