博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
DELPHI设置枚举类型size
阅读量:5207 次
发布时间:2019-06-14

本文共 1621 字,大约阅读时间需要 5 分钟。

 

delphi枚举类型长度默认为2个字节(单字),而在C中枚举为4个字节(双字),如果需要跨这两个平台编程,传输结构时会由于数据长度不一造成灾难。

经过查找资料,原来delphi可以通过{$Z+} {$Z-} {$Z1} {$Z4} 等宏设置枚举类型的长度,小至1个字节,大至4个字节。

官方说明如下:

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

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.

Note: Note: For backwards compatibility with early versions of Delphi and CodeGear Pascal, the directives {

$Z-} and {
$Z+} are also supported. They correspond to {
$Z1} and {
$Z4}, respectively.

例子如下:

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

转载于:https://www.cnblogs.com/h2zZhou/p/5000153.html

你可能感兴趣的文章
v-bind、v-on 的缩写
查看>>
在Web应用中接入微信支付的流程之极简清晰版
查看>>
BZOJ 1801: [Ahoi2009]chess 中国象棋( dp )
查看>>
Greg and Array
查看>>
[ext4] 磁盘布局 - extent tree
查看>>
mysql 自动备份脚本
查看>>
linux 设置时区
查看>>
异步路由、单播泛洪产生的安全侦听风险
查看>>
《STL源码剖析》STL的双层配置器
查看>>
VC分发包版本问题
查看>>
这是通过 Open Live Writer(是个博客编辑器) 发布的
查看>>
Codeforces Round #401 (div.2)
查看>>
在jsp中默认写上的一段java代码表示basePath 的路径的具体的意思是什么?
查看>>
AndroidDevTools简介
查看>>
《TCP/IP详解卷1:协议》读书笔记
查看>>
软硬链接的学习
查看>>
hibernate12--注解
查看>>
使用Gridview绑定数据库中的图片
查看>>
C# 视频监控系列(4):客户端——音频接收和截图
查看>>
asp.net mvc 无刷新高效分页
查看>>