| Thread overview | |||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
August 14, 2015 Can the compiler catch my stupid mistake? | ||||
|---|---|---|---|---|
| ||||
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);
}
| ||||
August 14, 2015 Re: Can the compiler catch my stupid mistake? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jack Stouffer | Try compiling with -O, it sometimes catches these things in the optimization process (weird i know) | |||
August 14, 2015 Re: Can the compiler catch my stupid mistake? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jack Stouffer | 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
| |||
August 14, 2015 Re: Can the compiler catch my stupid mistake? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Adam D. Ruppe | 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
:(
| |||
August 14, 2015 Re: Can the compiler catch my stupid mistake? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jack Stouffer | 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
| |||
August 14, 2015 Re: Can the compiler catch my stupid mistake? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Steven Schveighoffer | 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?
| |||
August 14, 2015 Re: Can the compiler catch my stupid mistake? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jack Stouffer | 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
| |||
August 14, 2015 Re: Can the compiler catch my stupid mistake? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Meta | 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, a la Java or C#?
| |||
August 14, 2015 Re: Can the compiler catch my stupid mistake? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jack Stouffer | 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 exception, a la Java or C#? http://www.deadalnix.me/2012/03/24/get-an-exception-from-a-segfault-on-linux-x86-and-x86_64-using-some-black-magic/ | |||
August 14, 2015 Re: Can the compiler catch my stupid mistake? | ||||
|---|---|---|---|---|
| ||||
Posted in reply to Jack Stouffer | Java / C# are executed under virtual machine. 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. | |||
Copyright © 1999-2021 by the D Language Foundation
Permalink
Reply