简体   繁体   中英

What is the need, local static variables are allocated memory during compile time?

I'm learning about static variables in C and got to know that memory for static variables is allocated at compile time (how much memory has to be allocated and its virtual address is calculated during compile time and actual memory is allocated when program loads) in either data segment/.bss depending whether initialized or not

I have seen in some web site posts that since the size the object/variable will take is predefined based on the variable type, the memory is allocated at compile time. But I didn't understand the need of this in case of local static variables that are defined in a function and whose scope is only within the function.

Consider the following code snippet:

void func()
{
    static int i;
    /*some logic*/
} 

void func1()
{
    static int data[10] = {1,2,3,4,5,6,7,8,9,10};
    /*some logic*/
}

int main()
{
    /*logic not involving func() and func1()*/
}

In this case, the functions func and func1 are not at all invoked in the program, yet the memory for static variables in those functions are allocated as soon as the program loads ( from what I learnt ) which is actually not used. So, with this drawback what's the use of allocating memory for local static variables. Why can't the compiler allocate memory for them in the data segment when it goes through the function.

I have gone through stack overflow questions regarding this and couldn't get an exact answer Please help!!!

Allocating and initializing the memory at compile time means the program doesn't have to keep track of whether the function has already been entered and the variable has been initialized. Local static variables with constant initial values are treated essentially the same as global variables, except that the name is only in the scope of that function.

It's a time-space tradeoff -- initializing it during the first call would require code that has to be executed every time the function is called. Initializing it when the program is loaded means that its initialization is done as part of the block copy from the executable's text segment to the data segment of memory, along with global statics.

See What is the lifetime of a static variable in a C++ function? for the more complicated case of C++ local static variables. In C++ I would probably use a static std::array , which I don't think would be initialized until the function is entered.

If you have a large array in a function that's rarely called, and you don't want to waste memory for it, use a static pointer instead of a static array, and initialize it yourself.

void func1() {
    static int *data;

    if (!data) { // Need to protect this with a mutex if multi-threading
        data = malloc(N * sizeof(int));
        for (int i = 0; i < N; i++) {
            data[i] = i;
        }
    }
    ...
}

This is the code the compiler would have to generate to do first-time initialization of the array.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM