Union Struct Yields Error

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Mike

    Union Struct Yields Error

    The following struct, DataStruct, is only part of a larger one that contains
    additional fields and arrays. I need the explicit layout because this
    struct is really a union, where some of the missing fields and arrays
    overlap. What's shown here, though, is sufficient for explaining the error.

    290 bytes of data come from a serial device and is to be placed in this
    struct. Hence, I want this struct to be 290 bytes in size, and, if I'm
    adding this right, each of the fields (DataMemb1 thru DataMemb17) are 2
    bytes each and the 128-element array of short values, DataArray, is 256
    bytes. When I try to use the struct, however, I get the following error:

    "Could not load type DataStruct from assembly <filename>,
    Version=1.0.0.2 3184, Culture=neutral , PublicKeyToken= null because it
    contains an object field at offset 34 that is incorrectly aligned or
    overlapped by a non-object field."

    Of course, offset 34 is where the short array, DataArray, begins. If I
    manually change it to FieldOffset( 36 ), this error disappears, but I
    encounter other problems because the size of the incoming data buffer, 290
    bytes, is not equal to the size of the struct, which is now 292 bytes.

    Can anyone shed some light on this problem? Thank you!


    -Mike


    [ StructLayout( LayoutKind.Expl icit ) ]
    public struct DataStruct
    {
    [ FieldOffset( 0 ) ]
    public ushort DataMemb1;

    [ FieldOffset( 2 ) ]
    public ushort DataMemb2;

    [ FieldOffset( 4 ) ]
    public ushort DataMemb3;

    [ FieldOffset( 6 ) ]
    public ushort DataMemb4;

    [ FieldOffset( 8 ) ]
    public ushort DataMemb5;

    [ FieldOffset( 10 ) ]
    public short DataMemb6;

    [ FieldOffset( 12 ) ]
    public short DataMemb7;

    [ FieldOffset( 14 ) ]
    public ushort DataMemb8;

    [ FieldOffset( 16 ) ]
    public short DataMemb9;

    [ FieldOffset( 18 ) ]
    public short DataMemb10;

    [ FieldOffset( 20 ) ]
    public ushort DataMemb11;

    [ FieldOffset( 22 ) ]
    public short DataMemb12;

    [ FieldOffset( 24 ) ]
    public short DataMemb13;

    [ FieldOffset( 26 ) ]
    public short DataMemb14;

    [ FieldOffset( 28 ) ]
    public short DataMemb15;

    [ FieldOffset( 30 ) ]
    public ushort DataMemb16;

    [ FieldOffset( 32 ) ]
    public ushort DataMemb17;

    [ MarshalAs( UnmanagedType.B yValArray, SizeConst = 128 ) ]
    [ FieldOffset( 34 ) ]
    public short[] DataArray;
    }


  • Grant Richins [MS]

    #2
    Re: Union Struct Yields Error

    34 % sizeof(void*) != 0. The array must begin on an natural boundary (like
    36). You could always read the data out of the serial port 2 bytes at a
    type and then allow the runtime to layout the struct more naturally.

    --
    --Grant
    This posting is provided "AS IS" with no warranties, and confers no rights.


    "Mike" <mike@bogus.net > wrote in message
    news:%23d%23Tdf UfDHA.1648@TK2M SFTNGP09.phx.gb l...[color=blue]
    > The following struct, DataStruct, is only part of a larger one that[/color]
    contains[color=blue]
    > additional fields and arrays. I need the explicit layout because this
    > struct is really a union, where some of the missing fields and arrays
    > overlap. What's shown here, though, is sufficient for explaining the[/color]
    error.[color=blue]
    >
    > 290 bytes of data come from a serial device and is to be placed in this
    > struct. Hence, I want this struct to be 290 bytes in size, and, if I'm
    > adding this right, each of the fields (DataMemb1 thru DataMemb17) are 2
    > bytes each and the 128-element array of short values, DataArray, is 256
    > bytes. When I try to use the struct, however, I get the following error:
    >
    > "Could not load type DataStruct from assembly <filename>,
    > Version=1.0.0.2 3184, Culture=neutral , PublicKeyToken= null because it
    > contains an object field at offset 34 that is incorrectly aligned or
    > overlapped by a non-object field."
    >
    > Of course, offset 34 is where the short array, DataArray, begins. If I
    > manually change it to FieldOffset( 36 ), this error disappears, but I
    > encounter other problems because the size of the incoming data buffer, 290
    > bytes, is not equal to the size of the struct, which is now 292 bytes.
    >
    > Can anyone shed some light on this problem? Thank you!
    >
    >
    > -Mike
    >
    >
    > [ StructLayout( LayoutKind.Expl icit ) ]
    > public struct DataStruct
    > {
    > [ FieldOffset( 0 ) ]
    > public ushort DataMemb1;
    >
    > [ FieldOffset( 2 ) ]
    > public ushort DataMemb2;
    >
    > [ FieldOffset( 4 ) ]
    > public ushort DataMemb3;
    >
    > [ FieldOffset( 6 ) ]
    > public ushort DataMemb4;
    >
    > [ FieldOffset( 8 ) ]
    > public ushort DataMemb5;
    >
    > [ FieldOffset( 10 ) ]
    > public short DataMemb6;
    >
    > [ FieldOffset( 12 ) ]
    > public short DataMemb7;
    >
    > [ FieldOffset( 14 ) ]
    > public ushort DataMemb8;
    >
    > [ FieldOffset( 16 ) ]
    > public short DataMemb9;
    >
    > [ FieldOffset( 18 ) ]
    > public short DataMemb10;
    >
    > [ FieldOffset( 20 ) ]
    > public ushort DataMemb11;
    >
    > [ FieldOffset( 22 ) ]
    > public short DataMemb12;
    >
    > [ FieldOffset( 24 ) ]
    > public short DataMemb13;
    >
    > [ FieldOffset( 26 ) ]
    > public short DataMemb14;
    >
    > [ FieldOffset( 28 ) ]
    > public short DataMemb15;
    >
    > [ FieldOffset( 30 ) ]
    > public ushort DataMemb16;
    >
    > [ FieldOffset( 32 ) ]
    > public ushort DataMemb17;
    >
    > [ MarshalAs( UnmanagedType.B yValArray, SizeConst = 128 ) ]
    > [ FieldOffset( 34 ) ]
    > public short[] DataArray;
    > }
    >
    >[/color]


    Comment

    • Mike

      #3
      Re: Union Struct Yields Error

      The data is read into a byte[] from the serial port without any trouble, and
      before it gets to the point where it moves the data into the struct, the
      error occurs. Since the error message indicates a problem in loading it, it
      seems that there's something about the layout that isn't right.

      Interestingly, when I use this struct with LayoutKind.Sequ ential (not
      requiring the union of remaining data members), it works fine.

      Any ideas?


      -Mike

      "Grant Richins [MS]" <grantri@online .microsoft.com> wrote in message
      news:%231bEoGXf DHA.616@TK2MSFT NGP11.phx.gbl.. .[color=blue]
      > 34 % sizeof(void*) != 0. The array must begin on an natural boundary[/color]
      (like[color=blue]
      > 36). You could always read the data out of the serial port 2 bytes at a
      > type and then allow the runtime to layout the struct more naturally.
      >
      > --
      > --Grant
      > This posting is provided "AS IS" with no warranties, and confers no[/color]
      rights.[color=blue]
      >
      >
      > "Mike" <mike@bogus.net > wrote in message
      > news:%23d%23Tdf UfDHA.1648@TK2M SFTNGP09.phx.gb l...[color=green]
      > > The following struct, DataStruct, is only part of a larger one that[/color]
      > contains[color=green]
      > > additional fields and arrays. I need the explicit layout because this
      > > struct is really a union, where some of the missing fields and arrays
      > > overlap. What's shown here, though, is sufficient for explaining the[/color]
      > error.[color=green]
      > >
      > > 290 bytes of data come from a serial device and is to be placed in this
      > > struct. Hence, I want this struct to be 290 bytes in size, and, if I'm
      > > adding this right, each of the fields (DataMemb1 thru DataMemb17) are 2
      > > bytes each and the 128-element array of short values, DataArray, is 256
      > > bytes. When I try to use the struct, however, I get the following[/color][/color]
      error:[color=blue][color=green]
      > >
      > > "Could not load type DataStruct from assembly <filename>,
      > > Version=1.0.0.2 3184, Culture=neutral , PublicKeyToken= null because it
      > > contains an object field at offset 34 that is incorrectly aligned or
      > > overlapped by a non-object field."
      > >
      > > Of course, offset 34 is where the short array, DataArray, begins. If I
      > > manually change it to FieldOffset( 36 ), this error disappears, but I
      > > encounter other problems because the size of the incoming data buffer,[/color][/color]
      290[color=blue][color=green]
      > > bytes, is not equal to the size of the struct, which is now 292 bytes.
      > >
      > > Can anyone shed some light on this problem? Thank you!
      > >
      > >
      > > -Mike
      > >
      > >
      > > [ StructLayout( LayoutKind.Expl icit ) ]
      > > public struct DataStruct
      > > {
      > > [ FieldOffset( 0 ) ]
      > > public ushort DataMemb1;
      > >
      > > [ FieldOffset( 2 ) ]
      > > public ushort DataMemb2;
      > >
      > > [ FieldOffset( 4 ) ]
      > > public ushort DataMemb3;
      > >
      > > [ FieldOffset( 6 ) ]
      > > public ushort DataMemb4;
      > >
      > > [ FieldOffset( 8 ) ]
      > > public ushort DataMemb5;
      > >
      > > [ FieldOffset( 10 ) ]
      > > public short DataMemb6;
      > >
      > > [ FieldOffset( 12 ) ]
      > > public short DataMemb7;
      > >
      > > [ FieldOffset( 14 ) ]
      > > public ushort DataMemb8;
      > >
      > > [ FieldOffset( 16 ) ]
      > > public short DataMemb9;
      > >
      > > [ FieldOffset( 18 ) ]
      > > public short DataMemb10;
      > >
      > > [ FieldOffset( 20 ) ]
      > > public ushort DataMemb11;
      > >
      > > [ FieldOffset( 22 ) ]
      > > public short DataMemb12;
      > >
      > > [ FieldOffset( 24 ) ]
      > > public short DataMemb13;
      > >
      > > [ FieldOffset( 26 ) ]
      > > public short DataMemb14;
      > >
      > > [ FieldOffset( 28 ) ]
      > > public short DataMemb15;
      > >
      > > [ FieldOffset( 30 ) ]
      > > public ushort DataMemb16;
      > >
      > > [ FieldOffset( 32 ) ]
      > > public ushort DataMemb17;
      > >
      > > [ MarshalAs( UnmanagedType.B yValArray, SizeConst = 128 ) ]
      > > [ FieldOffset( 34 ) ]
      > > public short[] DataArray;
      > > }
      > >
      > >[/color]
      >
      >[/color]


      Comment

      • Grant Richins [MS]

        #4
        Re: Union Struct Yields Error

        Yes, the runtime won't load the struct because it has 'invalid layout'. The
        invalid layout is caused by trying to put a managed object (the array) at an
        illegal offset. When you use LayoutKind.Sequ ential, the runtime will
        automatically put in the appropriate padding so that the array starts at a
        natural boundary, but then as you rightly pointed out, the managed struct
        will not have the same layout as the raw bytes (it will have 2 bytes of
        padding between DataMemb17 and DataArray.

        --
        --Grant
        This posting is provided "AS IS" with no warranties, and confers no rights.


        "Mike" <mike@bogus.net > wrote in message
        news:eGhf2qffDH A.2252@TK2MSFTN GP12.phx.gbl...[color=blue]
        > The data is read into a byte[] from the serial port without any trouble,[/color]
        and[color=blue]
        > before it gets to the point where it moves the data into the struct, the
        > error occurs. Since the error message indicates a problem in loading it,[/color]
        it[color=blue]
        > seems that there's something about the layout that isn't right.
        >
        > Interestingly, when I use this struct with LayoutKind.Sequ ential (not
        > requiring the union of remaining data members), it works fine.
        >
        > Any ideas?
        >
        >
        > -Mike
        >
        > "Grant Richins [MS]" <grantri@online .microsoft.com> wrote in message
        > news:%231bEoGXf DHA.616@TK2MSFT NGP11.phx.gbl.. .[color=green]
        > > 34 % sizeof(void*) != 0. The array must begin on an natural boundary[/color]
        > (like[color=green]
        > > 36). You could always read the data out of the serial port 2 bytes at a
        > > type and then allow the runtime to layout the struct more naturally.
        > >
        > > --
        > > --Grant
        > > This posting is provided "AS IS" with no warranties, and confers no[/color]
        > rights.[color=green]
        > >
        > >
        > > "Mike" <mike@bogus.net > wrote in message
        > > news:%23d%23Tdf UfDHA.1648@TK2M SFTNGP09.phx.gb l...[color=darkred]
        > > > The following struct, DataStruct, is only part of a larger one that[/color]
        > > contains[color=darkred]
        > > > additional fields and arrays. I need the explicit layout because this
        > > > struct is really a union, where some of the missing fields and arrays
        > > > overlap. What's shown here, though, is sufficient for explaining the[/color]
        > > error.[color=darkred]
        > > >
        > > > 290 bytes of data come from a serial device and is to be placed in[/color][/color][/color]
        this[color=blue][color=green][color=darkred]
        > > > struct. Hence, I want this struct to be 290 bytes in size, and, if[/color][/color][/color]
        I'm[color=blue][color=green][color=darkred]
        > > > adding this right, each of the fields (DataMemb1 thru DataMemb17) are[/color][/color][/color]
        2[color=blue][color=green][color=darkred]
        > > > bytes each and the 128-element array of short values, DataArray, is[/color][/color][/color]
        256[color=blue][color=green][color=darkred]
        > > > bytes. When I try to use the struct, however, I get the following[/color][/color]
        > error:[color=green][color=darkred]
        > > >
        > > > "Could not load type DataStruct from assembly <filename>,
        > > > Version=1.0.0.2 3184, Culture=neutral , PublicKeyToken= null because it
        > > > contains an object field at offset 34 that is incorrectly aligned or
        > > > overlapped by a non-object field."
        > > >
        > > > Of course, offset 34 is where the short array, DataArray, begins. If[/color][/color][/color]
        I[color=blue][color=green][color=darkred]
        > > > manually change it to FieldOffset( 36 ), this error disappears, but I
        > > > encounter other problems because the size of the incoming data buffer,[/color][/color]
        > 290[color=green][color=darkred]
        > > > bytes, is not equal to the size of the struct, which is now 292 bytes.
        > > >
        > > > Can anyone shed some light on this problem? Thank you!
        > > >
        > > >
        > > > -Mike
        > > >
        > > >
        > > > [ StructLayout( LayoutKind.Expl icit ) ]
        > > > public struct DataStruct
        > > > {
        > > > [ FieldOffset( 0 ) ]
        > > > public ushort DataMemb1;
        > > >
        > > > [ FieldOffset( 2 ) ]
        > > > public ushort DataMemb2;
        > > >
        > > > [ FieldOffset( 4 ) ]
        > > > public ushort DataMemb3;
        > > >
        > > > [ FieldOffset( 6 ) ]
        > > > public ushort DataMemb4;
        > > >
        > > > [ FieldOffset( 8 ) ]
        > > > public ushort DataMemb5;
        > > >
        > > > [ FieldOffset( 10 ) ]
        > > > public short DataMemb6;
        > > >
        > > > [ FieldOffset( 12 ) ]
        > > > public short DataMemb7;
        > > >
        > > > [ FieldOffset( 14 ) ]
        > > > public ushort DataMemb8;
        > > >
        > > > [ FieldOffset( 16 ) ]
        > > > public short DataMemb9;
        > > >
        > > > [ FieldOffset( 18 ) ]
        > > > public short DataMemb10;
        > > >
        > > > [ FieldOffset( 20 ) ]
        > > > public ushort DataMemb11;
        > > >
        > > > [ FieldOffset( 22 ) ]
        > > > public short DataMemb12;
        > > >
        > > > [ FieldOffset( 24 ) ]
        > > > public short DataMemb13;
        > > >
        > > > [ FieldOffset( 26 ) ]
        > > > public short DataMemb14;
        > > >
        > > > [ FieldOffset( 28 ) ]
        > > > public short DataMemb15;
        > > >
        > > > [ FieldOffset( 30 ) ]
        > > > public ushort DataMemb16;
        > > >
        > > > [ FieldOffset( 32 ) ]
        > > > public ushort DataMemb17;
        > > >
        > > > [ MarshalAs( UnmanagedType.B yValArray, SizeConst = 128 ) ]
        > > > [ FieldOffset( 34 ) ]
        > > > public short[] DataArray;
        > > > }
        > > >
        > > >[/color]
        > >
        > >[/color]
        >
        >[/color]


        Comment

        • Mike

          #5
          Re: Union Struct Yields Error

          Thanks for your reply, Grant.

          Please explain how this works:

          1. Why is the padding necessary, and how does one know
          when and how much it's used? (I have other arrays and
          data members to add to the union.)

          2. If the padding is automatic with LayoutKind.Sequ ential,
          then why is the struct's size still 290 bytes?

          3. Earlier, you mentioned letting the runtime layout the
          struct naturally. How do I expect it to do this?

          Thanks.


          -Mike
          [color=blue]
          >-----Original Message-----
          >Yes, the runtime won't load the struct because it[/color]
          has 'invalid layout'. The[color=blue]
          >invalid layout is caused by trying to put a managed[/color]
          object (the array) at an[color=blue]
          >illegal offset. When you use LayoutKind.Sequ ential, the[/color]
          runtime will[color=blue]
          >automaticall y put in the appropriate padding so that the[/color]
          array starts at a[color=blue]
          >natural boundary, but then as you rightly pointed out,[/color]
          the managed struct[color=blue]
          >will not have the same layout as the raw bytes (it will[/color]
          have 2 bytes of[color=blue]
          >padding between DataMemb17 and DataArray.
          >
          >--
          >--Grant
          >This posting is provided "AS IS" with no warranties, and[/color]
          confers no rights.[color=blue]
          >
          >
          >"Mike" <mike@bogus.net > wrote in message
          >news:eGhf2qffD HA.2252@TK2MSFT NGP12.phx.gbl.. .[color=green]
          >> The data is read into a byte[] from the serial port[/color][/color]
          without any trouble,[color=blue]
          >and[color=green]
          >> before it gets to the point where it moves the data[/color][/color]
          into the struct, the[color=blue][color=green]
          >> error occurs. Since the error message indicates a[/color][/color]
          problem in loading it,[color=blue]
          >it[color=green]
          >> seems that there's something about the layout that[/color][/color]
          isn't right.[color=blue][color=green]
          >>
          >> Interestingly, when I use this struct with[/color][/color]
          LayoutKind.Sequ ential (not[color=blue][color=green]
          >> requiring the union of remaining data members), it[/color][/color]
          works fine.[color=blue][color=green]
          >>
          >> Any ideas?
          >>
          >>
          >> -Mike
          >>
          >> "Grant Richins [MS]" <grantri@online .microsoft.com>[/color][/color]
          wrote in message[color=blue][color=green]
          >> news:%231bEoGXf DHA.616@TK2MSFT NGP11.phx.gbl.. .[color=darkred]
          >> > 34 % sizeof(void*) != 0. The array must begin on an[/color][/color][/color]
          natural boundary[color=blue][color=green]
          >> (like[color=darkred]
          >> > 36). You could always read the data out of the[/color][/color][/color]
          serial port 2 bytes at a[color=blue][color=green][color=darkred]
          >> > type and then allow the runtime to layout the struct[/color][/color][/color]
          more naturally.[color=blue][color=green][color=darkred]
          >> >
          >> > --
          >> > --Grant
          >> > This posting is provided "AS IS" with no warranties,[/color][/color][/color]
          and confers no[color=blue][color=green]
          >> rights.[color=darkred]
          >> >
          >> >
          >> > "Mike" <mike@bogus.net > wrote in message
          >> > news:%23d%23Tdf UfDHA.1648@TK2M SFTNGP09.phx.gb l...
          >> > > The following struct, DataStruct, is only part of a[/color][/color][/color]
          larger one that[color=blue][color=green][color=darkred]
          >> > contains
          >> > > additional fields and arrays. I need the explicit[/color][/color][/color]
          layout because this[color=blue][color=green][color=darkred]
          >> > > struct is really a union, where some of the missing[/color][/color][/color]
          fields and arrays[color=blue][color=green][color=darkred]
          >> > > overlap. What's shown here, though, is sufficient[/color][/color][/color]
          for explaining the[color=blue][color=green][color=darkred]
          >> > error.
          >> > >
          >> > > 290 bytes of data come from a serial device and is[/color][/color][/color]
          to be placed in[color=blue]
          >this[color=green][color=darkred]
          >> > > struct. Hence, I want this struct to be 290 bytes[/color][/color][/color]
          in size, and, if[color=blue]
          >I'm[color=green][color=darkred]
          >> > > adding this right, each of the fields (DataMemb1[/color][/color][/color]
          thru DataMemb17) are[color=blue]
          >2[color=green][color=darkred]
          >> > > bytes each and the 128-element array of short[/color][/color][/color]
          values, DataArray, is[color=blue]
          >256[color=green][color=darkred]
          >> > > bytes. When I try to use the struct, however, I[/color][/color][/color]
          get the following[color=blue][color=green]
          >> error:[color=darkred]
          >> > >
          >> > > "Could not load type DataStruct from assembly[/color][/color][/color]
          <filename>,[color=blue][color=green][color=darkred]
          >> > > Version=1.0.0.2 3184, Culture=neutral ,[/color][/color][/color]
          PublicKeyToken= null because it[color=blue][color=green][color=darkred]
          >> > > contains an object field at offset 34 that is[/color][/color][/color]
          incorrectly aligned or[color=blue][color=green][color=darkred]
          >> > > overlapped by a non-object field."
          >> > >
          >> > > Of course, offset 34 is where the short array,[/color][/color][/color]
          DataArray, begins. If[color=blue]
          >I[color=green][color=darkred]
          >> > > manually change it to FieldOffset( 36 ), this error[/color][/color][/color]
          disappears, but I[color=blue][color=green][color=darkred]
          >> > > encounter other problems because the size of the[/color][/color][/color]
          incoming data buffer,[color=blue][color=green]
          >> 290[color=darkred]
          >> > > bytes, is not equal to the size of the struct,[/color][/color][/color]
          which is now 292 bytes.[color=blue][color=green][color=darkred]
          >> > >
          >> > > Can anyone shed some light on this problem? Thank[/color][/color][/color]
          you![color=blue][color=green][color=darkred]
          >> > >
          >> > >
          >> > > -Mike
          >> > >
          >> > >
          >> > > [ StructLayout( LayoutKind.Expl icit ) ]
          >> > > public struct DataStruct
          >> > > {
          >> > > [ FieldOffset( 0 ) ]
          >> > > public ushort DataMemb1;
          >> > >
          >> > > [ FieldOffset( 2 ) ]
          >> > > public ushort DataMemb2;
          >> > >
          >> > > [ FieldOffset( 4 ) ]
          >> > > public ushort DataMemb3;
          >> > >
          >> > > [ FieldOffset( 6 ) ]
          >> > > public ushort DataMemb4;
          >> > >
          >> > > [ FieldOffset( 8 ) ]
          >> > > public ushort DataMemb5;
          >> > >
          >> > > [ FieldOffset( 10 ) ]
          >> > > public short DataMemb6;
          >> > >
          >> > > [ FieldOffset( 12 ) ]
          >> > > public short DataMemb7;
          >> > >
          >> > > [ FieldOffset( 14 ) ]
          >> > > public ushort DataMemb8;
          >> > >
          >> > > [ FieldOffset( 16 ) ]
          >> > > public short DataMemb9;
          >> > >
          >> > > [ FieldOffset( 18 ) ]
          >> > > public short DataMemb10;
          >> > >
          >> > > [ FieldOffset( 20 ) ]
          >> > > public ushort DataMemb11;
          >> > >
          >> > > [ FieldOffset( 22 ) ]
          >> > > public short DataMemb12;
          >> > >
          >> > > [ FieldOffset( 24 ) ]
          >> > > public short DataMemb13;
          >> > >
          >> > > [ FieldOffset( 26 ) ]
          >> > > public short DataMemb14;
          >> > >
          >> > > [ FieldOffset( 28 ) ]
          >> > > public short DataMemb15;
          >> > >
          >> > > [ FieldOffset( 30 ) ]
          >> > > public ushort DataMemb16;
          >> > >
          >> > > [ FieldOffset( 32 ) ]
          >> > > public ushort DataMemb17;
          >> > >
          >> > > [ MarshalAs( UnmanagedType.B yValArray,[/color][/color][/color]
          SizeConst = 128 ) ][color=blue][color=green][color=darkred]
          >> > > [ FieldOffset( 34 ) ]
          >> > > public short[] DataArray;
          >> > > }
          >> > >
          >> > >
          >> >
          >> >[/color]
          >>
          >>[/color]
          >
          >
          >.
          >[/color]

          Comment

          • Grant Richins [MS]

            #6
            Re: Union Struct Yields Error

            answers inline
            --
            --Grant
            This posting is provided "AS IS" with no warranties, and confers no rights.


            "Mike" <mike@bogus.net > wrote in message
            news:075f01c37e 2b$f6484210$a10 1280a@phx.gbl.. .[color=blue]
            > Thanks for your reply, Grant.
            >
            > Please explain how this works:
            >
            > 1. Why is the padding necessary, and how does one know
            > when and how much it's used? (I have other arrays and
            > data members to add to the union.)[/color]

            The padding is neccessary to improve perfomance. Even though x86 allows
            unaligned memory access (reading a 4-byte integer from a non-4-byte adress
            like 0xXXXXXXX1), it performs much better when everything is lined up
            properly. As far as how much is used, that gets very tricky. IIRC the ECMA
            spec states that this part is completely implementation specific, but the
            general rule is that each field's offset should be evenly divisible (no
            remainder) by the minimum of the size of the field, or the 'natural integer
            size' of the machine. For x86 the 'natural integer size' is 4 bytes. So
            field of type byte can appear at any offset, shorts may only appear at even
            offsets, ints at 4-byte offsets, etc.
            [color=blue]
            >
            > 2. If the padding is automatic with LayoutKind.Sequ ential,
            > then why is the struct's size still 290 bytes?[/color]

            I honestly don't know. My best guess is that the runtime is givng you the
            marshalling size which isn't the same as the way the struct is layed out in
            managed memory.
            [color=blue]
            >
            > 3. Earlier, you mentioned letting the runtime layout the
            > struct naturally. How do I expect it to do this?[/color]

            by using LayoutKind.Sequ ential like you've already done.
            [color=blue]
            >
            > Thanks.
            >
            >
            > -Mike
            >[/color]


            Comment

            • Mike

              #7
              Re: Union Struct Yields Error

              Thanks for the answers.

              Well, I'd be happy to use LayoutKind.Sequ ential, but can
              this be used when creating a union? I don't know of
              another way to layout the union except by using the
              Explicit and FieldOffset attributes.


              -Mike
              [color=blue]
              >-----Original Message-----
              >answers inline
              >--
              >--Grant
              >This posting is provided "AS IS" with no warranties, and[/color]
              confers no rights.[color=blue]
              >
              >
              >"Mike" <mike@bogus.net > wrote in message
              >news:075f01c37 e2b$f6484210$a1 01280a@phx.gbl. ..[color=green]
              >> Thanks for your reply, Grant.
              >>
              >> Please explain how this works:
              >>
              >> 1. Why is the padding necessary, and how does one know
              >> when and how much it's used? (I have other arrays and
              >> data members to add to the union.)[/color]
              >
              >The padding is neccessary to improve perfomance. Even[/color]
              though x86 allows[color=blue]
              >unaligned memory access (reading a 4-byte integer from a[/color]
              non-4-byte adress[color=blue]
              >like 0xXXXXXXX1), it performs much better when everything[/color]
              is lined up[color=blue]
              >properly. As far as how much is used, that gets very[/color]
              tricky. IIRC the ECMA[color=blue]
              >spec states that this part is completely implementation[/color]
              specific, but the[color=blue]
              >general rule is that each field's offset should be evenly[/color]
              divisible (no[color=blue]
              >remainder) by the minimum of the size of the field, or[/color]
              the 'natural integer[color=blue]
              >size' of the machine. For x86 the 'natural integer size'[/color]
              is 4 bytes. So[color=blue]
              >field of type byte can appear at any offset, shorts may[/color]
              only appear at even[color=blue]
              >offsets, ints at 4-byte offsets, etc.
              >[color=green]
              >>
              >> 2. If the padding is automatic with[/color][/color]
              LayoutKind.Sequ ential,[color=blue][color=green]
              >> then why is the struct's size still 290 bytes?[/color]
              >
              >I honestly don't know. My best guess is that the runtime[/color]
              is givng you the[color=blue]
              >marshalling size which isn't the same as the way the[/color]
              struct is layed out in[color=blue]
              >managed memory.
              >[color=green]
              >>
              >> 3. Earlier, you mentioned letting the runtime layout the
              >> struct naturally. How do I expect it to do this?[/color]
              >
              >by using LayoutKind.Sequ ential like you've already done.
              >[color=green]
              >>
              >> Thanks.
              >>
              >>
              >> -Mike
              >>[/color]
              >
              >
              >.
              >[/color]

              Comment

              • Mattias Sjögren

                #8
                Re: Union Struct Yields Error

                Mike,
                [color=blue]
                >Well, I'd be happy to use LayoutKind.Sequ ential, but can
                >this be used when creating a union?[/color]

                No, but do you really need that? You dind't have any overlapping
                fields in the DataStruct struct. Note that a Sequential struct can be
                part of a larger struct with Explicit layout (as long as you follow
                the overlapping rules).



                Mattias

                --
                Mattias Sjögren [MVP] mattias @ mvps.org

                Please reply only to the newsgroup.

                Comment

                • Mike

                  #9
                  Re: Union Struct Yields Error

                  Hi, Mattias,

                  I didn't include the overlapping members in the original post so that I
                  could focus my question on the error, but here is the structure in it's
                  entirety. Can it work as a union? Thanks.


                  -Mike


                  [ StructLayout( LayoutKind.Expl icit ) ]
                  public struct DataStruct
                  {
                  [ FieldOffset( 0 ) ]
                  public ushort DataMemb1;

                  [ FieldOffset( 2 ) ]
                  public ushort DataMemb2;

                  [ FieldOffset( 4 ) ]
                  public ushort DataMemb3;

                  [ FieldOffset( 6 ) ]
                  public ushort DataMemb4;

                  [ FieldOffset( 8 ) ]
                  public ushort DataMemb5;

                  [ FieldOffset( 10 ) ]
                  public short DataMemb6;

                  [ FieldOffset( 12 ) ]
                  public short DataMemb7;

                  [ FieldOffset( 14 ) ]
                  public ushort DataMemb8;

                  [ FieldOffset( 16 ) ]
                  public short DataMemb9;

                  [ FieldOffset( 18 ) ]
                  public short DataMemb10;

                  [ FieldOffset( 20 ) ]
                  public ushort DataMemb11;

                  [ FieldOffset( 22 ) ]
                  public short DataMemb12;

                  [ FieldOffset( 24 ) ]
                  public short DataMemb13;

                  [ FieldOffset( 26 ) ]
                  public short DataMemb14;

                  [ FieldOffset( 28 ) ]
                  public short DataMemb15;

                  [ FieldOffset( 30 ) ]
                  public ushort DataMemb16;

                  [ FieldOffset( 32 ) ]
                  public ushort DataMemb17;

                  [ MarshalAs( UnmanagedType.B yValArray, SizeConst = 128 ) ]
                  [ FieldOffset( 34 ) ]
                  public short[] DataArray;

                  [ MarshalAs( UnmanagedType.B yValArray, SizeConst = 128 ) ]
                  [ FieldOffset( 290 ) ]
                  public short[] DataArray2;

                  [ MarshalAs( UnmanagedType.B yValArray, SizeConst = 128 ) ]
                  [ FieldOffset( 546 ) ]
                  public short[] DataArray3;

                  [ MarshalAs( UnmanagedType.B yValArray, SizeConst = 2 ) ]
                  [ FieldOffset( 802 ) ]
                  public ushort[] DataArray4;

                  [ MarshalAs( UnmanagedType.B yValArray, SizeConst = 2 ) ]
                  [ FieldOffset( 802 ) ]
                  public ushort[] DataArray5;

                  [ MarshalAs( UnmanagedType.B yValArray, SizeConst = 128 ) ]
                  [ FieldOffset( 802 ) ]
                  public short[] DataArray6;

                  [ MarshalAs( UnmanagedType.B yValArray, SizeConst = 128 ) ]
                  [ FieldOffset( 1058 ) ]
                  public short[] DataArray7;

                  [ MarshalAs( UnmanagedType.B yValArray, SizeConst = 128 ) ]
                  [ FieldOffset( 1314 ) ]
                  public short[] DataArray8;

                  [ MarshalAs( UnmanagedType.B yValArray, SizeConst = 2 ) ]
                  [ FieldOffset( 1570 ) ]
                  public ushort[] DataArray9;

                  [ MarshalAs( UnmanagedType.B yValArray, SizeConst = 2 ) ]
                  [ FieldOffset( 1574 ) ]
                  public ushort[] DataArray10;

                  [ MarshalAs( UnmanagedType.B yValArray, SizeConst = 2 ) ]
                  [ FieldOffset( 1578 ) ]
                  public ushort[] DataArray11;

                  [ FieldOffset( 290 ) ]
                  public ushort DataMemb18;

                  [ MarshalAs( UnmanagedType.B yValArray, SizeConst = 4 ) ]
                  [ FieldOffset( 292 ) ]
                  public ushort[] DataArray12;

                  [ MarshalAs( UnmanagedType.B yValArray, SizeConst = 4 ) ]
                  [ FieldOffset( 300 ) ]
                  public ushort[] DataArray13;

                  [ MarshalAs( UnmanagedType.B yValArray, SizeConst = 4 ) ]
                  [ FieldOffset( 308 ) ]
                  public ushort[] DataArray14;

                  [ MarshalAs( UnmanagedType.B yValArray, SizeConst = 4 ) ]
                  [ FieldOffset( 316 ) ]
                  public ushort[] DataArray15;
                  }


                  Comment

                  Working...