0

I wonder, is the marked line in the below code correct. Because in this line the result of the function is assigned to the static variable prevRecCallResult (I'll call it "plain assignment"), which is changed inside this function (I'll call it "inside assignment"). Is it guaranteed, that the "inside assignment" is done, when the "plain assignment" executes?

int f(int _n)
{
  if (_n >= 1)
  {
    static int prevRecCallResult;
    prevRecCallResult = f(_n - 1);  //<-- Is this line Ok?
    return prevRecCallResult + 1;
  }
  else
    return _n;
}

I know, the standard says, that a sequence point occurs:

At a function return, after the return value is copied into the calling context.

, but I'm not sure, this is the answer to my question.

Update:

Considering replies I've received, I should clarify my question:

It's essence is: Is it true, that the prevRecCallResult is not in use by the assignment expression (in marked line) (i.e. is not occupied by it) until f(_n - 1) is finished? (And thus, until this moment, prevRecCallResult is absolutely free for any assignments inside f(_n - 1)?)

user1234567
  • 3,991
  • 3
  • 19
  • 25
  • 1
    Using the static variable makes the function thread unsafe (and possibly a bit inefficient for the general case of other types, since the compiler will have to endeavor to [make the initialization of that variable thread safe](http://stackoverflow.com/questions/8102125/is-local-static-variable-initialization-thread-safe-in-c11)). – Cheers and hth. - Alf Jan 16 '15 at 14:05

2 Answers2

1
static int prevRecCallResult;
prevRecCallResult = f(_n - 1);  //<-- Is this line Ok?

Your code is perfectly OK. But just wanted to make you remember that static int prevRecCallResult; executes only once. But prevRecCallResult = f(_n - 1); is assigned after each function call. Once function return prevRecCallResult's at time function's return value will be used in rest of the function.

One more thing, static variable will not die, after you return from function. So prevRecCallResult will not die across function calls.

Pranit Kothari
  • 9,721
  • 10
  • 61
  • 137
  • but can you tell, what is the reason to think, that this line of my code is correct? – user1234567 Jan 16 '15 at 14:14
  • @user3241228 Why it is wrong. You are calling `prevRecCallResult` recursively, every-time you are decrementing value of parameter by 1 and taking return in `prevRecCallResult`, now you may use it. What is wrong in that? – Pranit Kothari Jan 16 '15 at 14:18
  • @user3241228 I have updated my answer with --> One more thing, static variable will not die, after you return from function. So prevRecCallResult will not die across function calls. – Pranit Kothari Jan 16 '15 at 14:21
0

As I remember all static variables as well as all global variables in C and C++ are assigned automatically to default value for their types - in this particular example default value for 'static int prevRecCallResult' will be 0. So your fears are unfounded (you can easily check this with debugger). At the same time I cannot understand why you use static variable in this code... is it just simplified code for question or is it real code where you trying to economize memory on automatic variable of recursive function?

VolAnd
  • 6,367
  • 3
  • 25
  • 43
  • It's a slightly simplified example. Yes, I use `static` here to prevent creation as many copies of this variable, as the recursion depth is. – user1234567 Jan 18 '15 at 13:13
  • I understand. So, you can use feature of static variables - [link](http://stackoverflow.com/questions/2091499/why-global-and-static-variables-are-initialized-to-their-default-values) – VolAnd Jan 19 '15 at 06:46