简体   繁体   English

我对变量在递归函数中的工作方式的理解正确吗?

[英]Is my understanding of how variables work in recursive function correct?

I've recently gotten to recursion in my C++ class and I was wondering is it correct to think that the scope of the variables are local to the specific stack frame: 最近,我在C ++类中进行了递归,我想知道变量的范围对于特定的堆栈框架是本地的是否正确:

3) return 3)返回

2) flipString(" "); 2)flipString(“”); begin = a, end = t 开始= a,结束= t

1) flipString(at); 1)flipString(at); begin = s, end = r 开始= s,结束= r

So when it reaches the return statement it will pop the third frame off, 因此,当到达return语句时,它将弹出第三帧,

then the 2nd frame off with s = t + " " + a 然后以s = t +“” + a关闭第二帧

and then the first frame with s = r + at + s thus reversing the string. 然后第一帧s = r + at + s,从而反转字符串。

void flipString(string &s)
{
   if (s.size() < 2)
   {
      return;
   }
   else
   {

      string begin;
      string end;      

      begin = s.at(0);
      end = s.at(s.size()-1);

      s.erase(s.begin());
      s.erase(s.end()-1);

      flipString(s); 

      s = end + s + begin;   
   }

   return;
}

Yes, your idea of how the function works and the function itself seem correct. 是的,您对功能如何工作以及功能本身的想法似乎是正确的。

There is a different instance of the local variables begin and end in each invocation of the function, placed on the stack. 在每次调用函数时,都有一个不同的局部变量开始和结束实例,这些实例放在堆栈上。

The function argument s references or points to the same string in all stack frames, because, well, it's a reference. 函数参数s引用或指向所有堆栈帧中的相同字符串,因为它是一个引用。

If a plain function argument is passed (without a reference and not as a pointer) there would be a separate copy of that argument too at each call stack level. 如果传递一个普通的函数参数(没有引用而不是指针),则在每个调用堆栈级别也将有该参数的单独副本。

There are three symbol-types to understand, in this context: 在这种情况下,需要了解三种符号类型:

  1. Local variables — symbols declared within a function 局部变量-函数内声明的符号
  2. Formal parameters — symbols used as parameters within a function's implementation 形式参数-在函数实现中用作参数的符号
  3. Actual parameters — expressions passed by the caller on a function's invocation 实际参数-调用者在函数调用时传递的表达式

It is also useful to understand where the values are stored and how the stack works. 了解值的存储位置以及堆栈的工作方式也很有用。

A stack-frame is created for each function invocation. 为每个函数调用创建一个堆栈框架。 This creates a context for the function's execution and a reference to the caller's context. 这将创建函数执行的上下文以及对调用者上下文的引用。 When the function exits, the function's stack frame is removed (popped) and the caller's context is restored. 当函数退出时,将删除(弹出)函数的堆栈框架,并恢复调用者的上下文。

Formal parameters' locations are reserved in the stack-frame of the function being invoked. 形式参数的位置保留在所调用函数的堆栈框架中。 Similarly, local variables are stored in the the function's stack-frame. 同样,局部变量存储在函数的堆栈框架中。 Actual parameters or references to them are copied to the stack where they can be referenced by their formal parameter counterparts. 实际参数或对它们的引用将复制到堆栈中,在此堆栈中它们的形式参数对应对象可以对其进行引用。

When the function has exited, the stack-frame is freed up along with the formal parameter and local variable space. 函数退出后,将释放堆栈帧以及形式参数和局部变量空间。

Note that for parameters passed as a reference ( & ) to actual values, the space for the values may exist outside the current stack-frame (only the reference address, itself, is within the stack-frame. Forgetting this can lean to confusion about why values are persisting across function invocations. This is more obvious with pointers, but may be overlooked when passing references. 请注意,对于作为参考值( & )传递给实际值的参数,这些值的空间可能存在于当前堆栈框架之外(只有引用地址本身位于堆栈框架之内。忘了这可能会使人混淆为什么值会在函数调用之间保持不变 。这在使用指针时更为明显,但在传递引用时可能会被忽略。

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

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