My understanding about static variables declared inside a function is:
.bss
, otherwise in .data
main
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?
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.
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.
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.
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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With