Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Initialization global and static variable to 0 is always unnecessary?

Tags:

c

C standard guarantees that global and static variables, if not initialized, are always 0.

Here's my question: the un-initialized global and static variables go to BSS segment in the program. So the so called 0 should be all-bit 0.

For integral variables, all-bit 0 will be evaluated as 0. The floating point variables, if following IEEE 754, is also 0.0. But for pointers, null pointers is not necessarily to be all-bit 0, so does initialization of a global pointer like this:

int* p = NULL;

make any difference to just:

int *p;
like image 788
Yu Hao Avatar asked Jun 09 '13 18:06

Yu Hao


People also ask

Are global variables always initialized to 0?

Yes, all members of a are guaranteed to be initialised to 0. If an object that has static storage duration is not initialized explicitly, it is initialized implicitly as if every member that has arithmetic type were assigned 0 and every member that has pointer type were assigned a null pointer constant.

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.

Is static variable initialization necessary?

Initialization of Instance variables But if you declare an instance variable static and final Java compiler will not initialize it in the default constructor therefore, it is mandatory to initialize static and final variables.

Are global variables initialized to 0 in C?

Global variables are automatically initialized to 0 at the time of declaration. Global variables are generally written before main() function. In line 4, a and b are declared as two global variables of type int . The variable a will be automatically initialized to 0.


2 Answers

The standard guarantees that a pointer with static storage duration and no other initializer will be initialized to be a null pointer, regardless of what bit pattern that may involve.

The same basic idea applies to floating point and integer types as well -- they're guaranteed to be initialized to 0 or 0.0 as well. The implementation can leave this to the fact that BSS sets all bits to 0 if and only if it "knows" that doing so will result in the correct values.

like image 135
Jerry Coffin Avatar answered Oct 22 '22 03:10

Jerry Coffin


All variables with static storage duration are guaranteed to be initailized to their respective zero values, which generally does not mean that they have to be physically filled with all-bits-zero pattern.

The reason such variables might go to BSS segment on some specific platform is that on the target platform the null pointer is indeed represented by an all-bits-zero pattern. I.e. the all-bits-zero initialization for pointers just happens to work correctly on that platform, so the compiler uses BSS as the simplest and most efficient way to implement the correct behavior on that specific platform. If that was not the case, the compiler would have to initialize such static variables differently.

That would apply to floating-point values, for example, if some platform used non-IEEE 754 representation for such values with a non-zero bit pattern for representing 0.0 (C does not mandate IEEE 754).

(Moreover, this even used to apply to all integral types larger than char, until one of the TCs for C99 standard finally required all-bits-zero pattern to be a valid object representation for integer zeros of all types on all C platforms.)

A good real-life example comes from C++ (a different language but relevant in this case). C++ makes the same guarantee for scalar variables with static storage duration. Meanwhile, many popular C++ implementations use 0xFFFFFFFF value to represent null pointers of pointer-to-data-member type. E.g.

SomeType SomeClass::*p = 0;

actually translates into code that fills p with all-bits-one pattern. So, if you declare a static variable of such type without an explicit initializer, the compiler will have to ensure that its initial value is all-bits-one pattern, not all-bits-zero pattern. Some compilers are known to get it wrong by putting such variables into BSS are forgetting about them, thus leaving such variables filled with all-bits-zero pattern.

like image 8
AnT Avatar answered Oct 22 '22 02:10

AnT