tags:

views:

1962

answers:

5

Is there any difference in these two? If so, what exactly is the difference? Assume they are in a C function that may be called multiple times.

  1. declare and assign in same statement

    static uint32_t value = x; // x varies and may be passed into function.

  2. declare in one statement and assign in next statment.

    static uint32_t value;

    value = x; // x varies;

Is value updated only the first time it is declared/initialized or even on subsequent calls.

My understanding of (1) is that it is only set the first time that line is executed so even if x changes the next time the line executes, value will remain the same. I am not sure about (2) but clarification on both will be very helpful

EDIT: Compiler ARM(ADS1.20). EDIT: A follow up question on (2) from the answers given so far. Is the declaration(not the assignment) repeated on every call or just the first time?

+11  A: 

The first should not compile; the static variable requires a constant initializer.

The second sets value each time the function is called, so there was no need to make it static in the first place.

If the first notation was correct - initialized value to 1, say - then it would be initialized once when the program starts and would thereafter only take new values when the code changed it. The second notation still sets value on each call to the function, and so renders the use of static pointless. (Well, if you try hard enough, you can devise scenarios under which the second version has a use for static. For example, if the function returns a pointer to it that other code then modifies, then it might be needed, but that is esoteric in the extreme and would be a pretty bad 'code smell'.)

Jonathan Leffler
The first example compiles for me.
Brian Neal
Or the value of x could depend on previous the previous value of value. Still seems like a code smell, like you say.
RussellH
Please check C++ standard 6.7.4. First example given is perfectly valid.
Luc Touraille
See section 6.7 paragraph 4 in the 2003 C++ spec. Static variables can be initialized with both const and non-const initializers/expressions. If initialized with a non-const expression, the object is initialized the first time control passes through the declaration.
Brian Neal
However, this might not be true in C. Maybe the question should be retagged to avoid confusion.
Luc Touraille
Yes, I believe it is different in C.
Brian Neal
@Brian Neal: which compiler on which platform? I used GCC on Cygwin, with: int xxx(int x) { static int value = x; return(x+1); } and got an 'initializer must be constant' error. C++ has different rules, so a C++ compiler would give a different answer.
Jonathan Leffler
Thanks for the answers and clarification.
MeThinks
+2  A: 

1 is only executed once, but for 2 value will be reassigned every time. Static variables are initialized only once.

rlbond
The first does not compile - the initializer must be a constant.
Jonathan Leffler
not if the questioner actually talked about C++ tho (given that he tagged it as C++). i often saw question where they asked about C++, but only wrote "C" but tagged as C++ (sadly..) . but also saw they really meant C but tagged C++ . let's wait for MeThinks
Johannes Schaub - litb
The question is explicit about C function, both in the title and the main text; the C++ tag was misplaced, I think.
Jonathan Leffler
A: 

Your intuition is right. In the second example value is set to x each time the method is called. Static variables need to be initialized and declared in one statement if you only want it to run once.

If you always want value to have the value x, don't declare it as static.

tomjen
+2  A: 

These are very different declarations.

The first one is declaring a static local variable and giving it an initial value (this should not actually compile given that x is not a constant). This will only occur once before the function is every executed. This is almost certainly the initialization you want.

The second declaration is updating the value every time the function is called. If you want the variable to always start the function with the same value this is the right approach. But if this is truly what you want, then why use a static at all? Just use a local variable.

JaredPar
x is not a constant.
Jonathan Leffler
@Jonathan, thanks, read right past that.
JaredPar
In 2, I was under the impression the declarion is only when the function is called the first time and the assignment is every time the function is called. Is this correct understanding? Is there any benefit in declaring only once and only doing the assignment on every call?
MeThinks
A: 

When compiled, the first one will be put into the ".data" section, where data is initialized, while the second one will be put into the ".bss" section, where data is uninitialized.

Use readelf -S xx.o can check the section size of compiled object file.

example 1: static int i;

void test(){
    i = 2; 
}

example 2:

 static int i=1;

void test(){
    i = 2; 
}
arsane