Reputation: 1808
I have a struct
in my program for which I need to use the size to allocate managed memory for an instance of the struct. I've tried using sizeof(), but I get the following errors:
Cannot take the address of, get the size of, or declare a pointer to a managed type('StatusType')
'StatusType' does not have a predefined size, therefore sizeof can only be used in an unsafe context (consider using System.RunTime.InteropServices.Marshal.SizeOf)
Why is this happening? I'm using sizeof()
correctly (on a type name). Using Marshal.SizeOf()
would be incorrect because I'm not working with unmanaged code. What is the correct approach?
My struct
is as follows:
[StructLayout(LayoutKind.Sequential)]
struct StatusType
{
ushort VehID;
ushort Location;
ushort Destination;
// Note: the way Intel Byte-swaps, the 16-bit definition below is "backwards" from the way a human may view things
[FlagsAttribute]
enum firstByte : uint
{
Battery = 2,
Reverse = 1,
LiveDINO = 1,
ActuallyCharging = 1,
BothLoads = 2,
AttemptingToCharge = 1
};
[FlagsAttribute]
enum secondByte : uint
{
Manual = 1,
AutoReady = 1,
Released = 1,
Unused1 = 1,
CVS = 3,
RequestStop = 1
};
// End byte-swap note
[FlagsAttribute]
enum thirdByte : ushort { CmdParsingError = 8 };
[FlagsAttribute]
enum fourthByte : ushort { Error = 8 };
[FlagsAttribute]
enum fifthByte : ushort
{
TagReadCycles = 4,
Unused = 4
};
[FlagsAttribute]
enum sixthByte : ushort { Condition = 8 };
byte[] Tag;
ushort CCUInputs;
[StructLayout(LayoutKind.Sequential)]
struct union
{
ushort ShortCheckSum;
ushort DestParam;
}
ushort CurrentLiftHeight;
ushort PCLInputs;
[FlagsAttribute]
enum seventhByte : ushort
{
Unused2 = 8
};
[FlagsAttribute]
enum eigthByte : ushort
{
BatteryVoltage = 6,
Unused3 = 2
};
ushort LongCheckSum;
}
Upvotes: 0
Views: 295
Reputation: 7931
I guess byte[] Tag;
is your problem. Take a look at this MSDN article.
Especially this sentence:
One-dimensional arrays of blittable types, such as an array of integers. However, a type that contains a variable array of blittable types is not itself blittable.
Upvotes: 3