digitalmars.D - Can the compiler catch my stupid mistake?
- Jack Stouffer (28/28) Aug 14 2015 Recently, I made the mistake of trying to reference an enum
- Adam D. Ruppe (2/2) Aug 14 2015 Try compiling with -O, it sometimes catches these things in the
- Jack Stouffer (5/7) Aug 14 2015 $ dmd -O test.d
- Jack Stouffer (2/7) Aug 14 2015 Whoops, that should be ./test
- Steven Schveighoffer (6/10) Aug 14 2015 state *is* set to null, before your constructor is called. The access is...
- Jack Stouffer (5/17) Aug 14 2015 I'm confused. If state is set to null on struct initialization,
- Meta (2/23) Aug 14 2015 You're dereferencing a null pointer
- Jack Stouffer (4/5) Aug 14 2015 Oh, I see now, Thanks.
- deadalnix (2/7) Aug 14 2015 http://www.deadalnix.me/2012/03/24/get-an-exception-from-a-segfault-on-l...
- Temtaime (6/6) Aug 14 2015 Java / C# are executed under virtual machine.
- Steven Schveighoffer (7/12) Aug 14 2015 Are you on Linux?
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
Try compiling with -O, it sometimes catches these things in the optimization process (weird i know)
Aug 14 2015
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
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:Whoops, that should be ./testTry compiling with -O, it sometimes catches these things in the optimization process (weird i know)$ dmd -O test.d $ ./test.d
Aug 14 2015
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
On Friday, 14 August 2015 at 17:44:11 UTC, Steven Schveighoffer wrote:On 8/14/15 1:36 PM, Jack Stouffer wrote: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?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
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:You're dereferencing a null pointerOn 8/14/15 1:36 PM, Jack Stouffer wrote: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?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
On Friday, 14 August 2015 at 18:02:10 UTC, Meta wrote:You're dereferencing a null pointerOh, I see now, Thanks. Is there any way that the D runtime can throw a null pointer
Aug 14 2015
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:http://www.deadalnix.me/2012/03/24/get-an-exception-from-a-segfault-on-linux-x86-and-x86_64-using-some-black-magic/You're dereferencing a null pointerOh, I see now, Thanks. Is there any way that the D runtime can throw a null pointer
Aug 14 2015
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
On 8/14/15 2:06 PM, Jack Stouffer wrote:On Friday, 14 August 2015 at 18:02:10 UTC, Meta wrote: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) ). -SteveYou're dereferencing a null pointerOh, I see now, Thanks. Is there any way that the D runtime can throw a null pointer exception,
Aug 14 2015