Using NEW and DELETE operators?

D

Donos

Please see the following code :-

unsigned char* pChar = new unsigned char[20];

Now am using:-

delete [] pChar;

I want to add a condition statement before deleting:-

if(pChar != NULL)
delete [] pChar;

But it seems the above code doesn't seem to work. I want to delete the
pointer only if it's a valid pointer.

How to do that?
 
D

Daniel T.

Donos said:
Please see the following code :-

unsigned char* pChar = new unsigned char[20];

Now am using:-

delete [] pChar;

I want to add a condition statement before deleting:-

if(pChar != NULL)
delete [] pChar;

But it seems the above code doesn't seem to work. I want to delete the
pointer only if it's a valid pointer.

How to do that?

your original code will do that:

delete [] pChar; // will delete pChar only if it is not NULL
 
G

Guest

Please see the following code :-

unsigned char* pChar = new unsigned char[20];

Now am using:-

delete [] pChar;

I want to add a condition statement before deleting:-

if(pChar != NULL)
delete [] pChar;

But it seems the above code doesn't seem to work.

In what way does it not work? It works just fine for me.
I want to delete the pointer only if it's a valid pointer.
How to do that?

Why? Using delete on a null-pointer is a no-op, performing the check is
just a waste of code and execution time.
 
D

Duane Hebert

Erik Wikström said:
Please see the following code :-

unsigned char* pChar = new unsigned char[20];

Now am using:-

delete [] pChar;

I want to add a condition statement before deleting:-

if(pChar != NULL)
delete [] pChar;

But it seems the above code doesn't seem to work.

In what way does it not work? It works just fine for me.
I want to delete the pointer only if it's a valid pointer.
How to do that?

Why? Using delete on a null-pointer is a no-op, performing the check is
just a waste of code and execution time.

He's showing a test for null which isn't needed since it would
be a NOOP as you say. But he's saying that he wants to
call delete on pChar only if it's a VALID pointer. I suspect
he wants to prevent deleting an uninitialized pointer.
 
J

James Kanze

Why? Using delete on a null-pointer is a no-op, performing the check is
just a waste of code and execution time.

It's more a waste of programmer time; a good compiler could
optimize the check out, and even if it didn't, the difference in
execution time is not significant.

But his question stands. He didn't say he wanted to delete the
pointer only if it's non-null; he said he wanted to delete it
only if it's valid. And testing for null doesn't work, because
invalid pointers don't necessarily compare equal to null.
(Formally, even trying to compare an invalid pointer with null
results in undefined behavior. In practice, however, it will
usually just compare unequal---just like a valid pointer to an
object would.)

As for the answer to his question: you need some tool external
to C++, like Purify or valgrind, to do this. Such tools impose
a very significant runtime and space overhead, however, there
are probably licensing issues if you want to deliver the
software with the tool, and they generally don't convert the
operation to a no-op, but rather generate error messages, etc.

For pure memory management, of course, he can use the Boehm
collector, and forget the delete's entirely. This doesn't work
with objects which have a managed lifetime, of course, but
typically, if you're just conditionally deleting like he seems
to be, the object doesn't have a managed lifetime anyway.
 
D

Daniel T.

James Kanze said:
He didn't say he wanted to delete the
pointer only if it's non-null; he said he wanted to delete it
only if it's valid.
As for the answer to his question: you need some tool external
to C++, like Purify or valgrind, to do this.

Another option would be to write a smart pointer that does the right
thing.
 
J

Jim Langston

Donos said:
Please see the following code :-

unsigned char* pChar = new unsigned char[20];

Now am using:-

delete [] pChar;

I want to add a condition statement before deleting:-

if(pChar != NULL)
delete [] pChar;

But it seems the above code doesn't seem to work. I want to delete the
pointer only if it's a valid pointer.

How to do that?

Your code is most likely working. However, delete[] pChar does not change
the value of the pointer, it sitll points to some memory address (now
invalid). Perhaps you are deleting it, then attempting to delete it again.
You may just need to:

delete[] pChar;
pChar = NULL;

The check for NULL is not required. delete already does that check, but
only for a NULL value, any other value (even invalid) it will attempt to
delete.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top