Safe cast of arrays

Iakh via Digitalmars-d digitalmars-d at puremagic.com
Wed Feb 10 13:40:21 PST 2016


On Wednesday, 10 February 2016 at 20:14:29 UTC, Chris Wright 
wrote:
> @safe protects you from segmentation faults and reading and 
> writing outside an allocated segment of memory. With array 
> casts, @safety is assured

Yes, @safe protects from direct cast to/from ref types but there
still is a trick with T[] -> void[] -> T2[] cast:

import std.stdio;

int[] f(void[] a) @safe pure
{
     return cast(int[])a;
}

struct S
{
     int* a;
}

void main() @safe
{
     S[] a = new S[4];

     immutable b = a.f();
     writeln(b);
     a[0].a = new int;
     writeln(b);
     //b[0] = 0;
     //writeln(*a[0].a);

}

So no safety in this world.


More information about the Digitalmars-d mailing list