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

一个全局变量可以初始化, 如果你不主动初始化, 也会被自动初始化;
一个局部变量不能被初始化, 虽然你不能主动初始化, 但它会被赋予一个随机值.
unit Unit1;

interface

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

type
  TForm1 = class(TForm)
    procedure FormCreate(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.dfm}

var
  i1: Integer; {会被自动初始化为 0}
  p1: Pointer; {会被自动初始化为 0, 也就是空指针}
const
  BoolStr: array[Boolean] of string = ('False','True');

procedure TForm1.FormCreate(Sender: TObject);
var
  i2: Integer; {会被初始化为一个随机值}
  p2: Pointer; {会被初始化为一个随机地址}
  b: Boolean;
begin
  ShowMessage(IntToStr(i1));          {0}
  ShowMessage(IntToStr(i2));          {14909552}
  ShowMessage(IntToStr(Integer(p1))); {0}
  ShowMessage(IntToStr(Integer(p2))); {4570636}

  {判断 p1、p2 两个指针是否是已赋值}
  b := Assigned(p1);
  ShowMessage(BoolStr[b]);  {False}
  b := Assigned(p2);
  ShowMessage(BoolStr[b]);  {True}

  {释放指针后再判断}
  p1 := nil;
  p2 := nil;
  b := Assigned(p1);
  ShowMessage(BoolStr[b]);  {False}
  b := Assigned(p2);
  ShowMessage(BoolStr[b]);  {False}
end;

end.

posted on 2008-05-02 19:44  万一  阅读(2120)  评论(1编辑  收藏  举报