在delphi中初始化的局部变量? [英] local variable initialized in delphi?

查看:14
本文介绍了在delphi中初始化的局部变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在审查旧程序代码的过程中,出现了以下问题:方法中的所有局部变量都在开始后立即初始化.通常不初始化局部变量.但是我们有一个过程,其中所有变量都初始化为 0.有人知道这是怎么发生的吗?

例子:

类型TPrices = 整数数组[0..10, 0..5];程序 DoSomething();变量mPrices:TPrices;mValue:整数;开始如果 (mPrices[0,0] = 0) 那么MessageDlg('零', mtInformation, [mbOK], 0);如果 (mValue = 0) 那么MessageDlg('零整数', mtInformation, [mbOK], 0);结尾;

解决方案

这只是机会.变量未初始化.该变量将驻留在堆栈中,如果碰巧最后写入堆栈的该位置的内容为零,那么那里的值仍然为零.

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

考虑这个程序:

{$APPTYPE CONSOLE}类型TPrices = 整数数组[0..10, 0..5];程序Foo;变量mPrices:TPrices;开始Writeln(mPrices[0,0]);结尾;开始富;结尾.

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

<上一页>1638012

现在考虑这个程序:

{$APPTYPE CONSOLE}类型TPrices = 整数数组[0..10, 0..5];程序Foo;变量mPrices:TPrices;开始Writeln(mPrices[0,0]);FillChar(mPrices, SizeOf(mPrices), 0);结尾;程序栏;变量mPrices:TPrices;开始Writeln(mPrices[0,0]);结尾;开始富;酒吧;结尾.

这次的输出是:

<上一页>16380120

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

或者试试这个程序:

{$APPTYPE CONSOLE}类型TPrices = 整数数组[0..10, 0..5];程序Foo;变量mPrices:TPrices;开始Writeln(mPrices[0,0]);FillChar(mPrices, SizeOf(mPrices), 0);mPrices[0,0] := 666;结尾;程序栏;变量mPrices:TPrices;开始Writeln(mPrices[0,0]);Writeln(mPrices[0,1]);结尾;开始富;酒吧;结尾.

现在的输出是:

<上一页>16380126660

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

During a review process of an older programm code the following question arised: All local variables in a method are initialized right after begin. Usually local variables are not initialized. But we have a procedure where all variables are initialized to 0. Does anybody has an idea how this could happen?

Example:

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;

解决方案

This is just down to chance. The variable is not initialized. The variable will reside on the stack, and if it so happens that whatever was last written to that location of the stack was zero, then the value there will still be zero.

Local variables of unmanaged types are not initialized. Do not allow coincidences like the above persuade you otherwise.

Consider this program:

{$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.

When I run on my machine, the output is:

1638012

Now consider this program:

{$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.

This time the output is:

1638012
0

It so happens that the two functions place their local variables in the same location and the fact that the first function call zeroed the local variable before returning affects the uninitialized value of the other local variable in the second function.

Or try this program:

{$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.

Now the output is:

1638012
666
0

As you might imagine, many different things could lead to the content of that stack space changing. So trust what you know. Local variables of unmanaged types are not initialized.

这篇关于在delphi中初始化的局部变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆