Implicit static->dynamic arr and modifying

Nick Sabalausky via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Mon May 5 19:16:58 PDT 2014


On 5/5/2014 10:11 PM, Nick Sabalausky wrote:
> Is this kinds stuff a sane thing to do, or does it just work by accident?:
>
> void modify(ubyte[] dynamicArr)
> {
>      dynamicArr[$-1] = 5;
> }
>
> void main()
> {
>      ubyte[4] staticArr = [1,1,1,1];
>      modify(staticArr);
>      assert(staticArr == [1,1,1,5]);
> }

Duh, it's just using a normal slice of the static array...

// Roughly:
dynamicArr.ptr = &staticArr;
dynamicArr.length = typeof(staticArr).sizeof;

So all is well, and deliberately so. Pardon the noise.



More information about the Digitalmars-d-learn mailing list