Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why are static variables auto-initialized to zero? [duplicate]

Tags:

c

Possible Duplicate:
Why global and static variables are initialized to their default values?

What is the technical reason this happens? And is it supported by the standard across all platforms? Is it possible that certain implementations may return undefined variables if static variables aren't explicitly initialized?

like image 367
j riv Avatar asked Jul 30 '10 15:07

j riv


People also ask

Are static variables initialized to zero?

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.

Which variables are automatically initialized to zero?

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. These variables are allocated in .

Why static variables are initialized only once?

Master C and Embedded C Programming- Learn as you go 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 static variables be initialized twice?

C# language specification makes it pretty clear that static fields are initialized only once, before the class is first used: The static field variable initializers of a class correspond to a sequence of assignments that are executed in the textual order in which they appear in the class declaration.


2 Answers

It is required by the standard (§6.7.8/10).

There's no technical reason it would have to be this way, but it's been that way for long enough that the standard committee made it a requirement.

Leaving out this requirement would make working with static variables somewhat more difficult in many (most?) cases. In particular, you often have some one-time initialization to do, and need a dependable starting state so you know whether a particular variable has been initialized yet or not. For example:

int foo() { 
    static int *ptr;

    if (NULL == ptr)
       // initialize it
}

If ptr could contain an arbitrary value at startup, you'd have to explicitly initialize it to NULL to be able to recognize whether you'd done your one-time initialization yet or not.

like image 94
Jerry Coffin Avatar answered Oct 20 '22 13:10

Jerry Coffin


Yes, it's because it's in the standard; but really, it's because it's free. Static variables look just like global variables to the generated object code. They're allocated in .bss and initialized at load time along with all your constants and other globals. Since the section of memory where they live is just copied straight from your executable, they're initialized to a value known at compile-time for free. The value that was chosen is 0.

like image 26
nmichaels Avatar answered Oct 20 '22 12:10

nmichaels