Array Slices and Interior Pointers

Era Scarecrow rtcvb32 at yahoo.com
Tue Dec 11 13:58:59 PST 2012


On Tuesday, 11 December 2012 at 18:11:32 UTC, Robert Jacques 
wrote:
> On Tue, 11 Dec 2012 11:25:44 -0600, Alex Rønne Petersen wrote:
>> Interior pointers are OK in the stack and registers, so taking 
>> pointers to fields inside aggregates should be fine so long as 
>> they are not stored in the heap.
>
> So what about unions?

  The pointer & lengths won't work well together if you mix them. 
Consider.

   struct S {
     union {
       int[] i;
       byte[] b;
     }
   }

   S s;

   s.i.length = 4;
   assert(s.i.length == 4);
   assert(s.b.length == 16); //fails
   assert(s.b.length == 4);  //the implementation

   s.b = cast(byte[]) s.i;
   assert(s.b.length == 16); //true
   assert(s.i.length == 4);  //fails
   assert(s.i.length == 16); //the implementation (last twelve 
Sigfaults probably)

  The only way to properly use that is to have one of the data 
types you always convert from/to, but the GC wouldn't know and 
might try them all; Although only the base pointer might be 
considered so...


More information about the Digitalmars-d mailing list