tags:

views:

4269

answers:

10

I'm new to Delphi, and I've been running some tests to see what object variables and stack variables are initialized to by default:

TInstanceVariables = class
  fBoolean: boolean; // always starts off as false
  fInteger: integer; // always starts off as zero
  fObject: TObject; // always starts off as nil
end;

This is the behaviour I'm used to from other languages, but I'm wondering if it's safe to rely on it in Delphi? For example, I'm wondering if it might depend on a compiler setting, or perhaps work differently on different machines. Is it normal to rely on default initialized values for objects, or do you explicitly set all instance variables in the constructor?

As for stack (procedure-level) variables, my tests are showing that unitialized booleans are true, unitialized integers are 2129993264, and uninialized objects are just invalid pointers (i.e. not nil). I'm guessing the norm is to always set procedure-level variables before accessing them?

+7  A: 

Class fields are default zero. This is documented so you can rely on it. Local stack varaiables are undefined unless string or interface, these are set to zero.

mliesen
Thanks. "Zero" is confusing me a bit - does that mean strings are '', and interfaces are nil?
MB
Yes, exactly that. nil = 0 (at the assembler level) and '' = nil (Delphi convention).
gabr
Cheers - that makes sense, thanks!
MB
+14  A: 

Yes, this is the documented behaviour:

  • Object fields are always initialized to 0, 0.0, '', False, nil or whatever applies.

  • Global variables are always initialized.

  • Local variables are unitialized so you have to assign a value before you can use them.

Giacomo Degli Esposti
As Giacomo pointed out in the comments below, this is all explained in the Delphi help files at ms-help://borland.bds4/bds4ref/html/Variables.htm. In Delphi 2009 I found the same info by searching the help for "variables" (funnily enough I tried lots of searches but I didn't think to try that one).
MB
Local variables ARE initialized ($0) if they are of a managed type like strings, interfaces, dynamic arrays or variants
François
If this is 'the' answer, I think someone should add what sets and enums are initialized to for completeness.
Peter Turner
A: 

Global variables and object instance data (fields) are always initialized to zero. Local variables in procedures and methods are not initialized in Win32 Delphi; their content is undefined until you assign them a value in code.

TOndrej
A: 

Global variables are initialized? Where is that documented?

Thomas Mueller
I read it in the guide, but I cannot find it. Anyway it is referred in the "style guide" of Jedi group:http://homepages.borland.com/jedi/jcl/documents/styleguide.html
Giacomo Degli Esposti
I found it! :)In delphi 2006 help you can find it here:ms-help://borland.bds4/bds4ref/html/Variables.htm"If you don't explicitly initialize a global variable, the compiler initializes it to 0. Object instance data (fields) are also initialized to 0. "
Giacomo Degli Esposti
A: 

Even if a language does offer default initializations, I don't believe you should rely on them. Initializing to a value makes it much more clear to other developers who might not know about default initializations in the language and prevents problems across compilers.

Thomas Owens
Of course you can. And you should. Initializing everything to 0/''/false/nil in every constructor is just unnecessary.Initializing global variables, on other hand, is not so stupid - I for once can never remember if they are initialized or not (as I'm not using them much).
gabr
If Delphi let you initialize a variable at the same point as you declare it (e.g. var fObject: TObject = nil) I'd be inclined to agree that initializing to a value is probably a good idea. But to me it seems a bit much to do it in the constructor for every object field.
MB
+2  A: 

Here's a quote from Ray Lischners Delphi in a Nutshell Chapter 2

"When Delphi first creates an object, all of the fields start out empty, that is, pointers are initialized to nil, strings and dynamic arrays are empty, numbers have the value zero, Boolean fields are False, and Variants are set to Unassigned. (See NewInstance and InitInstance in Chapter 5 for details.)"

It's true that local-in-scope variables need to be initialised... I'd treat the comment above that "Global variables are initialised" as dubious until provided with a reference - I don't believe that.

edit... Barry Kelly says you can depend on them being zero-initialised, and since he's on the Delphi compiler team I believe that stands :) Thanks Barry.

Drew Gibson
In delphi 2006 help you can find it here: ms-help://borland.bds4/bds4ref/html/Variables.htm "If you don't explicitly initialize a global variable, the compiler initializes it to 0. Object instance data (fields) are also initialized to 0. "
Giacomo Degli Esposti
+1  A: 

From Delphi 2007 help file:

ms-help://borland.bds5/devcommon/variables_xml.html

"If you don't explicitly initialize a global variable, the compiler initializes it to 0."

TOndrej
+1  A: 

I have one little gripe with the answers given. Delphi zeros out the memory space of the globals and the newly-created objects. While this NORMALLY means they are initialized there is one case where they aren't: enumerated types with specific values. What if zero isn't a legal value??

Loren Pechtel
Zero is always a legal values, it is the 1st value of the enum. you can see it with ord(MyFirstEnumValue).
François
It would return the first value in the enumerated type.
skamradt
Zero is not always a legal value if you explicitly assign values to the enum. In that case, it's still initialized to 0, and you have an illegal value. But enums are just syntactic sugar painted over normal integer types, so this doesn't really break anything. Make sure your code can deal with it.
Mason Wheeler
+11  A: 

Global variables that don't have an explicit initializer are allocated in the BSS section in the executable. They don't actually take up any space in the EXE; the BSS section is a special section that the OS allocates and clears to zero. On other operating systems, there are similar mechanisms.

You can depend on global variables being zero-initialized.

Barry Kelly
+1  A: 

Just as a side note (as you are new to Delphi): Global variables can be initialized directly when declaring them:

var myGlobal:integer=99;

Heinrich Ulbricht