简体   繁体   English

将静态结果赋给静态变量的函数结果会在内部更改此静态变量

[英]assignation to the static variable the result of function which changes this static variable inside

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 " ). 因为在这一行中,该函数的结果分配给静态变量prevRecCallResult我将其称为“ 普通分配 ),该变量在此函数内部进行了更改( 我将其称为“ 内部分配 )。 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) (ie is not occupied by it) until f(_n - 1) is finished? 实质是:确实, f(_n - 1)完成之前 ,赋值表达式(在标记行中)未使用 prevRecCallResult (即,未被赋值表达式占用f(_n - 1)吗? (And thus, until this moment, prevRecCallResult is absolutely free for any assignments inside f(_n - 1) ?) (因此,直到这一刻,对于f(_n - 1) 内部的任何赋值, prevRecCallResult都是完全免费的吗?)

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; 但是只是想让您记住static int prevRecCallResult; executes only once. 只执行一次。 But prevRecCallResult = f(_n - 1); 但是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. 一旦函数返回prevRecCallResult的时间,函数的返回值将在其余函数中使用。

One more thing, static variable will not die, after you return from function. 从函数返回后,静态变量不会消失。 So prevRecCallResult will not die across function calls. 因此, prevRecCallResult将不会在函数调用之间死亡。

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). 我记得所有静态变量以及C和C ++中的所有全局变量都自动分配为其类型的默认值-在此示例中,“ static int prevRecCallResult”的默认值将为0。因此,您的担心是没有根据的(您可以轻松地使用调试器进行检查)。 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? 同时,我不明白为什么在此代码中使用静态变量...是只是简化的代码问题,还是试图在递归函数的自动变量上节省内存的真实代码?

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM