国产探花免费观看_亚洲丰满少妇自慰呻吟_97日韩有码在线_资源在线日韩欧美_一区二区精品毛片,辰东完美世界有声小说,欢乐颂第一季,yy玄幻小说排行榜完本

首頁 > 編程 > C++ > 正文

如何將C/C++程序轉譯成Delphi(十)

2019-11-18 18:50:10
字體:
來源:轉載
供稿:網友

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


上一篇:如何將C/C++程序轉譯成Delphi(十一)

下一篇:如何將C/C++程序轉譯成Delphi(九)

發表評論 共有條評論
用戶名: 密碼:
驗證碼: 匿名發表
學習交流
熱門圖片

新聞熱點

疑難解答

圖片精選

網友關注

主站蜘蛛池模板: 鸡西市| 高尔夫| 红桥区| 额济纳旗| 三江| 平乡县| 樟树市| 额敏县| 毕节市| 梁山县| 临猗县| 登封市| 宜宾市| 隆子县| 喀喇| 平山县| 厦门市| 贵港市| 利川市| 来凤县| 正宁县| 饶平县| 德庆县| 莲花县| 大宁县| 商水县| 泽州县| 蓬莱市| 扶沟县| 横山县| 房产| 屏东市| 逊克县| 镶黄旗| 龙陵县| 琼海市| 江达县| 通辽市| 洮南市| 浦县| 通山县|