Delphi Set Enumeration Type size

Source: Internet
Author: User

Delphi enumeration type length defaults to 2 bytes (word), and in C is enumerated as 4 bytes (double word), if you need to program across these two platforms, the transport structure will be caused by the data length of the disaster.

After looking for information, the original Delphi can be {$Z +} {$Z-} {$Z 1} {$Z 4} and other macros set the length of the enumeration type, as small as 1 bytes, as large as 4 bytes.

The official notes are as follows:

http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/devcommon/ Compdirsminimumenumerationsize_xml.html

The $Z directive controls the minimum storage size of Delphi enumerated types.

An enumerated type are stored as an unsigned byte if the enumeration have no more than values, and if the type was DECLA Red in the {$Z 1} state (the default). If an enumerated type have more than values, or if the type was declared in the {$Z 2} state, it is stored as a n unsigned word. Finally, if an enumerated type was declared in the {$Z 4} state, it's stored as an unsigned double word.

The {$Z 2} and {$Z 4} states is useful for interfacing with C and C + + libraries, which usually represent enumerated types as words or double words.

Note: Note: For backwards compatibility with early versions of Delphi and CodeGear Pascal, the directives {$Z-} and {$Z +< /c5>} is also supported. They correspond to {$Z 1} and {$Z 4}, respectively.

Examples are as follows:

Http://www.delphibasics.co.uk/RTL.asp?Name= $Z

Example code:various Enum type sizes

Type
{$Z1}
TCars1 = (Rover, Jaguar, Honda); //would fit into 1 byte
TFruit1 = (banana=255, Apple, Pear); //would 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

Delphi Set Enumeration Type size

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.