How does D handle null pointers?

Adam B cruxic at gmail.com
Fri Aug 20 14:03:59 PDT 2010


Hello all,

Yesterday I started playing with D for the first time, (I'm a Java &
C++ programmer by day and a python programmer by night).  In my first
experiments I wanted to prove that:

  1) Exceptions have stack traces (so you can tell *where* the error occurred!)
  2) Indexing into an array causes an exception instead of a segfault
or memory corruption
  3) Dereferencing null pointers causes an exception instead of a segfault

D 2.0 seems to handle my first two tests just fine but this program
causes a segfault:

------------
class A
{
    int i;
}

int main(char[][] args)
{
    A a = null;
    a.i++;
    return 0;
}
------------

I realize that checking every pointer dereference is expensive but, in
my experience, null pointer dereferences are one of the top 5 runtime
errors.  At work I troubleshoot java NullPointerExceptions on a weekly
basis.  Can you imagine trying to troubleshoot a massive web
application that crashes with the phrase "Segmentation Fault" after
running just fine for two weeks?  Without a stack trace it's not
reasonable.

Does D address this problem?  Perhaps another compiler switch similar
to the existing -noboundscheck ?

Cheers
- Adam B


More information about the Digitalmars-d mailing list