Forget to test pointer as NULL?

N

Nephi Immortal

I am surprised to find out why the destructor function does not test
pointer before memory is deleted.

struct Object
{
Object( int n ) : ptr( new char[ n ] )
{
}

~Object()
{
// comment if condition
// if( ptr != NULL )
delete [] ptr;
}

char *ptr;
};

Any C++ books say you must always include if condition to test
pointer if it is not zero before memory is allowed to be deallocated.
I found out that delete function already has if condition. The
delete function returns zero without deallocating memory if pointer is
zero.
 
N

Noah Roberts

        I am surprised to find out why the destructor function does not test
pointer before memory is deleted.

struct Object
{
        Object( int n ) : ptr( new char[ n ] )
        {
        }

        ~Object()
        {
                // comment if condition
                // if( ptr != NULL )
                        delete [] ptr;
        }

        char *ptr;

};

        Any C++ books say you must always include if condition totest
pointer if it is not zero before memory is allowed to be deallocated.

Throw all your C++ books away then.
        I found out that delete function already has if condition..  The
delete function returns zero without deallocating memory if pointer is
zero.

Yep.
 
P

Paul N

        I am surprised to find out why the destructor function does not test
pointer before memory is deleted.
struct Object
{
        Object( int n ) : ptr( new char[ n ] )
        {
        }
        ~Object()
        {
                // comment if condition
                // if( ptr != NULL )
                        delete [] ptr;
        }
        char *ptr;

        Any C++ books say you must always include if condition to test
pointer if it is not zero before memory is allowed to be deallocated.

Throw all your C++ books away then.
        I found out that delete function already has if condition.  The
delete function returns zero without deallocating memory if pointer is
zero.

Yep.

Besides, I thought new couldn't return NULL anyway?
 
V

Victor Bazarov

I am surprised to find out why the destructor function does not test
pointer before memory is deleted.
struct Object
{
Object( int n ) : ptr( new char[ n ] )
{
}
~Object()
{
// comment if condition
// if( ptr != NULL )
delete [] ptr;
}
char *ptr;

Any C++ books say you must always include if condition to test
pointer if it is not zero before memory is allowed to be deallocated.

Throw all your C++ books away then.
I found out that delete function already has if condition. The
delete function returns zero without deallocating memory if pointer is
zero.

Yep.

Besides, I thought new couldn't return NULL anyway?

A placement form with 'std::nothrow' can.

V
 
J

Jorgen Grahn

        I am surprised to find out why the destructor function does not test
pointer before memory is deleted.
struct Object
{
        Object( int n ) : ptr( new char[ n ] )
        {
        }
        ~Object()
        {
                // comment if condition
                // if( ptr != NULL )
                        delete [] ptr;
        }
        char *ptr;

        Any C++ books say you must always include if condition to test
pointer if it is not zero before memory is allowed to be deallocated.

Throw all your C++ books away then.
        I found out that delete function already has if condition.  The
delete function returns zero without deallocating memory if pointer is
zero.

Yep.

Besides, I thought new couldn't return NULL anyway?

A standard new cannot. But there are often other valid reasons 'ptr'
may be null at the time the destructor is called. In the class above,
a single 'obj.ptr = 0' will do.

/Jorgen
 
A

Andrey Tarasevich

Any C++ books say you must always include if condition to test
pointer if it is not zero before memory is allowed to be deallocated.

I'm not aware of any of such books. Must be some garbage book. All
standard memory deallocation facilities perform a this test internally,
which is why you shouldn't normally do it yourself.
I found out that delete function already has if condition.

Delete function? What "delete function" are you talking about?
The
delete function returns zero without deallocating memory if pointer is
zero.

Er... "Returns zero"? I don't know what "delete function" you are
referring to, but any potential candidates I know of don't return
anything at all.
 

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,755
Messages
2,569,536
Members
45,007
Latest member
obedient dusk

Latest Threads

Top