This is the mail archive of the
gcc@gcc.gnu.org
mailing list for the GCC project.
Re: Null pointer check elimination
There are several examples. One is converting from a derived class
to a base class when there is multiple inheritance. An offset must
be subtracted, unless it is a null pointer.
Why does it matter if the pointer is null? It is an error
in the program if it uses the result, but the same is true for
using null pointers.
No, because for example if the base class is passed to a function as an
argument, you want that function to be able to distinguish if its
argument is a NULL pointer.
Another is the "delete" operator. It must first check that the
argument is null; it only calls the underlying memory allocator if it is
not.
I have a similar problem with this.
... for example, if you pass the base class to delete, it must detect
that it is a NULL pointer and not invoke undefined behavior.
In other words, something like this (untested) program
#include <iostream>
class A { int x, y; };
class B : virtual public A { int z, w; };
class C : virtual public A { int j, k; };
class D : virtual public B, C { };
void f(A *a)
{
if (a == NULL)
std::cout << "a is NULL, but it works the same!\n";
delete a;
}
void g(D *d)
{
f (static_cast <A *> (d));
}
int main(void)
{
g (NULL);
}
must work. operator delete must not try to free the NULL pointer, and
the message should be displayed.
Paolo