Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

result in simple code when variables are uninitialized

Tags:

delphi

Today one of my friend ask me about below code :

var
  a: Integer;
begin
  ShowMessage(IntToStr(a));
end;

This is local variable and not been initialized , ok ?

Put code in OnClick event of a button component and then run code in three diffrent ways below :

  1. Click on the button and see result , result = 1635841
  2. Press Enter key and see result , result = 1
  3. Press Space key and see result , reuslt = 1636097

I test code in two diffrent computer & see same result , any idea about this ?

like image 862
Mojtaba Tajik Avatar asked Jul 23 '11 14:07

Mojtaba Tajik


People also ask

What does it mean if a variable is uninitialized?

An uninitialized variable has an undefined value, often corresponding to the data that was already in the particular memory location that the variable is using. This can lead to errors that are very hard to detect since the variable's value is effectively random, different values cause different errors or none at all.

What happens when you try to use an uninitialized variable?

An uninitialized variable is a variable that has not been given a value by the program (generally through initialization or assignment). Using the value stored in an uninitialized variable will result in undefined behavior.

What is an uninitialized variable SET TO LET result What is result?

In computing, an uninitialized variable is a variable that is declared but is not set to a definite known value before it is used. It will have some value, but not a predictable one. As such, it is a programming error and a common source of bugs in software.

What happens if a variable is not initialized in C?

If a variable is declared but not initialized or uninitialized and if those variables are trying to print, then, it will return 0 or some garbage value. Whenever we declare a variable, a location is allocated to that variable.


2 Answers

procedure TForm1.Button1Click(Sender: TObject);
var
  a: Integer;
begin
    ShowMessage(IntToStr(Integer(a)));
end;

procedure TForm1.Button2Click(Sender: TObject);
begin
 ShowMessage(IntToStr(Integer(Pointer(TButtonControl(Button1)))));
end;

on my machine this code produces same message as compiler uses ebx register for a variable, while TButtonControl.WndProc uses ebx to store pointer to Self(as EAX will be overwritten after WinAPI function calls from TbuttonControl.WndProc) which is button1 before calling the actual handler Button1Click. So alas, on Delphi 2007 message text is too predictable.

[edited] You can see what's happening inside VCL while debugging if you turn on Use debug DCUs option in your project compiler options Compiler->Debugging->Use debug DCUs.

like image 39
Boris Treukhov Avatar answered Sep 17 '22 12:09

Boris Treukhov


Since the variable is not initialized, its value can be anything. Since your result is 'something', there is nothing unusual at all going on here.

like image 144
Andreas Rejbrand Avatar answered Sep 17 '22 12:09

Andreas Rejbrand