29.4 Use of sizeof()
In some cases, sizeof()
is computed at runtime for EBC code, whereas
sizeof()
is never computed at runtime for native code. Because pointers and
the UEFI data types INTN
and UINTN
are different sizes on different CPU
architectures, an EBC image must adapt to the platform on which it is
executing. The example below shows several examples of simple and complex data
types. For the types that return different sizes for 32-bit versus 64-bit
processors, the EBC compiler generates code that computes the correct values at
runtime when executing on 32-bit and 64-bit processors.
Example 250-Size of data types with EBC
#include <Uefi.h>
typedef enum {
Red,
Green,
Blue
} COLOR_TYPE;
#pragma pack(1)
typedef struct {
UINT64 ValueU64;
UINT32 ValueU32;
UINT16 ValueU16;
UINT8 ValueU8;
} FIXED_STRUCTURE;
typedef struct {
UINTN ValueUN;
VOID *Pointer;
UINT64 ValueU64;
UINT32 ValueU32;
} VARIABLE_STRUCTURE;
#pragma pack()
UINT64 Size;
Size = sizeof (UINT64); // 8 bytes on all CPUs
Print (L"Size = %d\n", Size);
Size = sizeof (UINT32); // 4 bytes on all CPUs
Print (L"Size = %d\n", Size);
Size = sizeof (UINT16); // 2 bytes on all CPUs
Print (L"Size = %d\n", Size);
Size = sizeof (UINT8); // 1 byte on all CPUs
Print (L"Size = %d\n", Size);
Size = sizeof (UINTN); // 4 bytes on 32-bit CPU, 8 bytes on 64-bit CPU
Print (L"Size = %d\n", Size);
Size = sizeof (INTN); // 4 bytes on 32-bit CPU, 8 bytes on 64-bit CPU
Print (L"Size = %d\n", Size);
Size = sizeof (COLOR_TYPE); // 4 bytes on 32-bit CPU, 8 bytes on 64-bit CPU
Print (L"Size = %d\n", Size);
Size = sizeof (VOID *); // 4 bytes on 32-bit CPU, 8 bytes on 64-bit CPU
Print (L"Size = %d\n", Size);
//
// 15 bytes on 32-bit CPU, 15 bytes on 64-bit CPU
//
Size = sizeof (FIXED_STRUCTURE);
Print (L"Size = %d\n", Size);
//
// 20 bytes on 32-bit CPU, 28 bytes on 64-bit CPU
//
Size = sizeof (VARIABLE_STRUCTURE);
Print (L"Size = %d\n", Size);