Can the compiler catch my stupid mistake?

Jack Stouffer via Digitalmars-d digitalmars-d at puremagic.com
Fri Aug 14 10:59:10 PDT 2015


On Friday, 14 August 2015 at 17:44:11 UTC, Steven Schveighoffer 
wrote:
> On 8/14/15 1:36 PM, Jack Stouffer wrote:
>> Recently, I made the mistake of trying to reference an enum 
>> pointer in a
>> struct before it was set (see example below). I was wondering 
>> if it's
>> possible for DMD to catch this mistake at compile time, as this
>> currently compiles fine and segfaults on execution.
>
> state *is* set to null, before your constructor is called. The 
> access is not "before it was set".
>
> Could the compiler *possibly* flag this as an error? It could, 
> but it doesn't, and likely won't in the future.
>
> -Steve

I'm confused. If state is set to null on struct initialization, 
then why does accessing that memory segfault? Shouldn't the if 
condition just fail?


More information about the Digitalmars-d mailing list