Declaring a function level static variable inside an if block that is never hit
My understanding about static variables declared inside a function is:
- If no initial value is specified, the static variable will reside in
.bss
, otherwise in.data
- The memory for statics are allocated along with globals - i.e., well before the execution enters
main
- are these two assumptions correct ?
- 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).
- ...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?
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.
The C standard does not prescribe where to place variables and stuff. It just prescribes that a conforming implementation shall have equivalent behaviour (to a reference behaviour which is specified by the standard), where "equivalent" is also defined by the standard.
So the simple answer is that it is an optimization, and how to turn it on/off depends on the particular compiler.
An implementation that does interprocedural analysis would probably be able to get rid of bar_var_in_0
as well.
Just to add to the correct answers from the others. Your assumptions about initialization of static
variables are not correct.
- Variables with static storage are
always initialized. Either explicitly
if you provide an initializer or
implicitly from
0
. - The initializer for such a variable must always be a compile time constant expression. So the value is computed at compile time and written directly into the object. (Well if it is all zero, some systems have tricks / special sections that avoid an explicit storage of the variable in the object file.)
- So, no, there will be no initializer statement run when the variable is accessed the first time (how would the system know, by the value of another static variable?) but whenever you start the program the variable is already initialized.
精彩评论