Forget to test pointer as NULL?

Discussion in 'C++' started by Nephi Immortal, Sep 8, 2011.

  1. 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.
    Nephi Immortal, Sep 8, 2011
    #1
    1. Advertising

  2. Nephi Immortal

    Noah Roberts Guest

    On Sep 8, 8:56 am, Nephi Immortal <> 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.
    Noah Roberts, Sep 8, 2011
    #2
    1. Advertising

  3. Nephi Immortal

    Paul N Guest

    On Sep 8, 5:05 pm, Noah Roberts <> wrote:
    > On Sep 8, 8:56 am, Nephi Immortal <> 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?
    Paul N, Sep 8, 2011
    #3
  4. On 9/8/2011 2:52 PM, Paul N wrote:
    > On Sep 8, 5:05 pm, Noah Roberts<> wrote:
    >> On Sep 8, 8:56 am, Nephi Immortal<> 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
    Victor Bazarov, Sep 8, 2011
    #4
  5. Nephi Immortal

    Jorgen Grahn Guest

    On Thu, 2011-09-08, Paul N wrote:
    > On Sep 8, 5:05 pm, Noah Roberts <> wrote:
    >> On Sep 8, 8:56 am, Nephi Immortal <> 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 .
    Jorgen Grahn, Sep 8, 2011
    #5
  6. 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
    Andrey Tarasevich, Sep 9, 2011
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    5
    Views:
    26,484
    Mike Schilling
    Mar 29, 2006
  2. Ken
    Replies:
    69
    Views:
    12,470
    Michael D. Borghardt
    Jul 31, 2004
  3. Ken
    Replies:
    5
    Views:
    2,513
    Victor Bazarov
    Jul 24, 2004
  4. aneuryzma
    Replies:
    3
    Views:
    690
    Jim Langston
    Jun 16, 2008
  5. Christopher
    Replies:
    4
    Views:
    426
    Ruben Safir
    Jul 9, 2011
Loading...

Share This Page