Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Declaring a function level static variable inside an if block that is never hit

Tags:

c

My understanding about static variables declared inside a function is:

  1. If no initial value is specified, the static variable will reside in .bss, otherwise in .data
  2. The memory for statics are allocated along with globals - i.e., well before the execution enters main
    • are these two assumptions correct ?
  3. When the execution hits the function for the first time, statics are initialized to the user specified value (or zero in case no initial value is specified).
  4. ...and they retain their values across subsequent invocations of the function

But what if I declare my static variable inside an if block? I assume my third point should be updated to "when the execution hits the line where the static variable is declared, they're initialized to ... " - am I right ?

Now, what if the if block in which they're declared is never hit (and the compiler is able to figure this out) - I understand that the variable will never be initialized; but does any memory get allocated for that variable?

I wrote two functions to try to figure out what's happening:

#include <stdio.h>

void foo()
{
    static foo_var_out;
    if(0){
        static foo_var_in_0;
        printf("%d %d\n", foo_var_in_0);
    } else {
        static foo_var_in_1;
        printf("%d %d\n", foo_var_in_1);
    }   
}

static void bar(int flag)
{
    static bar_var_out;
    if(flag){
        static bar_var_in_0;
        printf("%d %d\n", bar_var_in_0);
    } else {
        static bar_var_in_1;
        printf("%d %d\n", bar_var_in_1);
    }   
}

int main()
{
    foo();
    bar(0);
}

And I took the object dump:

$ gcc -o main main.c
$ objdump -t main | grep var
45:080495c0 l     O .bss    00000004              foo_var_in_1.1779
46:080495c4 l     O .bss    00000004              foo_var_out.1777
47:080495c8 l     O .bss    00000004              bar_var_in_1.1787
48:080495cc l     O .bss    00000004              bar_var_in_0.1786
49:080495d0 l     O .bss    00000004              bar_var_out.1785

From the output it looks like foo_var_in_0 was not created at all (presumably because it is inside an explicit if(0)), whereas bar_var_in_0 was created (as it is possible for the caller to pass a non-zero value - although the only caller is explicitly passing zero).

I guess my question is: is it correct to assume that no memory was allocated for the variable foo_var_in_0 at all? I am asking about this specific case; am I reading the objdump correctly - or should I be doing something more to verify if the variable will take some memory while the program is ran?

In other words, if the line that declares a function level static variable is never hit, is the variable actually declared at all?

If it will not be created at all, is this according to the C standard (less likely), or a compile time optimization and at what level - how do I turn it ON/OFF (in gcc 4.1.1)?

I understand that one int is not a big deal to care about, but I am more interested in how it works; also, what if the variable was a big array of size, say 5000 elements of a 10 byte struct?

like image 438
Amarghosh Avatar asked Nov 29 '10 14:11

Amarghosh


People also ask

Can static variable be declared inside function?

Static variables are the variables which have the property to preserve it's value from its previous scope. Static keywords can be used to declare a variable inside a function as well as to declare a global variable. If we have not initialized the value of a static variable, by default, it takes the value 0.

How do you declare a static variable in a function?

A static variable is a variable that is declared using the keyword static. The space for the static variable is allocated only one time and this is used for the entirety of the program. Once this variable is declared, it exists till the program executes.

How do you declare a static variable inside a function in Java?

Declaration Scope of the Static Variables in Java We cannot declare static variables in the main method or any kind of method of the class. static variables must be declared like a class member in the class.

Why static variables should not be declared inside structure?

Static variables should not be declared inside structure. The reason is C compiler requires the entire structure elements to be placed together (i.e.) memory allocation for structure members should be contiguous.


1 Answers

is it correct to assume that no memory was allocated for the variable foo_var_in_0 at all?

No, I don't think it would be correct to assume that. As far as I know, optimizations like this are not part of the standard.

If you know for a fact that you compiler does this and you want to assume it, go ahead. If you write anything that needs this to be the case, you might want to write a post-build test to make sure that it happened.

Probably, what you are seeing is a side-effect of the compiler just pruning out some code that it knew would never run. Meaning, it's not specifically looking to remove statics, but it did remove an entire branch, so any code in it just got removed as well.

like image 104
Lou Franco Avatar answered Oct 03 '22 07:10

Lou Franco