Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialisation of static variable with itself

Tags:

c++

Consider following code example:

#include <iostream>

static int bar = bar;

int main()
{
    int foo = foo;
    std::cout << "foo = " << foo << std::endl;
    std::cout << "bar = " << bar << std::endl;
}

I get following compiler warning:

main.cpp: In function 'int main()':
main.cpp:7:15: warning: 'foo' is used uninitialized in this function [-Wuninitialized]
     int foo = foo;
               ^

Output:

foo = 0
bar = 0

I expected this warning as foo is used unitialised. Instead of 0, 'foo' can be anything. Self-assignment is undefined.

But why is the the self-assignment of 'bar' not warned? Is this assignment of 'bar' defined or undefined behaviour and why?

I know, static variables of elementar data types are initialised with '0' but in this case, the variable 'bar' is used during its initialisation. I'm wondering, if this is defined behaviour and the '0' is the expected output. (Which would explain, that no compiler warning occurs).

Link to Live example

like image 749
meddle0106 Avatar asked Apr 10 '14 14:04

meddle0106


People also ask

How do you initialize a static variable?

Static functions can be called directly by using class name. Static variables are initialized only once. Compiler persist the variable till the end of the program. Static variable can be defined inside or outside the function.

Can we initialize the static variable?

A static variable in a block is initialized only one time, prior to program execution, whereas an auto variable that has an initializer is initialized every time it comes into existence. A static object of class type will use the default constructor if you do not initialize it.

Are static variables initialized to 0?

3) Static variables (like global variables) are initialized as 0 if not initialized explicitly. For example in the below program, value of x is printed as 0, while value of y is something garbage.

Can we initialize static variable multiple times?

Static variables can be assigned as many times as you wish. static int count = 0; is initialization and that happens only once, no matter how many times you call demo .


1 Answers

I believe that part of the standard is relevant to your question (§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.[...]

So in this case, even before the compiler looks at your definition of bar, it has already zero-initialized it.

As it is specified a bit further in the standard, there shall be two phases for static variables initialization (emphasis mine).

Together, zero-initialization and constant initialization are called static initialization; all other initialization is dynamic initialization. Static initialization shall be performed before any dynamic initialization takes place.

like image 116
JBL Avatar answered Oct 08 '22 18:10

JBL