Variant Records: The equivalent to the C-union structure

博客可能围绕Delphi和Pascal展开,涉及structure、string、integer等类型相关内容,属于信息技术领域中编程语言相关知识。
部署运行你感兴趣的模型镜像
Variant Records: The equivalent to the C-union structure

Is there a way to create a C 'union'-like structure in Delphi? That is, a structure that uses the same memory area?


The Delphi (Pascal/ObjectPascal) equivalent to a C-union structure is called a Variant Record (not to be confused with the Variant "type" available in Delphi 2.0+). As with a C-union, the Pascal variant record allows several structure types to be combined into one, and all will occupy the same memory space. Look up the syntax declaration under "Records" in the help file. But here's an example:

type 

TPerson = record
 FirstName,
 LastName: string[40];
 BirthDate: TDate;
 case Citizen: Boolean of
  True: (BirthPlace: string[40]);
  False: (Country: string[20];
   EntryPort: string[20];
   EntryDate: TDate;
   ExitDate: TDate);
end;

The record above is actually a single expression of two records that could describe a person:

type
TPersonCitizen = record
 FirstName,
 LastName: string[40];
 BirthDate: TDate;
 BirthPlace: string[40]
end;

and

type
TPersonAlien = record
 FirstName,
 LastName: string[40];
 BirthDate: TDate;
 Country: string[20];
 EntryPort: string[20];
 EntryDate: TDate;
 ExitDate: TDate;
end;

And as in a union, the combination of the two types of records makes for much more efficient programming, because a person could be expressed in a variety of ways.

Everything I explained above is pretty hypothetical stuff. In Delphi, the TRect structure that describes a rectangle is actually a variant record:

type
TPoint = record
 X: Longint;
 Y: Longint;
end;

TRect = record
 case Integer of
  0: (Left, Top, Right, Bottom: Integer);
  1: (TopLeft, BottomRight: TPoint);
end;

where the coordinates of the rectangle can be expressed using either four integer values or two TPoints.

I realize this is pretty quick and dirty, so I suggest you refer to the help file for a more in-depth explanation, or go to your nearest book store or library and look at any Pascal book (not Delphi -- most won't explain this fairly esoteric structure). However, if you're familiar with the C-union, this stuff should be an absolute breeze.

您可能感兴趣的与本文相关的镜像

Llama Factory

Llama Factory

模型微调
LLama-Factory

LLaMA Factory 是一个简单易用且高效的大型语言模型(Large Language Model)训练与微调平台。通过 LLaMA Factory,可以在无需编写任何代码的前提下,在本地完成上百种预训练模型的微调

> Configure project :unityLibrary Variant 'debug', will keep symbols in binaries for: 'libunity.so' 'libil2cpp.so' 'libmain.so' Variant 'release', symbols will be stripped from binaries. > Configure project :launcher Variant 'debug', will keep symbols in binaries for: 'libunity.so' 'libil2cpp.so' 'libmain.so' Variant 'release', symbols will be stripped from binaries. > Task :unityLibrary:preBuild UP-TO-DATE > Task :unityLibrary:preReleaseBuild UP-TO-DATE > Task :unityLibrary:writeReleaseAarMetadata UP-TO-DATE > Task :unityLibrary:generateReleaseResValues UP-TO-DATE > Task :unityLibrary:generateReleaseResources UP-TO-DATE > Task :launcher:preBuild UP-TO-DATE > Task :launcher:preReleaseBuild UP-TO-DATE > Task :unityLibrary:packageReleaseResources UP-TO-DATE > Task :unityLibrary:extractDeepLinksRelease UP-TO-DATE > Task :launcher:javaPreCompileRelease UP-TO-DATE > Task :unityLibrary:processReleaseManifest UP-TO-DATE > Task :unityLibrary:compileReleaseLibraryResources UP-TO-DATE > Task :unityLibrary:parseReleaseLocalResources UP-TO-DATE > Task :unityLibrary:generateReleaseRFile UP-TO-DATE > Task :launcher:checkReleaseAarMetadata UP-TO-DATE > Task :launcher:generateReleaseResValues UP-TO-DATE > Task :unityLibrary:javaPreCompileRelease UP-TO-DATE > Task :launcher:mapReleaseSourceSetPaths UP-TO-DATE > Task :launcher:generateReleaseResources UP-TO-DATE > Task :launcher:mergeReleaseResources UP-TO-DATE > Task :launcher:packageReleaseResources UP-TO-DATE > Task :launcher:parseReleaseLocalResources UP-TO-DATE > Task :launcher:createReleaseCompatibleScreenManifests UP-TO-DATE > Task :launcher:extractDeepLinksRelease UP-TO-DATE > Task :unityLibrary:compileReleaseJavaWithJavac UP-TO-DATE > Task :unityLibrary:bundleLibCompileToJarRelease UP-TO-DATE > Task :unityLibrary:bundleLibRuntimeToJarRelease UP-TO-DATE > Task :launcher:processReleaseMainManifest UP-TO-DATE > Task :unityLibrary:processReleaseJavaRes UP-TO-DATE > Task :unityLibrary:createFullJarRelease UP-TO-DAT
03-18
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值