Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Forget to test pointer as NULL?

Reply
Thread Tools

Forget to test pointer as NULL?

 
 
Nephi Immortal
Guest
Posts: n/a
 
      09-08-2011
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.
 
Reply With Quote
 
 
 
 
Noah Roberts
Guest
Posts: n/a
 
      09-08-2011
On Sep 8, 8:56*am, Nephi Immortal <(E-Mail Removed)> wrote:
> * * * * 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.
 
Reply With Quote
 
 
 
 
Paul N
Guest
Posts: n/a
 
      09-08-2011
On Sep 8, 5:05*pm, Noah Roberts <(E-Mail Removed)> wrote:
> On Sep 8, 8:56*am, Nephi Immortal <(E-Mail Removed)> wrote:
>
>
>
>
>
> > * * * * 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?

 
Reply With Quote
 
Victor Bazarov
Guest
Posts: n/a
 
      09-08-2011
On 9/8/2011 2:52 PM, Paul N wrote:
> On Sep 8, 5:05 pm, Noah Roberts<(E-Mail Removed)> wrote:
>> On Sep 8, 8:56 am, Nephi Immortal<(E-Mail Removed)> wrote:
>>
>>
>>
>>
>>
>>> 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
--
I do not respond to top-posted replies, please don't ask
 
Reply With Quote
 
Jorgen Grahn
Guest
Posts: n/a
 
      09-08-2011
On Thu, 2011-09-08, Paul N wrote:
> On Sep 8, 5:05*pm, Noah Roberts <(E-Mail Removed)> wrote:
>> On Sep 8, 8:56*am, Nephi Immortal <(E-Mail Removed)> wrote:


>> > * * * * 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

--
// Jorgen Grahn <grahn@ Oo o. . .
\X/ snipabacken.se> O o .
 
Reply With Quote
 
Andrey Tarasevich
Guest
Posts: n/a
 
      09-09-2011
On 9/8/2011 8:56 AM, Nephi Immortal wrote:
>
> 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.

--
Best regards,
Andrey Tarasevich
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Pointer to pointer or reference to pointer A C++ 7 07-05-2011 07:49 PM
Pointer to pointer Vs References to Pointer bansalvikrant@gmail.com C++ 4 07-02-2009 10:20 AM
passing the address of a pointer to a func that doesnt recieve a pointer-to-a-pointer jimjim C Programming 16 03-27-2006 11:03 PM
Pointer-to-pointer-to-pointer question masood.iqbal@lycos.com C Programming 10 02-04-2005 02:57 AM
test test test test test test test Computer Support 2 07-02-2003 06:02 PM



Advertisments