assigment to null class object member compiled? is this a bug?

Vijay Nayar madric at gmail.com
Fri Oct 19 09:08:32 UTC 2018


On Friday, 19 October 2018 at 06:53:32 UTC, dangbinghoo wrote:
> hi,
>
> why the code bellow compiles?
>
> ---
> import std.stdio;
> class A {
>     int m;
> }
>
> void main() {
>     A a;
>     a.m = 1;
> }
> ---
>
> and running this code get:
>
> `segmentation fault (core dumped)  ./test`
>
> I consider this couldn't be compiled according to book <The D 
> Programming Language>.
>
> The latest dmd (2.082) and LDC2 behaves the same.

Technically the code you have is syntactically correct.  You are 
permitted to create a class variable without assigning it to a 
class object.  (Assigning it to a class object would look like "A 
a = new A();")

Which section of The D Programming Language book makes you think 
this would not compile?  I have the book as well, but I'm not 
quite sure what part of the book you're referring to.


More information about the Digitalmars-d-learn mailing list