Casting in Safe D
anonymous via Digitalmars-d-learn
digitalmars-d-learn at puremagic.com
Sun Nov 23 15:12:51 PST 2014
On Sunday, 23 November 2014 at 19:37:45 UTC, Nordlöw wrote:
> I just noticed that
>
> void foo() @safe pure nothrow
> {
> void[] void_array = new void[3];
> auto ubyte_array = cast(ubyte[])void_array;
> auto short_array = cast(short[])void_array;
> }
>
> compiles but gives a
>
> object.Error@(0): array cast misalignment
>
> because of the
>
> cast(short[])
>
> I'm surprised---why is cast between different alignments and
> element lengths allowed at all in Safe D?
As far as I understand, it's @safe because it's guaranteed to
fail at run time on mismatches.
Similarly, array accesses are @safe because invalid ones will
throw RangeError:
void foo(int[] a) @safe {a[100] = 13;}
And even pointer dereferencing is @safe. Invalid ones will fail
with a segfault at run time:
void foo(int* a) @safe {*a = 13;}
More information about the Digitalmars-d-learn
mailing list