Shouldn't invalid references like this fail at compile time?
Mike Franklin
slavo5150 at yahoo.com
Tue Jan 23 00:20:45 UTC 2018
On Monday, 22 January 2018 at 23:30:16 UTC, Aedt wrote:
> I was asked in Reddit
> (https://www.reddit.com/r/learnprogramming/comments/7ru82l/i_was_thinking_of_using_d_haxe_or_another/) how would D handle the following similar D code. I'm surprised that both dmd and ldc provides no warnings even with -w argument passed.
>
> import std.stdio;
>
> void main()
> {
> string foo = "foo";
> string* p1, p2;
>
> string*[] ls;
> ls ~= &foo;
> p1 = ls[0];
> ls.destroy();
> p2 = ls[0];
> writeln(p2);
>
> }
D is not memory safe by default (unfortunately), so it's not
surprising to me that you can do this in `@system` code. I would
be surprised if the compiler allowed you to do something like
this in `@safe` code. To make your programs memory safe, you
should add `@safe` to your `main` function.
Mike
More information about the Digitalmars-d
mailing list