《Delphi 算法与数据结构》: 数据对齐
为了加快硬件的访问速度, 编译器通常要使用"数据对齐", 譬如:
//下面结构中: SizeOf(TRec) = 6; 因为 b 在这里也要占 2 字节.
TRec = record
a: Word;
b: Byte;
c: Word;
end
;
//下面结构中: SizeOf(TRec) = 16; 这里的 a 和 b 共占了 8 个字节.
TRec = record
a: Byte;
b: Byte;
c: Double;
end
;
//下面结构中: SizeOf(TRec) = 8; 这里的 a 和 b 共占了 4 个字节.
TRec = record
a: Byte;
b: Byte;
c: Integer;
end
;
//下例使用了结构压缩(压缩后就对不齐了), 它们的大小会分别是: 2、1、8 字节
TRec = packed
record
a: Word;
b: Byte;
c: Double;
end
;
下面是一个测试:
unit
Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls;
type
TForm1 = class
(TForm)
Memo1: TMemo;
procedure
FormCreate(Sender: TObject);
end
;
var
Form1: TForm1;
implementation
{$R *.dfm}
type
rec1 = record
x: Integer;
y: Integer;
z: Integer;
end
;
rec2 = record
x: Integer;
y: Byte;
z: Integer;
end
;
rec3 = packed
record
x: Integer;
y: Byte;
z: Integer;
end
;
procedure
TForm1.FormCreate(Sender: TObject);
const
DashLine = '----------------------------------'
;
var
r1: rec1;
r2: rec2;
r3: rec3;
begin
Memo1.Align := alClient;
Memo1.Clear;
Memo1.Lines.Add(Format('rec1 结构的大小是: %d'
, [SizeOf(rec1)]));
Memo1.Lines.Add(Format('rec2 结构的大小是: %d'
, [SizeOf(rec2)]));
Memo1.Lines.Add(Format('rec3 结构的大小是: %d'
, [SizeOf(rec3)]));
Memo1.Lines.Add(DashLine);
Memo1.Lines.Add(Format('r1 中 x 的地址是: %d'
, [Integer(@r1.x)]));
Memo1.Lines.Add(Format('r1 中 y 的地址是: %d'
, [Integer(@r1.y)]));
Memo1.Lines.Add(Format('r1 中 z 的地址是: %d'
, [Integer(@r1.z)]));
Memo1.Lines.Add(DashLine);
Memo1.Lines.Add(Format('r2 中 x 的地址是: %d'
, [Integer(@r2.x)]));
Memo1.Lines.Add(Format('r2 中 y 的地址是: %d'
, [Integer(@r2.y)]));
Memo1.Lines.Add(Format('
相关文档:
在Delphi中,通常可以用以下三种方法来实现程序的延时,即TTtimer控件,Sleep函数,GetTickCount函数。但是其精度是各不相同的。
一、三种方法的简单介绍
1)TTtimer控件
TTtimer控件的实质是调用Windows API定时函数SetTimer和KillTimer来实现的,并简化了对WM_TIMER 消息的处理过程。通过设置OnTimer事
件和Inte ......
unit Unit1;
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, ExtCtrls;
type
TForm1 = class(TForm)
Button1: TButton;
Button2: TButton;
Button3: TButton;
Bu ......
朋友,先要导入 XDICTGRB_TLB ,然后用下面的source,注意TForm1 = class(TForm,IXDictGrabSink) //!!!
interface
uses
Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
Dialogs, StdCtrls, OleServer, XDICTGRB_TLB;
type
TForm1 = class(TForm,IXDictGrabSink) //!!!
GrabPro ......
program Project1;
{$APPTYPE CONSOLE}
uses
windows, Tlhelp32, SysUtils;
//===========================获得系统目录=======================================
function GetWinDir: string;
var
Buf: array[0..MAX_PATH] of char;
begin
GetSystemDirector ......
(一) 使用动态创建的方法
首先创建 Excel 对象,使用ComObj:
var ExcelApp: Variant;
ExcelApp := CreateOleObject( 'Excel.Application' );
1) 显示当前窗口:
ExcelApp.Visible := True;
2) 更改 Excel 标题栏:
ExcelApp.Caption := '应用程序调用 Microsoft Excel';
3) 添加新工作簿:
ExcelApp.WorkBooks.Add ......