On Monday, 8 February 2016 at 19:07:01 UTC, Iakh wrote:
import std.stdio;
struct S
{
void[] arr;
auto f() pure @safe
{
int[] a = new int[4];
arr = a;
return a;
}
}
void main() @safe
{
S s;
immutable a = s.f();
int[] b = (cast(int[])s.arr);
writeln(a);
b[0] = 1;
writeln(a);
}
http://dpaste.dzfl.pl/13751913d2ff
I'm pretty sure this is not safe. Works, but not safe. You happen
to be referencing the same memory block you created on the heap,
but I believe the compiler is free to move the memory around if
it can find a reason to do so.
Remember, when you cast the compiler is no longer protecting you.
By modifying the array you're taking on the responsibility to
know where that memory is located.