3

I've found this code on web as an example, but I think this is not correct. An address to automatic variable is returned and this is just coincidence that it might work sometimes:

returning a pointer to an destroyed local variable, which becomes invalid memory location, is undefined behavior.

My only little hesitancy is about the pointer being static, but I think this changes nothing as this is the variable that should be static not a pointer: local variable is going to be destroyed. Can you please confirm or deny?

double *& showNumber()
{
    double n = 1550.85;
    static double *v = &n;
    return v;
}

int main(int argc, char *argv[])
{
    double sn = *showNumber();
    sn = *showNumber();
    //...
}
1
  • static persists for the entire duration of the program as soon as you initialize them so this should be okay for v, not for n Commented Mar 1, 2014 at 13:07

5 Answers 5

2

For this code to be well-defined, both n and v would need to be static.

Right now, the *showNumber() has undefined behaviour as it dereferences a dangling pointer.

Sign up to request clarification or add additional context in comments.

4 Comments

only n should be static
v should also be static as intended use of this function is also to return a pointer I think
@bolov: Why is that (bearing in mind that v is returned by reference)?
I am tired, didn't see the return by reference. You are right
2

Your code has still undefined behaviour because the value of the static pointer is invalid after exiting the function. The local variable refered to by the pointer will be destroyed.And any next time when the function will be called the address of this local variable can be different.

You could write your function the following way

double * showNumber()
{
    static double n = 1550.85;
    return &n;
}

In this case the returned pointer would contain the same valid value.

1 Comment

In C, the code static double* v = &n; would be illegal, because &n is not a compile-time constant. In C++, it is legal; the initialisation happens the first time the static variable is used, and only once. So even if you wrote *v = 1.0; inside that function, it would probably crash on the second call.
1

Static variables persist for the entire duration of the program as soon as you initialize them. You're all set for v, but not for n's address.

Comments

0

If pointer and it's variable both are static then only code will be OK. Otherwise local variable will anyway going to die.

Comments

0

Independent of local or global, static variable will live till the end of program. but it is recommenced that to use global when we return the value of that variable. because static local may get affected due to threads.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.