How does D handle null pointers?

dsimcha dsimcha at yahoo.com
Fri Aug 20 16:26:57 PDT 2010


== Quote from Adam B (cruxic at gmail.com)'s article
> 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 occu
> rred!)
>   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 segfaul
> t
> 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

Currently null pointer dereferences do just segfault your program.  IMHO this
needs to be fixed eventually.  I'd love to see something analogous to array bounds
checking where null pointers are checked for in debug mode and these checks are
turned off in release mode.


More information about the Digitalmars-d mailing list