在delphi中初始化局部变量?

eli*_*ite -1 delphi variables initialization

在较旧的程序代码的审查过程中出现以下问题:方法中的所有局部变量在开始之后立即初始化.通常局部变量未初始化.但我们有一个程序,所有变量都初始化为0.是否有人知道如何发生这种情况?

例:

type
  TPrices = array[0..10, 0..5] of Integer;

procedure DoSomething();
var
  mPrices : TPrices;
  mValue  : Integer; 
begin
  if (mPrices[0,0] = 0) then
    MessageDlg('Zero', mtInformation, [mbOK], 0);
  if (mValue = 0) then
    MessageDlg('Zero Integer', mtInformation, [mbOK], 0);
end;
Run Code Online (Sandbox Code Playgroud)

Dav*_*nan 5

这只是机会.变量未初始化.变量将驻留在堆栈上,如果发生这种情况,那么最后写入堆栈位置的任何内容都为零,则其中的值仍为零.

非托管类型的局部变量未初始化.不要让像上面这样的巧合说服你.

考虑这个程序:

{$APPTYPE CONSOLE}

type
  TPrices = array[0..10, 0..5] of Integer;

procedure Foo;
var
  mPrices: TPrices;
begin
  Writeln(mPrices[0,0]);
end;

begin
  Foo;
end.
Run Code Online (Sandbox Code Playgroud)

当我在我的机器上运行时,输出是:

1638012

现在考虑这个程序:

{$APPTYPE CONSOLE}

type
  TPrices = array[0..10, 0..5] of Integer;

procedure Foo;
var
  mPrices: TPrices;
begin
  Writeln(mPrices[0,0]);
  FillChar(mPrices, SizeOf(mPrices), 0);
end;

procedure Bar;
var
  mPrices: TPrices;
begin
  Writeln(mPrices[0,0]);
end;

begin
  Foo;
  Bar;
end.
Run Code Online (Sandbox Code Playgroud)

这次输出是:

1638012
0

碰巧这两个函数将它们的局部变量放在同一个位置,并且第一个函数调用在返回之前将局部变量归零,这会影响第二个函数中另一个局部变量的未初始化值.

或者尝试这个程序:

{$APPTYPE CONSOLE}

type
  TPrices = array[0..10, 0..5] of Integer;

procedure Foo;
var
  mPrices: TPrices;
begin
  Writeln(mPrices[0,0]);
  FillChar(mPrices, SizeOf(mPrices), 0);
  mPrices[0,0] := 666;
end;

procedure Bar;
var
  mPrices: TPrices;
begin
  Writeln(mPrices[0,0]);
  Writeln(mPrices[0,1]);
end;

begin
  Foo;
  Bar;
end.
Run Code Online (Sandbox Code Playgroud)

现在的输出是:

1638012
666
0

正如您可能想象的那样,许多不同的东西可能会导致堆栈空间的内容发生变化.相信你所知道的.非托管类型的局部变量未初始化.