Topic: Is operator delete called for null pointers?


Author: stephen.clamage@sun.com (Steve Clamage)
Date: 1999/02/04
Raw View
Ron Natalie <ron@sensor.com> writes:

>Andrei Alexandrescu wrote:
>>
>> int main()
>> {
>>     A * p1 = 0, * p2 = 0;
>>     delete p1;
>>     delete[] p2;
>> }
>>
>> Will the two operator delete be called?

>That's a good question.  The spec appears to be
>contradictory on this point, but I'm leaning to
>believe htat it will.  The operator delete[] docs
>in chapter 18, specifically call out handling null
>pointer.
>
>However the delete expression documentation
>in Chapter 5 says calling delete with a null argument
>has no effect (no effect to me means don't do anything,
>let alone call the operator overload).

But that's not what "no effect" means in the standard.

The standard describes the behavior of an abstract machine.
The requirements on implementations are that they have the
same observable behavior as the abstract machine. Observable
behavior consists of reading or writing volatile objects,
and calls to library I/O functions. (See section 1.9.)

The standard says that deleting a null pointer has no effect.
That means that it has no observable behavior. A further
requirement on all versions of operator delete (including
user-written versions) is that when called with a null first
argument, they must have no effect. (See 3.7.3.2 and 5.3.5.)

Thus, the standard neither requires nor forbids that deleting
a null pointer call the corresponding operator delete. If the
function is called, it must have no observable effect. Thus,
you can't tell (without stepping outside the language rules)
whether the function is called, and in a conforming program
it won't matter whether it is called.

--
Steve Clamage, stephen.clamage@sun.com


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: "Andrei Alexandrescu" <alexandrescua@micromodeling.com>
Date: 1999/02/02
Raw View
Hi,

Very simple question!

class A
{
public:
    void operator delete(void *);
    void operator delete[](void *);
};

int main()
{
    A * p1 = 0, * p2 = 0;
    delete p1;
    delete[] p2;
}

Will the two operator delete be called?

Andrei




[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]






Author: Ron Natalie <ron@sensor.com>
Date: 1999/02/03
Raw View
Andrei Alexandrescu wrote:
>

> int main()
> {
>     A * p1 = 0, * p2 = 0;
>     delete p1;
>     delete[] p2;
> }
>
> Will the two operator delete be called?

That's a good question.  The spec appears to be
contradictory on this point, but I'm leaning to
believe htat it will.  The operator delete[] docs
in chapter 18, specifically call out handling null
pointer.
H
However the delete expression documentation
in Chapter 5 says calling delete with a null argument
has no effect (no effect to me means don't do anything,
let alone call the operator overload).  Now normally,
an overload totally changes the behavior of the built-in
behavior elsewhere in the spec, but delete is funny
(since even when overloaded other things happen).

However poking around at several compilers show that
all call the operator delete with a null pointer in
the above example
---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]





Author: sbnaran@localhost.localdomain.COM (Siemel Naran)
Date: 1999/02/03
Raw View
On 2 Feb 1999 17:42:26 GMT, Andrei Alexandrescu

>class A
>{
>public:
>    void operator delete(void *);
>    void operator delete[](void *);
>};
>
>int main()
>{
>    A * p1 = 0, * p2 = 0;
>    delete p1;
>    delete[] p2;
>}
>
>Will the two operator delete be called?

If the delete operators are inline, then it is easy for the compiler
to see that "delete p" where p==null has no effect.  Eg,
   inline void A::operator delete(void * ptr, size_t n)
   {
      if (!ptr) return;
      actual_delete(ptr,n); // call non-inline static function
   }

The same holds here
   inline void A::operator delete(void * ptr, size_t n)
   {
      return std::free(ptr);
   }
because it is well known that std::free(0) has no effect.


--
----------------------------------
Siemel B. Naran (sbnaran@uiuc.edu)
----------------------------------


[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://reality.sgi.com/austern_mti/std-c++/faq.html              ]