Bloomberg LP Interview Question
Financial Software DevelopersStatic keyword (file static or function static) have the following properties:
+ Initialized only once at runtime (to zero)
+ Placed in the data section of the program i.e. memory location is fixed for the life of the program
+ Retain the value for the entire life time of the program.
Function static variables are only visible inside the function they are declared in; File static variables are only visible inside the file they are declared in.
Contrast that with non-static Local variables. Local variables have the following properties
+ Not initialized, unless explicitly coded
+ Variable itself disappears once the block context disappears i.e. no retaining value business
+ Storage is allocated on the stack afresh each time a new block context is created.
For your question,
+ value of i after 2 iterations is 2, but the returned value for the second call to foo is 1. This is because of the post-increment operator.
+ All global variables need protection from multiple contexts. So, a lock is required for the load-modify phase of the variable i.
Static Hence its initialised to 0 . we must all see that its declared inside the function . So , the first time its called , its set to 1 . the second time also it should be reinitialised to 0 and value should be 1 again ..
if it was something like
int static i ;
int foo
{
return i++;
}
this would be 2 in the second iteration .
In this case
int foo
{
int static i ;
return i++
}
The reinitialising will definetely happen !!
correct me if i am wrong
Hi Mallika
Here reinitialization will not happen in second case also, the reason is static variables are stored in data section which is outside the stack of the executing function and hence it's value will persist until the program is terminated, When a function is called a new stack frame is created and all non static variables are reinitialized as per their types.
Hi ,
So , in this case i got what u sed .. so the answer is 1 or 2? bcos of the post increment thing? doest it return first and then increment ?
Dinesh is right, try this
#include<iostream>
using namespace std;
int foo() {
static int i;
return ++i;
}
int main() {
cout << foo() << endl;
cout << foo() << endl;
return 0;
}
The answer is
1
2
This code is not thread safe.
Since local Static Variables are not thread safe. For example
int foo()
{
static int i = SomeF();
return i++;
}
this function is internally converted as
int foo()
{
static bool i_init = false;
static int i; // Ths is uninitialized
if (!i_init) {
i_init = true;
i = SomeF();
}
return i++
}
Above code has race condition. So not thread safe.
Keeping this function's body inside Critical section can solve thsi problem.
final value of i is 2.
static variables are only initialized only once (default is 0) during compilation of program. and as they are incremented or changed inside the functions their values remains throughout the lifetime of the program. Static are saved inside the data segment like globals but have limited scope if defined inside the function.
Here there is no way of telling the value of i because i has not been initialized.
- Anonymous April 08, 2009Static keyword preserves the value of variable across function call.
Yes if the program is multithreaded then before return i++ each thread should obtain a lock return i