Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

struct sizeof result not expected

Tags:

c++

struct

sizeof

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.

like image 878
Alistair Evans Avatar asked Nov 30 '22 19:11

Alistair Evans


1 Answers

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.

like image 106
Pavel Radzivilovsky Avatar answered Dec 11 '22 04:12

Pavel Radzivilovsky