Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why can't I initialize a static variable with a non literal value?

I had this code:

int foo(void){
    return 1;
}

int main(void){
    static const int x = foo();

    //do stuff
    return 0;
}

But I got an error about initializing a static variable with a non-const value. I thought it had something to do with the const specifier, but it didn't. I ended dropping the const keyword and doing this:

int foo(void){
    return 1;
}

int main(void){
    static int x = 0;
    if (x == 0) x = foo();

    //do stuff
    return 0;
}

Now, why can't the compiler just delay the initialization of the static int x variable until it's used, and more importantly, why can't it just put it in a read-write section, and just enforce that it's not written to in compile time? I'd like to use the const AND static keyword for improved semantics in my code, but I don't really care how the compiler handles this, just let it work.

Is my understanding of the C standard wrong? Or is my compiler sucking? It's MSVC 9.0.

like image 209
Spidey Avatar asked Mar 26 '12 17:03

Spidey


People also ask

Can we initialize static variable with another variable?

You can define a static field using the static keyword. If you declare a static variable in a class, if you haven't initialized it, just like with instance variables compiler initializes these with default values in the default constructor. Yes, you can also initialize these values using the constructor.

What controls the initial value of the non initialized static variables?

If the initial value of a static variable can't be evaluated at compile time, the compiler will perform zero-initialization. Hence, during static initialization all static variables are either const-initialized or zero-initialized. After static initialization, dynamic initialization takes place.

Can a static variable be initialized in a method?

The only way to initialize static final variables other than the declaration statement is Static block. A static block is a block of code with a static keyword. In general, these are used to initialize the static members. JVM executes static blocks before the main method at the time of class loading.

Can we initialize non-static variable in static Block?

In the static method, the method can only access only static data members and static methods of another class or same class but cannot access non-static methods and variables.


2 Answers

C requires it.

From the C Standard:

(C99, 6.7.8p4) "All the expressions in an initializer for an object that has static storage duration shall be constant expressions or string literals."

Note that the const qualifier does not mean constant but rather read-only. A const object is not a constant in C.

The reason a static object cannot be initialized by a non constant value is related to the fact that the initialization of a static object is done "prior to program startup" (C99, 6.2.4p3).

like image 172
ouah Avatar answered Nov 11 '22 07:11

ouah


The value for initialization must be determined at compile or link time. C doesn't have the concept of constructors that could be run at the startup of the program.

like image 26
Jens Gustedt Avatar answered Nov 11 '22 05:11

Jens Gustedt