4.3.8 Piecemeal Structure Allocations

Structures should always be allocated using the sizeof() operator on the name of the structure. Never use the sum of the sizes of the structure's members because it does not take into account the padding that the compiler introduces to guarantee alignment. The following example shows two examples for allocating memory for a structure. The first allocation is incorrect, the second allocation is correct.

Example 14-Incorrect and correct piecemeal structure allocation

typedef struct {
UINT8 Value8;
UINT64 Value64;
// Wrong. This only allocates 9 bytes, but MyStructure is 16 bytes
Example = AllocatePool (sizeof (UINT8) + sizeof (UINT64));
// Correct. This allocates 16 bytes for MyStructure.
Example = AllocatePool (sizeof (EXAMPLE_STRUCTURE));