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

原创 2001年07月24日 13:33:00

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

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

如何将C/C++程序转译成Delphi(五) 发表日期:2006-2-4 1. Anatomy of a C Header Back to content...
  • autumn20080101
  • autumn20080101
  • 2013年05月05日 14:54
  • 791

Delphi 与 C/C++ 数据类型对照表(转)

 原文地址:http://www.cnblogs.com/del/archive/2007/12/03/981563.html Delphi 数据类型 C/C++ ...
  • edisator
  • edisator
  • 2015年01月17日 11:15
  • 946

Delphi与C语言类型转换对照

 When converting C function prototypes to Pascal equivalent declarations, it's important to subst...
  • MaxWoods
  • MaxWoods
  • 2014年10月10日 14:31
  • 2075

UTC转换本地时间

//===================================================================== //TITLE: //    UTC转换本地时间 ...
  • nui111
  • nui111
  • 2015年05月11日 16:37
  • 709

Delphi 与 C/C++ 数据类型对照表

Delphi 数据类型 C/C++ ShorInt 8位有符号整数 char Byte 8位无符号整数 BYTE,unsigned sh...
  • rocklee
  • rocklee
  • 2015年09月24日 11:53
  • 3193

Delphi与C/C++ 数据类型的对应

原文地址:Delphi与C/C++ 数据类型对照关系 Delphi 数据类型 C/C++ ShorInt 8位有符号整数 char Byte 8位无符号整数...
  • shuiyuanfengxing
  • shuiyuanfengxing
  • 2013年01月05日 16:13
  • 3029

C/C++调用Delphi制作的dll时发现的一些问题

做了一个网络业务逻辑实体,是用C++编写的dll,内部有一个全局的业务实体,外部准备用Delphi编写的界面程序来控制并显示状态...... 1. C++的dll中不能用C++风格的导出方式:_dec...
  • gyymen
  • gyymen
  • 2016年12月09日 08:31
  • 1063

【整理】C/C++中字符串与整数之间的相互转换

C/C++中字符串与整数之间的相互转换 一、用C标准库中的sprintf()和sscanf()转换  二、用C++中的stringstream类转换 三、atoi、atof、atol、itoa等 总...
  • sherry_zs
  • sherry_zs
  • 2016年01月13日 20:23
  • 2568

Matlab转c与c++代码

作为一个强大的科学计算软件,matlab广泛运用于较多领域,以其简单的编程风格,便利的调试环境等等众多优点,在编写算法与测试的时候通常用到。然而很多的实际运用上matlab毕竟还不是很普及,相反还是c...
  • on2way
  • on2way
  • 2015年10月17日 15:36
  • 19065

将Python代码嵌入到C++程序

这篇文章主要介绍了将Python代码嵌入C++程序进行编写的实例,尽管通常还是Python代码中调用C++程序的情况较多...需要的朋友可以参考下 把python嵌入的C++里面需要做一些步...
  • u011854789
  • u011854789
  • 2016年06月19日 00:17
  • 1774
内容举报
返回顶部
收藏助手
不良信息举报
您举报文章:如何将C/C++程序转译成Delphi(十)
举报原因:
原因补充:

(最多只允许输入30个字)