Union Struct Yields Error

M

Mike

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.23184, 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.Explicit ) ]
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.ByValArray, SizeConst = 128 ) ]
[ FieldOffset( 34 ) ]
public short[] DataArray;
}
 
G

Grant Richins [MS]

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.
 
M

Mike

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.Sequential (not
requiring the union of remaining data members), it works fine.

Any ideas?


-Mike

Grant Richins said:
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 said:
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.23184, 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.Explicit ) ]
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.ByValArray, SizeConst = 128 ) ]
[ FieldOffset( 34 ) ]
public short[] DataArray;
}
 
G

Grant Richins [MS]

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.Sequential, 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 said:
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.Sequential (not
requiring the union of remaining data members), it works fine.

Any ideas?


-Mike

Grant Richins said:
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 said:
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.23184, 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.Explicit ) ]
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.ByValArray, SizeConst = 128 ) ]
[ FieldOffset( 34 ) ]
public short[] DataArray;
}
 
M

Mike

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.Sequential,
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
-----Original Message-----
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.Sequential, 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 said:
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.Sequential (not
requiring the union of remaining data members), it works fine.

Any ideas?


-Mike

natural boundary
(like and confers no
rights.
to be placed in
this in size, and, if
I'm thru DataMemb17) are
2 values, DataArray, is
256
get the following
error:
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.Explicit ) ]
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.ByValArray, SizeConst = 128 ) ]
[ FieldOffset( 34 ) ]
public short[] DataArray;
}


.
 
G

Grant Richins [MS]

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


Mike said:
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.)

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.
2. If the padding is automatic with LayoutKind.Sequential,
then why is the struct's size still 290 bytes?

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.
3. Earlier, you mentioned letting the runtime layout the
struct naturally. How do I expect it to do this?

by using LayoutKind.Sequential like you've already done.
 
M

Mike

Thanks for the answers.

Well, I'd be happy to use LayoutKind.Sequential, 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
 
M

Mattias Sjögren

Mike,
Well, I'd be happy to use LayoutKind.Sequential, but can
this be used when creating a union?

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
 
M

Mike

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.Explicit ) ]
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.ByValArray, SizeConst = 128 ) ]
[ FieldOffset( 34 ) ]
public short[] DataArray;

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

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

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

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

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

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

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

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

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

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

[ FieldOffset( 290 ) ]
public ushort DataMemb18;

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

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

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

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