CSDN博客

img mysine

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

发表于2001/7/24 13:33:00  658人阅读

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:

#include Means 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.h Back 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

相关博文

我的热门文章

img
取 消
img