Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How are local and global variables initialized by default?

Based on below, am i right?

  • global_A reference is initialized to null.
  • global_int is 0
  • local_A reference is null
  • local_int is uninitialized
  • Both global_A.x and local_A.x is uninitialized.

THanks for any help.


A global_A;
int global_int;

class A {
  public : int x;
}

int main()
{
  int local_int;
  A local_A;
}
like image 210
extdummy Avatar asked Aug 24 '10 04:08

extdummy


People also ask

Are local variables initialized by default?

The local variable name has been used, that is, read from, before it has been assigned a value. In C and C++, local variables aren't initialized by default.

How are global variables initialized?

In C, static and global variables are initialized by the compiler itself. Therefore, they must be initialized with a constant value.

What is the default value of local and global variables?

Default value→ All uninitialized global variables will have 0 as the default value. Lifetime→ The lifetime of these variables is till the end of the execution of the program.

How are local variables initialized?

Local Variables Declarations specify the type followed by the name, and optionally the initialization. Initialization sets the variable to a new instance. It must be to a type that is compatible with the declaration type. In the above code, the variable a is declared as a string and is initialized to "Hello World".


2 Answers

Building up on Andrey's response.

$3.6.2- "Objects with static storage duration (3.7.1) shall be zero-initialized (8.5) before any other initialization takes place.". In OP, "global_A" and "global_int" have static storage duration. "local_int" and "local_A" have no linkage as these are local objects.

$8.5/5- To zero-initialize an object of type T means:

— if T is a scalar type (3.9), the object is set to the value of 0 (zero) converted to T;

— if T is a non-union class type, each nonstatic data member and each base-class subobject is zeroinitialized;

— if T is a union type, the object’s first named data member89) is zero-initialized;

— if T is an array type, each element is zero-initialized;

— if T is a reference type, no initialization is performed.

$6.7.4/4- "The zero-initialization (8.5) of all local objects with static storage duration (3.7.1) is performed before any other initialization takes place. A local object of POD type (3.9) with static storage duration initialized with constant-expressions is initialized before its block is first entered. An implementation is permitted to perform early initialization of other local objects with static storage duration under the same conditions that an implementation is permitted to statically initialize an object with static storage duration in namespace scope(3.6.2). Otherwise such an object is initialized the first time control passes through its declaration; such an object is considered initialized upon the completion of its initialization. If the initialization exits by throwing an exception, the initialization is not complete, so it will be tried again the next time control enters the declaration. If control re-enters the declaration (recursively) while the object is being initialized, the behavior is undefined."

EDIT 2:

$8.5/9- "If no initializer is specified for an object, and the object is of (possibly cv-qualified) non-POD class type (or array thereof), the object shall be default-initialized; if the object is of const-qualified type, the underlying class type shall have a user-declared default constructor. Otherwise, if no initializer is specified for a nonstatic object, the object and its subobjects, if any, have an indeterminate initial value90); if the object or any of its subobjects are of const-qualified type, the program is ill-formed."

In general, you want to read up these sections along with $8.5 for good hold on this aspect.

like image 173
Chubsdad Avatar answered Oct 22 '22 17:10

Chubsdad


There are no references in your code, so any of your points that mention "references" make no sense.

In your example, both global object - global_int and global_A - are zero-initialized. Both local objects - local_int and local_A - contain indeterminate values, which means that local_int and local_A.x are not initialized.

P.S. Of course, as other already noted, your code is non-compilable. You can't declare A objects before declaring class A (and you are missing a ; after the class definition).

like image 45
AnT Avatar answered Oct 22 '22 17:10

AnT