C++ 动态和静态范围程序差异
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19461503/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Dynamic and static Scoping program differences
提问by user1789951
int x;
int main() {
x = 14;
f();
g();
}
void f() {
int x = 13;
h();
}
void g() {
int x = 12;
h();
}
void h() {
printf("%d\n",x);
}
If static scoping is used, what is the result? If dynamic scoping is used, what is the result?
如果使用静态范围,结果是什么?如果使用动态范围,结果是什么?
Now if I understand scoping right, the difference between static and dynamic scoping is that static makes variables local to a class. So the value x
would be local to void f()
, void g()
and int main ()
and dynamic would make them globally available. I'm just not sure how to apply it to this code. If static scoping was used would it only print the last value (12 from void g()
) and dynamic scoping would be using all the values of x
?
现在,如果我正确理解范围,静态和动态范围之间的区别在于静态使变量成为类的局部变量。所以值x
将是本地的void f()
,void g()
并且int main ()
和动态会使他们全局可用。我只是不确定如何将它应用于此代码。如果使用静态范围,它只会打印最后一个值(来自 12 void g()
),而动态范围将使用x
?
I'm a little confused on how scoping actually works. I know C
uses static scoping though.
我对范围界定的实际工作方式有些困惑。我知道C
使用静态范围。
回答by Idan Arye
Static scoping means that x
refers to the x
declared innermost scope of declarationthat has one. Since h
is declared inside the global scope, the innermost x
is the one in the global scope(it has no access to the x
s in f
and g
, since it was not declared inside them), so the program prints 14
twice.
静态作用域意味着x
指的是x
声明的最里面的声明范围,它有一个。由于h
是在全局作用域内声明的,最里面的x
是全局作用域中的那个(它无法访问and 中的x
s ,因为它没有在它们内部声明),所以程序打印了两次。f
g
14
Dynamic scoping means that x
refers to the x
declared in the most recent frame of the call-stackthe has one. If C used dynamic scoping, h
would use the x
from either f
or g
- whichever one that called it - so the program would print 13
and 12
.
动态范围意味着x
指的是在调用堆栈的x
最近一帧中声明的有一个。如果 C 使用动态范围,h
将使用x
fromf
或g
- 无论哪个调用它 - 所以程序将打印13
和12
.
回答by Jason Enochs
C/C++ doesn't use Dynamic scoping. Your programming language will use one or the other, you don't get to choose (Unless you are using Clojure! according to Idan Arye below).
C/C++ 不使用动态范围。您的编程语言将使用一种或另一种,您无法选择(除非您使用 Clojure!根据下面的 Idan Arye)。
Here is a great explanation/comparison with a good example: http://msujaws.wordpress.com/2011/05/03/static-vs-dynamic-scoping/
这是一个很好的解释/比较和一个很好的例子:http: //msujaws.wordpress.com/2011/05/03/static-vs-dynamic-scoping/
回答by DataCruncher
Consider the following code snippet
考虑以下代码片段
int x=10;
Here the value 10 is stored in a particular location in memory and the name 'x' is bound to that location.
Scope is the part of the program where this Bindingis valid. In simple words, the part of program where the variable is visible.
The "part of the program" may refer to a particular portion of code (Static scope)or portion of runtime (Dynamic scope), where this binding is valid.
In this case
这里值 10 存储在内存中的特定位置,名称“x”绑定到该位置。
范围是程序中此绑定有效的部分。简单来说,程序中变量可见的部分。
“程序的一部分”可以指代码的特定部分(静态范围)或运行时部分(动态范围),其中此绑定有效。
在这种情况下
int x; //X1
int main() {
x = 14;
f();
g();
}
void f() {
int x = 13; //X2
h();
}
void g() {
int x = 12; //X3
h();
}
void h() {
printf("%d\n",x);
}
X1 can be accessed anywhere in the program. So in main() X1 is assigned the value 14.
When f() is called, a new variable(stored in a new memory location) 'x'(X2) local to f()is created and initialized the value 13 and when g() is invoked,similarly another new variable 'x'(X3) local to g()with value 12 is created.
In Static scoping
when f()or g() calls h(), X2 and X3 are not visible outside f() and g() respectively(portion of code),while X1 is globally visible so the output will be
X1 可以在程序的任何地方访问。所以在 main() 中 X1 被赋值为 14。
当 f() 被调用时,一个新的变量(存储在一个新的内存位置)f() 本地的 'x'(X2)被创建并初始化值 13,当g() 被调用,类似地,另一个新变量'x'(X3) 本地到 g()的值为 12 被创建。
在静态作用域中,
当 f() 或 g() 调用 h() 时,X2 和 X3 分别在 f() 和 g() 之外不可见(部分代码),而 X1 是全局可见的,因此输出将是
14
14
In Dynamic scopingWhen f() executes the name 'x' is binded to X2 and when f() calls h(),the function f() is still executing and the name 'x' is still binded to X2 so the local X2 will be printed to the output. After f() is executed,this binding does not exist(portion of runtime) and 'x' is binded to X1. And when g() executes , the name 'x' is binded to X3.And when h() is invoked, f() is still running and the name 'x' is still binded to X3 so the local X3 will be printed and the output will be
在动态作用域中,当 f() 执行名称 'x' 绑定到 X2 并且当 f() 调用 h() 时,函数 f() 仍在执行并且名称 'x' 仍然绑定到 X2,因此本地 X2将打印到输出。执行 f() 后,此绑定不存在(运行时的一部分)并且 'x' 绑定到 X1。当 g() 执行时,名称 'x' 绑定到 X3。当 h() 被调用时,f() 仍在运行并且名称 'x' 仍然绑定到 X3,因此将打印本地 X3 并输出将是
13
12
回答by Scott Hunter
In static scoping, the scope of an indentifier is determined by its location in the code, and since that doesn't change, the scope doesn't either. In dynamic scoping, the scope is determined by the sequence of calls that has led to the use of an identifier, and since that can be different each time that use is reached, is dynamic.
在静态作用域中,标识符的作用域是由它在代码中的位置决定的,因为它不会改变,所以作用域也不会改变。在动态作用域中,作用域由导致使用标识符的调用序列确定,并且由于每次达到使用时可能不同,因此是动态的。
In your example, under static scoping, the x in h will always be the global x, regardless of how h came to be called. But with dynamic scoping, it would refer to the x declared in f or the one declared in g, depending on which had called h that particular time.
在您的示例中,在静态作用域下, h 中的 x 将始终是全局 x,无论 h 是如何被调用的。但是对于动态范围,它会引用在 f 中声明的 x 或在 g 中声明的那个,这取决于哪个特定时间调用了 h。
回答by Naveen Kuntla
This is the exactly proven ans in
这是完全证明的答案
static scoping: Once the global variable is assigned a value it's scope will exist throughout the program execution.
Hence the answer for this is : 14
and 14
.as C-language posses static scoping u can check it in TURBO-C compiler.
静态范围:一旦全局变量被赋值,它的范围将存在于整个程序执行过程中。因此,答案是:14
并且14
.as C 语言拥有静态范围,您可以在 TURBO-C 编译器中检查它。
Where as when u come to
当你来的时候
Dynamic scoping: Irrespective of the value of global variable .The value assigned in the block is considered.
therefore the Answer is : f()=13
and g()=12
动态范围:不考虑全局变量的值。考虑块中分配的值。因此答案是:f()=13
和g()=12
回答by rashedcs
Static scope :Static scope refers to the scope of variable that is defined at compile time. It is always refers to the variable with top level environment.
In this program, the result of static scope is 14 and 14 because f() and g() always return the value of global variable x. f() and g() not dependent on who are calling them.
静态范围:静态范围是指在编译时定义的变量范围。它总是指具有顶级环境的变量。
在这个程序中,静态作用域的结果是 14 和 14,因为 f() 和 g() 总是返回全局变量 x 的值。f() 和 g() 不依赖于谁在调用它们。
Dynamic Scope :Dynamic scope refers to scope of a variable that is defined at run time. It refers to the identifier with the most recent environment. It is something like dynamic programming because in dp the value is updated.
In this program, the result of dynamic scope are 13 and 12 because f() and g() return current variable x value not the global one.
动态范围:动态范围是指在运行时定义的变量的范围。它是指具有最新环境的标识符。它类似于动态规划,因为在 dp 中该值已更新。
在这个程序中,动态范围的结果是 13 和 12,因为 f() 和 g() 返回当前变量 x 值而不是全局值。
Reference : GeeksforGeeks