关于 c++ 的问题递归和局部变量
假设我有这个递归:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
是的。
?
是的。
编辑1:
这个例子应该会有帮助。
输出:
请注意
num
的地址不同,并且每次调用都有其自己的num
值。Ideone
Yes.
No.
Yes.
Edit 1:
This example should be helpful.
Output:
Notice the address of
num
being different and each call has it's own value ofnum
.Ideone
每个调用都会获得自己的变量副本。在一个函数调用中分配给副本不会影响任何其他函数调用中的版本。这就是为什么递归必须通过传递参数和返回值来在“阶段”之间进行通信。
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.
是的,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
每次调用函数时,都会创建新的局部变量
x
和y
,它们与该函数的任何其他调用无关。这就是局部变量与全局变量的不同之处。Every time a function is invoked, there are new local variables
x
andy
created, that are unrelated to any other invocation of the function. This is what makes local variables different from global variables.您将 x + y 按值传递到函数 doSomething() 中。这意味着在函数堆栈上, doSomething() 将有权访问一个局部变量 j ,其值从堆栈上位于其下方的函数设置为任何 x + y 计算结果(即调用它们的函数)。由于该变量与父函数中的变量完全分开,因此修改其值不会影响堆栈中位于其下方的变量。
但是,如果您希望 j 与父函数中的变量完全相同,那么您可以执行以下操作
:在 double 之后,这告诉编译器该函数通过其地址接受 double 的值,这通过地址调用是适当的。这里,在每个 doSomething() j 的子级中,j 是堆栈上位于其下方的函数中的变量 willChange 的别名。
如果你想专门修改 x 和 y 那么也许可以做类似的事情
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:
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