473,387 Members | 3,810 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

Odd size of struct



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
Nov 16 '05 #1
1 1557
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" <Mo************@hotmail.com> schrieb im Newsbeitrag
news:opsdrk7vhnklbvpo@stone...


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

Nov 16 '05 #2

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: MSReddy | last post by:
Hi Group, I have a structure than has other structures in it. I added 3 members to a strucuture and my program started behaving very strangly. When I printed sizes of the strucutures the values...
7
by: ANaiveProgrammer | last post by:
Hi all I have made the following two structs and size is not according to what is supposed to be, so please ponder over following and identify if im wrong... please also mention what would be...
19
by: junky_fellow | last post by:
Can the size of pointer variables of different type may be different on a particular architecture. For eg. Can the sizeof (char *) be different from sizeof(int *) or sizeof (void *) ? What...
3
by: Dirk Reske | last post by:
Hello, I have the following struct: public struct WAVEFORMATEX { public UInt16 wFormatTag; //2 bytes public UInt16 nChannels; //2 bytes public UInt32 nSamplesPerSec; //4 bytes...
1
by: Jón Sveinsson | last post by:
Hello everyone I have been trying to read and write struct to binary files, I'm using to functions to convert the struct to bytes and bytes to struct, I always receive the following error ...
8
by: redefined.horizons | last post by:
I would like to have an array declaration where the size of the array is dependent on a variable. Something like this: /* Store the desired size of the array in a variable named "array_size". */...
11
by: chinu | last post by:
mail hi all.. is it possible to find the size of a structure upto bit size.., where the structure contains only one element and that too with bit specifier..eg.. struct x { char a : 4; } y;
111
by: Tonio Cartonio | last post by:
I have to read characters from stdin and save them in a string. The problem is that I don't know how much characters will be read. Francesco -- ------------------------------------- ...
6
by: marktxx | last post by:
Although the C90 standard only mentions the use of 'signed int' and 'unsigned int' for bit-fields (use 'int' at your own risk) and C99 adds _Bool. It seems that most compilers create the size of...
15
by: kris | last post by:
Hi I am writing a small program where I need to obtain the actual size of a structure. The programm is as follows struct abc { int j; char k; int i; }*a;
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.