Take the 2-minute tour ×
Programmers Stack Exchange is a question and answer site for professional programmers interested in conceptual questions about software development. It's 100% free, no registration required.

Can anyone explain this piece of code?

CODE1

int &fun()
{
    static int x = 10;
    int &b =x;
    return b;
}
int main()
{
    fun() = 3;
    cout << fun();
    return 0;
}

Output: 3

CODE 2

int &fun()
{
    int x = 10;
    int &b =x;
    return x;
}
int main()
{
    fun() = 3;
    cout << fun();
    return 0;
}

Output: 10

share|improve this question

closed as off-topic by Dan Pichelman, Yusubov, Bart van Ingen Schenau, Dynamic, Kilian Foth Aug 19 '13 at 7:32

  • This question does not appear to be about software development within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

    
Admins I couldn't post any question on the stackoverflow forum of stackexchange.I don't know why, but i couldn't. So i posted this question here as the name is "PROGRAMMERS".... –  Munai Das Udasin Sep 1 '13 at 17:21
    
I would like to give a suggestion. Plz change the name of the forum to Software Development or something that "does appear to be about software development". Programmers is quite a board term.And people will get down voted for asking valid questions.Not to mention questions, not relating to software development. All programmers are not SOFTWARE DEVELOPERS Thank you.... –  Munai Das Udasin Sep 1 '13 at 17:30
add comment

3 Answers

up vote 3 down vote accepted

The static keyword makes the storage class of the int x static -- which means that it is essentially a global variable that has its access scoped by the function. In particular, the lifetime of x extends past the execution of the function, and the initializer is only executed once.

When fun() returns the reference to x in Code 1, the reference points to the static storage for x. As a result, the assignment to fun() in main sets the static variable x to 3, which is then returned when fun() is called again on the next line. Since the initializer for x is only executed once, the second call to fun() does not reset the value to 10.

The example in Code 2 is not valid. Returning a reference to a local variable is not valid because the storage for the local variable does not exist after the function returns. While it may appear to work in this case, the result of this code is undefined. Since local variables are generally stored on the stack, for this specific sequence of code, the value in the location that was used for x has not been trashed and the value is set by the initializer in fun(). But this is completely implementation dependent and the compiler specification makes no guarantees about this behavior.

In fact, if the code was a bit more complex that value could have been trashed and you might get some random value printed. Or the code could even crash. In any case don't return references or pointers to local variables as that is not valid code.

share|improve this answer
add comment

It means that x is shared in between function calls. It's the function equivalent of static members of classes.

Static members are shared in between instances while static function variables are shared in between invocations. This means that when you modify x after the first function call in your example it stays modified for all future invocations.

share|improve this answer
add comment
int &fun()
{
    static int x = 10; // x is statically allocated at compile time
    int &b =x;         // b is a reference to x
    return b;          // each call to fun() returns a reference to the same int
}
int main()
{
    fun() = 3;         // sets 'x' to 3
    cout << fun();     // it's still 3
    return 0;
}
/////////////////////////////////
int &fun()
{
    int x = 10; // x allocated on the stack whenever f runs
    int &b =x;  // does nothing, b is initialized but never used
    return x;   // undefined behavior.  After fun returns, x does not exist
}
int main()
{
    fun() = 3;  // more undefined behavior
    cout << fun(); // could print any number, or crash
    return 0;
}
share|improve this answer
add comment

Not the answer you're looking for? Browse other questions tagged or ask your own question.