www.digitalmars.com         C & C++   DMDScript  

digitalmars.D - Can the compiler catch my stupid mistake?

reply "Jack Stouffer" <jack jackstouffer.com> writes:
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.


Thanks

----------------
enum State {
     ONE,
     TWO
}

struct TestA {
     private State *state;

     void method(ref State program_state) {
         this.state = &program_state;
     }

     this (int temp_arg) {
         import std.stdio;

         // Problem code
         if (*this.state == state.ONE)
             "ONE".writeln;
     }
}

void main() {
     State program_state = State.TWO;
     auto a = TestA(1);
     a.method(program_state);
}
Aug 14 2015
next sibling parent reply "Adam D. Ruppe" <destructionator gmail.com> writes:
Try compiling with -O, it sometimes catches these things in the 
optimization process (weird i know)
Aug 14 2015
parent reply "Jack Stouffer" <jack jackstouffer.com> writes:
On Friday, 14 August 2015 at 17:38:48 UTC, Adam D. Ruppe wrote:
 Try compiling with -O, it sometimes catches these things in the 
 optimization process (weird i know)
$ dmd -O test.d $ ./test.d [1] 1755 segmentation fault ./test :(
Aug 14 2015
parent "Jack Stouffer" <jack jackstouffer.com> writes:
On Friday, 14 August 2015 at 17:45:51 UTC, Jack Stouffer wrote:
 On Friday, 14 August 2015 at 17:38:48 UTC, Adam D. Ruppe wrote:
 Try compiling with -O, it sometimes catches these things in 
 the optimization process (weird i know)
$ dmd -O test.d $ ./test.d
Whoops, that should be ./test
Aug 14 2015
prev sibling parent reply Steven Schveighoffer <schveiguy yahoo.com> writes:
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
Aug 14 2015
parent reply "Jack Stouffer" <jack jackstouffer.com> writes:
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?
Aug 14 2015
parent reply "Meta" <jared771 gmail.com> writes:
On Friday, 14 August 2015 at 17:59:12 UTC, Jack Stouffer wrote:
 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?
You're dereferencing a null pointer
Aug 14 2015
parent reply "Jack Stouffer" <jack jackstouffer.com> writes:
On Friday, 14 August 2015 at 18:02:10 UTC, Meta wrote:
 You're dereferencing a null pointer
Oh, I see now, Thanks. Is there any way that the D runtime can throw a null pointer
Aug 14 2015
next sibling parent "deadalnix" <deadalnix gmail.com> writes:
On Friday, 14 August 2015 at 18:06:41 UTC, Jack Stouffer wrote:
 On Friday, 14 August 2015 at 18:02:10 UTC, Meta wrote:
 You're dereferencing a null pointer
Oh, I see now, Thanks. Is there any way that the D runtime can throw a null pointer
http://www.deadalnix.me/2012/03/24/get-an-exception-from-a-segfault-on-linux-x86-and-x86_64-using-some-black-magic/
Aug 14 2015
prev sibling next sibling parent "Temtaime" <temtaime gmail.com> writes:

D is executed on real hardware.

You can use your OS api to catch these exceptions, for windows it 
is SetUnhandledExceptionFilter.

But you will never be able to log for example line where this 
exception happend.
Aug 14 2015
prev sibling parent Steven Schveighoffer <schveiguy yahoo.com> writes:
On 8/14/15 2:06 PM, Jack Stouffer wrote:
 On Friday, 14 August 2015 at 18:02:10 UTC, Meta wrote:
 You're dereferencing a null pointer
Oh, I see now, Thanks. Is there any way that the D runtime can throw a null pointer exception,
Are you on Linux? If so there is an undocumented option for this: https://github.com/D-Programming-Language/druntime/blob/master/src/etc/linux/memoryerror.d#L33 Otherwise, no. And it won't be added (Walter has made this repeatedly and abundantly clear (and I agree with him) ). -Steve
Aug 14 2015