随笔 - 2146  文章 - 97 评论 - 11767 trackbacks - 253

问题来源: http://www.cnblogs.com/del/archive/2008/05/02/1179416.html#1179645

我觉得 GetMem 和 GetMemory 的功能是一样的, 有点类似与 Format 和 FmtStr 的关系;
它们的区别只在于参数位置和返回值的区别, 看看 GetMemory 函数的源码就知道, 它其实就是调用的 GetMem.
unit Unit1;

interface

uses
  Windows, Messages, SysUtils, Variants, Classes, Graphics, Controls, Forms,
  Dialogs, StdCtrls;

type
  TForm1 = class(TForm)
    Button1: TButton;
    Button2: TButton;
    procedure Button1Click(Sender: TObject);
    procedure Button2Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

procedure TForm1.Button1Click(Sender: TObject);
var
  p: PInteger;
begin
  GetMem(p, SizeOf(Integer));
  p^ := 100;
  ShowMessage(IntToStr(p^)); {100}
  FreeMem(p);
end;

procedure TForm1.Button2Click(Sender: TObject);
var
  p: PInteger;
begin
  p := GetMemory(SizeOf(Integer));
  p^ := 100;
  ShowMessage(IntToStr(p^)); {100}
  FreeMemory(p);
end;

end.

posted on 2008-05-02 19:57  万一  阅读(...)  评论(...编辑  收藏