Why does this simple program segfault?

Chad J gamerChad at _spamIsBad_gmail.com
Tue Aug 14 21:05:57 PDT 2007


Bill Baxter wrote:
> Robert Fraser wrote:
>> Bill Baxter Wrote:
>>
>>> Isn't there something we can do about this?  I do this about once a 
>>> day when in a heavy D using phase.  It bites newbies and 
>>> not-so-newbies alike.  I almost think it should be made so that no 
>>> initializer calls the default constructor, and if you really want it 
>>> to be null then you should initialize with null:
>>>
>>>     Tester fun;  // creates a new Tester
>>>     Tester nofun = null;  // doesn't create anything
>>
>> But then I (coming from a Java background) would be bitten by it about 
>> once a day.
>>
>> I think the best way is to make a compiler warning for every local 
>> variable that's used before it's explicitly assigned, a la Java 
>> (actually, it's an error in Java).
> 
> Assuming it applied only to classes in D, that seems like a good idea.
> 
> --bb

Probably a good idea.

I hesitated because I sometimes use null as a sentinel value, but null 
can be assigned explicitly, so all is good.

I think a worse problem though is how D errors on null dereferencing. 
On windows I get Access Violations, and I think the segv is a linux 
thing if I remember correctly.  Such error messages are completely 
unhelpful while debugging.  The following should probably be done:

instance.member = something;

becomes

assert(instance !is null,
   format("%s,%d: %s is null!",
   __FILE__,__LINE__,instance.stringof)
   );
instance.member = something;

It would be sort of like how arrays are bounds checked at runtime unless 
in release mode.


More information about the Digitalmars-d-learn mailing list