null dereference exception vs. segfault?

Ryan W Sims rwsims at gmail.com
Sun Aug 1 21:59:42 PDT 2010


The following code fails with a "Bus error" (OSX speak for "Segfault," 
if I understand correctly).

// types.d
import std.stdio;

class A {
     int x = 42;
}

void fail_sometimes(int n) {
     A a;
     if (n == 0) {
         a = new A;  // clearly a contrived example
     }
     assert(a.x == 42, "Wrong x value");
}

void main() {
     fail_sometimes(1);
}

It's even worse if I do a 'dmd -run types.d', it just fails without even 
the minimalistic "Bus error." Is this correct behavior? I searched the 
archives & looked at the FAQ & found workarounds (registering a signal 
handler), but not a justification, and the threads were from a couple 
years ago. Wondering if maybe something has changed and there's a 
problem with my system?

--
rwsims


More information about the Digitalmars-d-learn mailing list