Assigning a static array
Jonathan M Davis
jmdavisProg at gmx.com
Thu Apr 18 14:39:12 PDT 2013
On Thursday, April 18, 2013 23:06:32 Brad Anderson wrote:
> Is this supposed to be allowed:
>
> ubyte[] a;
> ubyte[16] b;
> a = b;
> assert(a.ptr == b.ptr);
>
> Because if so that makes it terribly easy to do a bug like this
> (as I just saw in IRC):
>
> struct A
> {
> ubyte[] a;
> this(ubyte c)
> {
> ubyte[16] b;
> b[] = c;
> this.a = b; // a now points at an immediately invalid
> static array
> }
> }
Yes, that's legal, though it should arguably be @system, since it's doing
essentially the same thing as
int i;
int* p = &i;
which _is_ @system. The compiler doesn't currently consider slicing a static
array in general to be @system though, much as it should (
http://d.puremagic.com/issues/show_bug.cgi?id=8838 ). I could see an argument
that it should have to be
a = b[];
so that the slicing is explicit instead of just
a = b;
where it's implicit, but AFAIK, that's not currently required. You have to be
very careful when slicing static arrays. If it were up to me, exlicit slicing
would _always_ be required when slicing a static array, even when calling
functions which take a dynamic array, but that's not how it works
unfortunately.
- Jonathan M Davis
More information about the Digitalmars-d-learn
mailing list