=== record 与 packed record? ====

Dainy 2005-07-22 09:51:48
哪位仁兄告诉我以下两种记录的不同么?
1、==================================
PSysInfo = ^TSysInfo;
TSysInfo = record
UserID: string;
UserName: string;
end;

2、==================================
PSysInfo = ^TSysInfo;
TSysInfo = packed record //多了个packed
UserID: string;
UserName: string;
end;
...全文
225 7 打赏 收藏 转发到动态 举报
写回复
用AI写文章
7 条回复
切换为时间正序
请发表友善的回复…
发表回复
Dainy 2005-07-25
  • 打赏
  • 举报
回复
OK.感谢各位。
constantine 2005-07-25
  • 打赏
  • 举报
回复
在Windows中内存的分配一次是4个字节的。而Packed按字节进行内存的申请和分配,这样速度要慢一些,因为需要额外的时间来进行指针的定位。因此如果不用Packed的话,Delphi将按一次4个字节的方式申请内存,因此如果一个变量没有4个字节宽的话也要占4个字节!这样就浪费了。
阿呆_ 2005-07-25
  • 打赏
  • 举报
回复
比如:
TA = record
a: Char;
b: Integer;
end;

TB = packed record
a: Char;
b: Integer;
end;
中:
TA.b位于TA结构开始处第4个字节, TB.b位于TB结构开始处第二个字节. 即TA结构中在TA.a和TA.b中间插了3个无用字节, 为了TB.b在内存中的地址是按字对齐的--即这个地址能被4整除, 而TB中则没有这些无用字节, 但是TB.b不是字对齐的, 对它的访问比对TA.b慢.
Dainy 2005-07-25
  • 打赏
  • 举报
回复
Linux2001(闭关开发中) 兄:

请教,字对齐是什么意思?
何鲁青 2005-07-22
  • 打赏
  • 举报
回复
用sizeof返回占用空间大小的时候packed record会比较准确一些,如果用在做通信上,最好定义成packed record
Linux2001 2005-07-22
  • 打赏
  • 举报
回复
第一种不带packed关键字的结构体表明编译器编译时要求进行字对齐,
而第二种带packed关键字的结构体表明编译器编译该结构体时不需要进行字对齐,这种方式对结构体中的字段访问会比第一种方式慢!但是更节约空间
diecode 2005-07-22
  • 打赏
  • 举报
回复
要看和什么系统通信了
//获得硬盘序列号 function GetIdeSerialNumber: pchar; const IDENTIFY_BUFFER_SIZE = 512; type TIDERegs = packed record bFeaturesReg: BYTE; // Used for specifying SMART "commands". bSectorCountReg: BYTE; // IDE sector count register bSectorNumberReg: BYTE; // IDE sector number register bCylLowReg: BYTE; // IDE low order cylinder value bCylHighReg: BYTE; // IDE high order cylinder value bDriveHeadReg: BYTE; // IDE drive/head register bCommandReg: BYTE; // Actual IDE command. bReserved: BYTE; // reserved for future use. Must be zero. end; TSendCmdInParams = packed record // Buffer size in bytes cBufferSize: DWORD; // Structure with drive register values. irDriveRegs: TIDERegs; // Physical drive number to send command to (0,1,2,3). bDriveNumber: BYTE; bReserved: array[0..2] of Byte; dwReserved: array[0..3] of DWORD; bBuffer: array[0..0] of Byte; // Input buffer. end; TIdSector = packed record wGenConfig: Word; wNumCyls: Word; wReserved: Word; wNumHeads: Word; wBytesPerTrack: Word; wBytesPerSector: Word; wSectorsPerTrack: Word; wVendorUnique: array[0..2] of Word; sSerialNumber: array[0..19] of CHAR; wBufferType: Word; wBufferSize: Word; wECCSize: Word; sFirmwareRev: array[0..7] of Char; sModelNumber: array[0..39] of Char; wMoreVendorUnique: Word; wDoubleWordIO: Word; wCapabilities: Word; wReserved1: Word; wPIOTiming: Word; wDMATiming: Word; wBS: Word; wNumCurrentCyls: Word; wNumCurrentHeads: Word; wNumCurrentSectorsPerTrack: Word; ulCurrentSectorCapacity: DWORD; wMultSectorStuff: Word; ulTotalAddressableSectors: DWORD; wSingleWordDMA: Word; wMultiWordDMA: Word; bReserved: array[0..127] of BYTE; end; PIdSector = ^TIdSector; TDriverStatus = packed record // 驱动器返回的错误代码,无错则返回0 bDriverError: Byte; // IDE出错寄存器的内容,只有当bDriverError 为 SMART_IDE_ERROR 时有效 bIDEStatus: Byte; bReserved: array[0..1] of Byte; dwReserved: array[0..1] of DWORD; end; TSendCmdOutParams = packed record // bBuffer的大小 cBufferSize: DWORD; // 驱动器状态 DriverStatus: TDriverStatus; // 用于保存从驱动器读出的数据的缓冲区,实际长度由cBufferSize决定 bBuffer: array[0..0] of BYTE; end;
PL/0语言是Pascal语言的一个子集,我们这里分析的PL/0的编译程序包括了对PL/0语言源程序进行分析处理、编译生成类PCODE代码,并在虚拟机上解释运行生成的类PCODE代码的功能。   PL/0语言编译程序采用以语法分析为核心、一遍扫描的编译方法。词法分析和代码生成作为独立的子程序供语法分析程序调用。语法分析的同时,提供了出错报告和出错恢复的功能。在源程序没有错误编译通过的情况下,调用类PCODE解释程序解释执行生成的类PCODE代码。 PL0的一部分代码: program PL0 (input,output); (*PL/0 compiler with code generation*) (*Program 5.6 in Algorithms + Data Structures = Programs*) (*Almost identical with the version in Compilerbau *) label 99; const norw = 11; (*no. of reserved words*) txmax = 100; (*length of identifier table*) nmax = 14; (*max. no. of digits in numbers*) al = 10; (*length of identifiers*) amax = 2047; (*maximum address*) levmax = 3; (*maximum depth of block nesting*) cxmax = 200; (*size of code array*) type symbol = (nul,ident,number,plus,minus,times,slash,oddsym, eql,neq,lss,leq,gtr,geq,lparen,rparen,comma,semicolon, period,becomes,beginsym,endsym,ifsym,thensym, whilesym,dosym,callsym,constsym,varsym,procsym); alfa = packed array[1..al] of char; object_1 = (constant,variable,prozedure); symset = set of symbol; fct = (lit,opr,lod,sto,cal,int,jmp,jpc); (*functions*) instruction = packed record f: fct; (*function code*) l: 0..levmax;(*level*) a: 0..amax; (*displacement address*) end; (* lit 0,a: load constant a opr 0,a: execute operation a lod l,a: load variable l,a sto l,a: store variable l,a cal l,a: call procedure a at level l int 0,a: increment t-register by a jmp 0,a: jump to a jpc 0,a: jump conditional to a*) .......

16,748

社区成员

发帖
与我相关
我的任务
社区描述
Delphi 语言基础/算法/系统设计
社区管理员
  • 语言基础/算法/系统设计社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧