如何将C/C++程序转译成Delphi(十)

3.6.3. Packed Records (Alignment)

If a record is packed (aligned) then the starting byte of a field (and consequently also of the record) is aligned with the first byte of a word (16-bit or 2-byte align) or of a DWord (32-bit or 4-byte alignment). This significantly speeds CPU access to the fields, but consumes more memory.

The older APIs (from the 16-bit world) usually use packed structures (one or two byte alignment). It is the default in 16-bit Delphi: there is no difference under Delphi 1 if you write record or packed record.

Four-byte alignment is the default under Delphi32, although some APIs do use packed records under Win32.

It's important to know which type of alignment is used in an API, because the size of a record depends on the alignment.

The following example explains the difference between the coding of 2-byte and 4-byte alignment:

TmyRecNotPacked = Record



  FieldA : Word;



  FieldB: LongInt;



  FieldC: Byte;



End;







TmyRecPacked = packed Record



  FieldA : Word;



  FieldB: LongInt;



  FieldC: Byte;



End; 
Offset

0

1

2

3

4

5

6

7

8

9

10

11

TMyRecUnPacked

A

A

.

.

B

B

B

B

C

.

.

.

TMyRecPacked

A

A

B

B

B

B

C

     

Now, how to determine whether packing is required or not. In the Microsoft APIs, two methods are used. One is clear and simple:

#pragma pack(n)

Use the packed keyword if n is 1 or 2, but don't use packed if n is 4.

However, Microsoft uses #include <filename> to control packing by putting the packing pragma in the include file. The following include files are used to control the packing method:

#includeMeans Packed required
pshpack1.h#pragma pack (1) Yes
pshpack2.h#pragma pack (2) Yes
pshpack3.h#pragma pack (3) Yes
pshpack4.h#pragma pack (4) No
Poppack.hBack to previous packing ?

If a header file contains the following line

#include <pshpack1.h>

you can interpret it as #pragma pack (1) and use the packed keywords in records from now until you come to another include of pshpack?.h or to an include of poppack.h.

Four-byte alignment (non-packed records) is the default under Delphi32. But what if the user changes the compiler options and switches off 4-byte alignment? If you need to ensure that 4-byte alignment is enabled you must insert the following line at the top of the unit:

{$ALIGN ON} 

A unit header must look at least like this:

Unit MyUnit;



  {$ALIGN ON}

You can use the{$ALIGN} directive instead of explicitly using the packed keyword, too, but I prefer explicit packing because it overrides {$ALIGN ON} and makes things quite specific.

NOTE: A field whose length is an odd number of bytes presents a problem if you encounter #pragma pack(2) and use the packed keyword for translating it. The field will be close packed on a byte boundary when it should be packed on a word boundary. A solution is being sought for handling this situation.


Back to contents

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值