4

I have a a struct defined thusly:

typedef struct _CONFIGURATION_DATA {
    BYTE configurationIndicator;
    ULONG32 baudRate;
    BYTE stopBits;
    BYTE parity;
    BYTE wordLength;
    BYTE flowControl;
    BYTE padding;
} CONFIGURATION_DATA;

Now, by my reckoning, that struct is 10 bytes long. However, sizeof reports that it is 16 bytes long? Anyone know why?

I am compiling using the build tools in the Windows DDK.

2
  • forced alignment of some sort? Commented Dec 16, 2009 at 10:50
  • Remember: The size of a structure may not be equal to the sum of the sizes of its members; implementations are allowed to insert padding between members and after the last members. <b>ALWAYS</b> access members by name, not by offset as the offset may change between compiler versions and vendors. Commented Dec 16, 2009 at 20:12

4 Answers 4

12

Alignment.

use

#pragma pack(1)

...struct goes here...

#pragma pack()

I would also recommend reordering things, and if necessary padding then with RESERVED bytes, so that multi-byte integral types will be better aligned. This will make processing faster for tbe CPU, and your code smaller.

Sign up to request clarification or add additional context in comments.

7 Comments

Only do this if you've got a good reason to -- many architectures disallow misaligned accesses (so the compiler will have to use byte loads and saves for safety) and most introduce a performance penalty.
True. But I'd expect the compiler to generate proper instructions to access misaligned integral types when architecture does not support it. This is a common pattern to access binary data in files, communications and other IO. On the contrary, i'd say avoid writing a structure if you do not specify the alignment explicitly, because it will change with compiler version/settings.
Actually, the reason i need it packed like this is because it gets written to a socket, and the communications spec dicatates a 10-byte message.
@Pavel -- you might 'expect' the compiler to do something but there is no guarantee that it will! For example the MS VC++ cross-compiler will not automatically generate instructions for misaligned types, you have to explicitly tell it to do so using pointers declared with __unaligned.
@Pavel: I certainly wouldn't "expect" that. You're just spoiled because you're used to x86 hardware. Writing programs that crash at the slightest provocation is a "common pattern" too.
|
4

Change the order of the elements. Start with the ULONG, followed by the BYTEs. This will improve the struct's alignment in memory.

Comments

3

This is due to padding, because on your platform, an ULONG32 apparently must be aligned on 4-byte boundaries. Since the start and end of the struct apparently also must be aligned, the first and last BYTE will be padded with 3 bytes each.

Comments

2

The extra size you are measuring is the padding introduced by the compiler.

Presumably, you are working on a 32 bits system, so you will have 3 bytes of padding between configurationIndicator and baudRate, and 3 more bytes of padding at the end of the struct.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.