Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Are "class var"s initialized to zero?

I know that, in Delphi, instance variables and global variables are initialized to zero (this has been asked here before).

However, what about static variables (class var)? I would expect class vars to be initialized to zero, just like global variables. But I've seen too many new Delphi compiler features that were still half-baked to assume that it works, without documentation that actually states a guarantee.

The Help has no index entry for "class var". The "Fields" topic mentions class fields, but does not specify whether they're initialized at program startup. And the obvious fix, of explicitly initializing them (class var X: Integer = 0;), doesn't compile ("';' expected but '=' found").

Are class variables initialized to zero? Is there documentation that explicitly states this?

like image 724
Joe White Avatar asked Jun 26 '09 15:06

Joe White


People also ask

Which variables are automatically initialized to zero?

3) Static variables (like global variables) are initialized as 0 if not initialized explicitly.

Are class variables initialized?

In C++, class variables are initialized in the same order as they appear in the class declaration.

Which variable is initialized to zero by compiler?

Global and static variables are initialized to their default values because it is in the C or C++ standards and it is free to assign a value by zero at compile time. Both static and global variable behave same to the generated object code.

Are C variables initialized to 0?

Unlike some programming languages, C/C++ does not initialize most variables to a given value (such as zero) automatically. Thus when a variable is given a memory address to use to store data, the default value of that variable is whatever (garbage) value happens to already be in that memory address!


1 Answers

I'm not aware of any documentation that explicitly states it, but class vars are just a special type of global variable, and globals get zeroed.

like image 128
Mason Wheeler Avatar answered Sep 17 '22 22:09

Mason Wheeler