Nextrp [CPP] RU + Many GEOs Игра на карте России | NEXTRP

Are delphi variables initialized with a value by default?

delphi variables initialization

57313 просмотра

9 ответа

5464 Репутация автора

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?

Автор: MB. Источник Размещён: 25.09.2008 11:28

Ответы (9)


20 плюса

1215 Репутация автора

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.

Автор: Martin Liesén Размещён: 25.09.2008 11:33

100 плюса

2174 Репутация автора

Решение

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 to 0 etc as well;

  • Local reference-counted* variables are always initialized to nil or '';

  • Local non reference-counted* variables are uninitialized so you have to assign a value before you can use them.

I remember that Barry Kelly somewhere wrote a definition for "reference-counted", but cannot find it any more, so this should do in the meantime:

reference-counted == that are reference-counted themselves, or directly or indirectly contain fields (for records) or elements (for arrays) that are reference-counted like: string, variant, interface or dynamic array or static array containing such types.

Notes:

  • record itself is not enough to become reference-counted
  • I have not tried this with generics yet
Автор: Giacomo Degli Esposti Размещён: 25.09.2008 11:41

6 плюса

35403 Репутация автора

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.

Автор: Ondrej Kelle Размещён: 25.09.2008 11:42

4 плюса

73326 Репутация автора

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 Размещён: 25.09.2008 11:57

8 плюса

1081 Репутация автора

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 Размещён: 25.09.2008 12:03

4 плюса

35403 Репутация автора

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."

Автор: Ondrej Kelle Размещён: 25.09.2008 02:27

3 плюса

7601 Репутация автора

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 Размещён: 25.09.2008 02:46

26 плюса

37510 Репутация автора

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 Размещён: 25.09.2008 03:58

16 плюса

7690 Репутация автора

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 Размещён: 30.01.2009 01:30
Вопросы из категории :
32x32