关于 c++ 的问题递归和局部变量

发布于 2024-11-04 14:52:36 字数 391 浏览 0 评论 0原文

假设我有这个递归:

void doSomething(double j)
{
    double x;
    double y;

    x = j -1;
    y = j -2 ;

    doSomething(x+y);

    x = j + 31;
    y = j + 12 ;
}

我知道这个递归无限执行,但忽略

我的问题是关于递归树中变量 x 和 y 的范围... x 和 y 的范围仅对该特定函数中有效递归树中的阶段?或者当我再次调用 doSomething() 时,当递归树中的子 doSomething() 重新声明 x 和 y 时,它是否也会重置父级的 x 和 y 变量,或者是否创建一个全新的有效 x 和 y 变量仅适用于递归树中的那个阶段?

suppose I have this recursion:

void doSomething(double j)
{
    double x;
    double y;

    x = j -1;
    y = j -2 ;

    doSomething(x+y);

    x = j + 31;
    y = j + 12 ;
}

I know that this recursion executes infinitely, but just ignore that

My question is with regards to variables x and y's scope in the recursion tree...will x and y's scope be valid only for the function in that specific stage in the recursion tree? or when I call doSomething() again, when the child doSomething() in the recursion tree redeclares x and y, will it reset the parents' x and y variables as well or is it creating an entirely new x and y variables that is valid for that stage in the recursion tree only?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

2024-11-11 14:52:36

x 和 y 的作用域仅对递归树中特定阶段的函数有效吗?

是的。

当我再次调用 doSomething() 时,递归树中的子 doSomething() 重新声明 x 和 y 时,它是否也会重置父级的 x 和 y 变量

它是否创建了一个全新的 x 和 y 变量,仅对递归树中的该阶段有效?

是的。

编辑1:
这个例子应该会有帮助。

#include <iostream>

void foo( int temp )
{
     int num = temp;

     if( temp == 0)
          return; 

     foo(temp-1) ;

     std::cout << &num << "\t" << num << "\n" ;
}

int main()
{
     foo(5) ;
     return 0;
}

输出:

0xbfa4e2d0 1
0xbfa4e300 2
0xbfa4e330 3
0xbfa4e360 4
0xbfa4e390 5

请注意 num 的地址不同,并且每次调用都有其自己的 num 值。
Ideone

will x and y's scope be valid only for the function in that specific stage in the recursion tree?

Yes.

when I call doSomething() again, and the child doSomething() in the recursion tree, redeclares x and y, will it reset the parents' x and y variables as well

No.

is it creating an entirely new x and y variables that is valid for that stage in the recursion tree only?

Yes.

Edit 1:
This example should be helpful.

#include <iostream>

void foo( int temp )
{
     int num = temp;

     if( temp == 0)
          return; 

     foo(temp-1) ;

     std::cout << &num << "\t" << num << "\n" ;
}

int main()
{
     foo(5) ;
     return 0;
}

Output:

0xbfa4e2d0 1
0xbfa4e300 2
0xbfa4e330 3
0xbfa4e360 4
0xbfa4e390 5

Notice the address of num being different and each call has it's own value of num.
Ideone

翻身的咸鱼 2024-11-11 14:52:36

每个调用都会获得自己的变量副本。在一个函数调用中分配给副本不会影响任何其他函数调用中的版本。这就是为什么递归必须通过传递参数和返回值来在“阶段”之间进行通信。

Every call gets its own copy of the variables. Assigning to the copy in one function call has no effect on the versions in any other function call. That's why the recursion has to communicate between "stages" by passing arguments and returning a value.

尸血腥色 2024-11-11 14:52:36

是的,x 和 y 是堆栈变量,因此在每次调用之间都是独立的。每次调用 doSomething 都会创建一个新的、基于堆栈的 x 和 y。

如果您希望它们在每次调用中都是相同的变量,则应该将它们声明为static

yes, x and y are stack variables, and thus are independent between each call. A fresh, stack-based x and y will be created for each call to doSomething.

If you wanted them to be the same variable in each call, you should declare them static

Saygoodbye 2024-11-11 14:52:36

每次调用函数时,都会创建新的局部变量xy,它们与该函数的任何其他调用无关。这就是局部变量与全局变量的不同之处。

Every time a function is invoked, there are new local variables x and y created, that are unrelated to any other invocation of the function. This is what makes local variables different from global variables.

闻呓 2024-11-11 14:52:36

您将 x + y 按值传递到函数 doSomething() 中。这意味着在函数堆栈上, doSomething() 将有权访问一个局部变量 j ,其值从堆栈上位于其下方的函数设置为任何 x + y 计算结果(即调用它们的函数)。由于该变量与父函数中的变量完全分开,因此修改其值不会影响堆栈中位于其下方的变量。

但是,如果您希望 j 与父函数中的变量完全相同,那么您可以执行以下操作

void doSomething( double& j )
{ 
  double x = j - 1;
  double y = j - 2; 

  double willChange = x + y;
  doSomething( willChange );
  x = j + 31;
  y = j + 12 ;
}

:在 double 之后,这告诉编译器该函数通过其地址接受 double 的值,这通过地址调用是适当的。这里,在每个 doSomething() j 的子级中,j 是堆栈上位于其下方的函数中的变量 willChange 的别名。

如果你想专门修改 x 和 y 那么也许可以做类似的事情

void doSomething( double& x, double& y )
{ 
  double j = x + y
  double x = j - 1;
  double y = j - 2; 

  doSomething( x, y );
  x = j + 31;
  y = j + 12 ;
}

You are passing x + y by value into the function doSomething(). This means that on the function stack doSomething() will have access to one local variable j with its value set from the function below it on the stack to whatever x + y evaluates to (i.e the function that called them). Since this variable is completely separate from that in the parent function, modifying its value will have no affect on variables below it on the stack.

If, however, you want j to be the exact same variable as in the parent function then you can do something like:

void doSomething( double& j )
{ 
  double x = j - 1;
  double y = j - 2; 

  double willChange = x + y;
  doSomething( willChange );
  x = j + 31;
  y = j + 12 ;
}

Notice the & after the double, this tells the compiler that the function accepts the value of a double by its address, which is called appropriately enough passing by address. Here in the child of every doSomething() j is an alias for the variable willChange in the function below it on the stack.

If you want to modify x and y specifically then maybe do something like

void doSomething( double& x, double& y )
{ 
  double j = x + y
  double x = j - 1;
  double y = j - 2; 

  doSomething( x, y );
  x = j + 31;
  y = j + 12 ;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文