An enumerated type is stored as an unsigned byte if the enumeration has no more than 256 values, and if the type was declared in the {$Z1} state (the default). If an enumerated type has more than 256 values, or if the type was declared in the {$Z2} state, it is stored as an unsigned word. Finally, if an enumerated type is declared in the {$Z4} state, it is stored as an unsigned double word.
The {$Z2} and {$Z4} states are useful for interfacing with C and C++ libraries, which usually represent enumerated types as words or double words.
Example code : Various enum type sizes |
type {$Z1} TCars1 = (Rover, Jaguar, Honda); // Will fit into 1 byte TFruit1 = (Banana=255, Apple, Pear); // Will exceed one byte {$Z4} TCars2 = (Ford, Nissan, Vauxhall); // Now uses 4 bytes TFruit2 = (Orange=255, Plum, Grape); // Now uses 4 bytes begin ShowMessage('TCars1 size = '+IntToStr(SizeOf(TCars1))); ShowMessage('TFruit1 size = '+IntToStr(SizeOf(TFruit1))); ShowMessage('TCars2 size = '+IntToStr(SizeOf(TCars2))); ShowMessage('TFruit2 size = '+IntToStr(SizeOf(TFruit2))); end; |
TCars1 size = 1 TFruit1 size = 2 TCars2 size = 4 TFruit2 size = 4 |