title: Cannot marshal a struct that contains a union
body: <p>I have a C++ struct that looks like this:</p>
  
  <pre><code>struct unmanagedstruct
  {
      int             flags;
      union
      {
          int             offset[6];
          struct
          {
              float           pos[3];
              float           q[4];
          } posedesc;
      } u;
  };
  </code></pre>
  
  <p>And I'm trying to Marshal it like so in C#:</p>
  
  <pre><code>[StructLayout(LayoutKind.Explicit)]
  public class managedstruct {
      [FieldOffset(0)]
      public int flags;
  
      [FieldOffset(4), MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 6)]
      public int[] offset;
  
      [StructLayout(LayoutKind.Explicit)]
      public struct posedesc {
          [FieldOffset(0), MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 3)]
          public float[] pos;
  
          [FieldOffset(12), MarshalAsAttribute(UnmanagedType.ByValArray, SizeConst = 4)]
          public float[] q;
      }
  
      [FieldOffset(4)]
      public posedesc pose;
  }
  </code></pre>
  
  <p>However, when I try loading data into my struct only the first 3 elements of the <strong>offset</strong> array are there (the array's length is 3). I can confirm that their values are correct - but I still need the other 3 elements. Am I doing something obviously wrong?</p>
  
  <p>I'm using these functions to load the struct:</p>
  
  <pre><code>private static IntPtr addOffset(IntPtr baseAddress, int byteOffset) {
      switch (IntPtr.Size) {
          case 4:
              return new IntPtr(baseAddress.ToInt32() + byteOffset);
          case 8:
              return new IntPtr(baseAddress.ToInt64() + byteOffset);
          default:
              throw new NotImplementedException();
      }
  }
  
  public static T loadStructData&lt;T&gt;(byte[] data, int byteOffset) {
      GCHandle pinnedData = GCHandle.Alloc(data, GCHandleType.Pinned);
      T output = (T)Marshal.PtrToStructure(addOffset(pinnedData.AddrOfPinnedObject(), byteOffset), typeof(T));
      pinnedData.Free();
      return output;
  }
  </code></pre>
  
  <p>Loading example:</p>
  
  <pre><code>managedstruct mystruct = loadStructData&lt;managedstruct&gt;(buffer, 9000);
  </code></pre>
  
  <p>Let me know if you need more information.</p>