Struct sizeof result not expected

I have a structure defined this way:

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

Now, according to my calculations, this structure has a length of 10 bytes. However, sizeof reports the file is 16 bytes long? Does anyone know why?

I am compiling using build tools in the Windows DDK.

+3
source share
4 answers

Alignment.

use

#pragma pack(1)

...struct goes here...

#pragma pack()

I would also recommend reordering things and, if necessary, populating them with RESERVED bytes so that multi-byte integral types are better aligned. This will speed up the processing of the processor, and your code will decrease.

+10
source

Reorder the items. Start with ULONG and then BYTE. This will improve the alignment of the structure in memory.

+3
source

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

+3
source

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

Presumably, you are working on a 32-bit system, so you will have 3 fill bytes between configurationIndicator and baudRate and 3 more fill bytes at the end of the structure.

+2
source

All Articles