Segfault when casting array of Interface types

rcor via Digitalmars-d-learn digitalmars-d-learn at puremagic.com
Tue Sep 16 10:59:50 PDT 2014


On Tuesday, 16 September 2014 at 14:13:48 UTC, Marc Schütz wrote:
> On Tuesday, 16 September 2014 at 11:26:05 UTC, rcor wrote:
>>
>> Is to! creating a new array of pointers while cast isn't? This 
>> isn't a performance critical section and it's not a huge 
>> array, so I ask mostly out of curiosity.
>
> Yes, it is. (Probably. I don't have the time to test it now, 
> but it's likely if my theory about the pointer adjustment is 
> correct.)

I guess I could have checked that out myself:

import std.stdio;
import std.conv;

interface I {}
class C : I {}
class D : I {}

void main() {
   C[] c = [new C, new C];
   I[] i = cast(I[]) c;
   I[] i2 = c.to!(I[]);
   assert(c is cast(C[]) i);    // i and c point to same address
   assert(i !is i2);            // to! appears to create a new 
array
}


More information about the Digitalmars-d-learn mailing list