Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How will this variable be initialized?

I have the following

char mem_pool[1024*1024*64]; 

int main() {
    // ... 
}

I'm trying to get a thorough understanding how will mem_pool be initialized. After a lot of search my conclusions are :

  • it's a static initialization (not as in static the keyword, but as in "run before the program does - during static initialization phase")
  • it will run in 2 phases : zero initialization and default initialization (the second phase won't do anytning)
  • it's an array of PODs so the default initialization for every element should apply, but due to the previous 2 points we won't have an array of indeterminable values (as we would with a char ar[N] in a function scope) but an array of zeroes.

Could someone help me dissambiguate what's guaranteed by the language and correct me if I'm wrong ?

I also thought of doing any of the following

char mem_pool[1024*1024*64] {}; 
char mem_pool[1024*1024*64] = ""; 

I suspect it's a better/recommended practice, but for now I need to understand my initial question.

like image 755
Lorah Attkins Avatar asked Mar 17 '15 21:03

Lorah Attkins


People also ask

How will you initialize a variable?

The way of initializing a variable is very similar to the use of PARAMETER attribute. More precisely, do the following to initial a variable with the value of an expression: add an equal sign (=) to the right of a variable name. to the right of the equal sign, write an expression.

How do you initialize a variable in c?

Different ways of initializing a variable in Cint a, b; a = b = 10; int a, b = 10, c = 20; Method 5 (Dynamic Initialization : Value is being assigned to variable at run time.)

How are variables initialized in Java?

The syntax for an initializer is the type, followed by the variable name, followed by an equal sign, followed by an expression. That expression can be anything, provided it has the same type as the variable. In this case, the expression is 10, which is an int literal.

How many ways are there to initialize a final variable?

How many ways are there to initialize a final variable in java? 1 Initializing a final variable. Once you declare a final variable, it is a must to initialize it. ... At the time of... 2 Example1: At the time of declaration. 3 Example2: within an instance block. 4 Output. 5 Example3: within the default constructor. More ...

What is the purpose of initializing a variable?

Initializing a variable as Telastyn pointed out can prevent bugs. If the variable is a reference type, initializing it can prevent null reference errors down the line. A variable of any type that has a non null default will take up some memory to store the default value.

How do you initialize a variable in a class?

These variables should normally be final and initialized directly after definition using = or from within a class initializer block static { // initialize here }. As in many higher level and scripting languages fields will be automatically be assigned a default value. For numbers and char this will be the zero value.

What is dynamic initialization in Java?

Dynamic Initialization: Here, the variable is assigned a value at the run time. The value of this variable can be altered every time the program is being run. Method 3 (Declaring multiple variables simultaneously and then initializing them separately)


1 Answers

Your understanding is correct.

The array's elements will all be zero-initialised, because the array has static storage duration:

[C++11: 3.6.2/2]: Variables with static storage duration (3.7.1) or thread storage duration (3.7.2) shall be zero-initialized (8.5) before any other initialization takes place. [..]

[C++11: 8.5/5]: To zero-initialize an object or reference of type T means:

  • if T is a scalar type (3.9), the object is set to the value 0 (zero), taken as an integral constant expression, converted to T;
  • if T is a (possibly cv-qualified) non-union class type, each non-static data member and each base-class subobject is zero-initialized and padding is initialized to zero bits;
  • if T is a (possibly cv-qualified) union type, the object’s first non-static named data member is zero-initialized and padding is initialized to zero bits;
  • if T is an array type, each element is zero-initialized;
  • if T is a reference type, no initialization is performed.

If it did not have static storage duration, the elements would all have indeterminate values:

[C++11: 8.5/11]: If no initializer is specified for an object, the object is default-initialized; if no initialization is performed, an object with automatic or dynamic storage duration has indeterminate value. [..]

[C++11: 8.5/6]: To default-initialize an object of type T means:

  • if T is a (possibly cv-qualified) class type (Clause 9), the default constructor for T is called (and the initialization is ill-formed if T has no accessible default constructor);
  • if T is an array type, each element is default-initialized;
  • otherwise, no initialization is performed.
like image 158
Lightness Races in Orbit Avatar answered Oct 07 '22 07:10

Lightness Races in Orbit