【发布时间】:2010-10-26 01:15:37
【问题描述】:
所以我总是听说类字段(基于堆)已初始化,但基于堆栈的变量没有。我还听说记录成员(也是基于堆栈的)也没有初始化。编译器警告局部变量未初始化([DCC 警告] W1036 变量“x”可能尚未初始化),但不警告记录成员。所以我决定做一个测试。
对于所有记录成员,我总是从 Integers 中得到 0,从 Booleans 中得到 false。
我尝试打开和关闭各种编译器选项(调试、优化等),但没有区别。我所有的记录成员都在初始化。
我错过了什么?我正在使用 Delphi 2009 Update 2。
program TestInitialization;
{$APPTYPE CONSOLE}
uses
SysUtils;
type
TR = Record
Public
i1, i2, i3, i4, i5: Integer;
a: array[0..10] of Integer;
b1, b2, b3, b4, b5: Boolean;
s: String;
End;
var
r: TR;
x: Integer;
begin
try
WriteLn('Testing record. . . .');
WriteLn('i1 ',R.i1);
WriteLn('i2 ',R.i2);
WriteLn('i3 ',R.i3);
WriteLn('i4 ',R.i4);
WriteLn('i5 ',R.i5);
Writeln('S ',R.s);
Writeln('Booleans: ', R.b1, ' ', R.b2, ' ', R.b3, ' ', R.b4, ' ', R.b5);
Writeln('Array ');
for x := 0 to 10 do
Write(R.a[x], ' ');
WriteLn;
WriteLn('Done . . . .');
except
on E:Exception do
Writeln(E.Classname, ': ', E.Message);
end;
ReadLn;
end.
输出:
测试记录。 . . . 我1 0 i2 0 i3 0 i4 0 i5 0 小号 布尔值: FALSE FALSE FALSE FALSE FALSE 大批 0 0 0 0 0 0 0 0 0 0 0 完毕 。 . . .【问题讨论】:
标签: delphi variables initialization delphi-2009