Does an automatic variable have a static lifetime if not initialized?

I have the concept of static local variables pretty well: global lifespan, local area. In the same way, I understand that automatic variables are allocated / deallocated automatically when the program flow enters and leaves the context of the variable.

#include <stdio.h> void test_var(void){ static unsigned foo = 0; unsigned bar = 0; printf(" %u %u\n", foo++, bar++); } int main(void){ printf("Foo Bar\n"); printf("--- ---\n"); for(unsigned x = 0; x < 10; x++){ test_var(); } return 0; } 

Thus, the previous example behaves as expected and outputs the following result:

 Foo Bar --- --- 0 0 1 0 2 0 3 0 4 0 5 0 6 0 7 0 8 0 9 0 

What confuses me is how the variables behave when not initialized:

 #include <stdio.h> void test_var(void){ static unsigned foo; /* not initialized */ unsigned bar; /* not initialized */ printf(" %u %u\n", foo++, bar++); } int main(void){ printf("Foo Bar\n"); printf("--- ---\n"); for(unsigned x = 0; x < 3; x++){ test_var(); } return 0; } 

Output:

 Foo Bar --- --- 0 2 1 3 2 4 3 5 4 6 5 7 6 8 7 9 8 10 9 11 

So the static variable behaves as expected - getting the default value of 0 and saving through function calls; but automatic variable seems to be preserved, although while holding the garbage value, it increases with every call.

Is this because the behavior is undefined in the C standard, or is there a set of rules in the standard that explain this?

+4
source share
1 answer

Standard C says that the lifetime of an object is the time during which storage is guaranteed for it (see, for example, 6.2.4 of ISO / IEC 9899: TC3). The lifetime of static and global variables is in the entire program, and for this, the above behavior is guaranteed by the standard. These values ​​are initialized before starting the program. For automatic objects, objects are located at a fixed address, but are guaranteed only through their service life. Thus, although the bar seems to stay alive on several function calls, you cannot guarantee this. That is why you should always initialize your variables, you can never find out which variable was in the same place before using it.

I adapted the program a bit to also print the address of both a static and a local variable:

 #include <stdio.h> void test_var(void){ static unsigned foo; /* not initialized */ unsigned bar; /* not initialized */ printf(" %u %u\t%p\t %p\n", foo++, bar++, &foo, &bar); } int main() { printf("Foo Bar\n"); printf("--- ---\n"); for(unsigned x = 0; x < 3; x++){ test_var(); } return 0; } 

This gave the following output on my computer:

 Foo Bar --- --- 0 33616 0x1067c 0xbee894fc 1 33617 0x1067c 0xbee894fc 2 33618 0x1067c 0xbee894fc 

This shows that on my machine, static foo and automatic bar were at the same address for every call, but this is a coincidence, and the C standard does not guarantee that bar will always be at the same address.

+1
source

Source: https://habr.com/ru/post/1480999/


All Articles