Odd size of struct

  • Thread starter Morten Wennevik
  • Start date
M

Morten Wennevik

Hi,

Using a struct like the following, Marshal.SizeOf always reports 14 bytes yet the struct should be 13 bytes.
What am I doing wrong?

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct GIF_HEADER
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst=3)]
public byte[] Signature; // Header Signature (always "GIF")
[MarshalAs(UnmanagedType.ByValArray, SizeConst=3)]
public byte[] Version; // GIF format version ("87a" or "89a")
public UInt16 ScreenWidth; // width in pixels
public UInt16 ScreenHeight; // height in pixels
public byte Packed; // screen and color map information
public byte BackgroundColor; // background color index
public byte AspectRatio; // pixel aspect ratio
}

Morten
 
C

cody

Try to use SizeConst=13 or Pack=1 in the StructLayout Attribute to ensure no
internal struct padding is done.

--
cody

[Freeware, Games and Humor]
www.deutronium.de.vu || www.deutronium.tk
Morten Wennevik said:
Hi,

Using a struct like the following, Marshal.SizeOf always reports 14 bytes
yet the struct should be 13 bytes.
What am I doing wrong?

[StructLayout(LayoutKind.Sequential, CharSet = CharSet.Ansi)]
public struct GIF_HEADER
{
[MarshalAs(UnmanagedType.ByValArray, SizeConst=3)]
public byte[] Signature; // Header Signature (always "GIF")
[MarshalAs(UnmanagedType.ByValArray, SizeConst=3)]
public byte[] Version; // GIF format version ("87a" or "89a")
public UInt16 ScreenWidth; // width in pixels
public UInt16 ScreenHeight; // height in pixels
public byte Packed; // screen and color map information
public byte BackgroundColor; // background color index
public byte AspectRatio; // pixel aspect ratio
}

Morten
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top