Following is the code, that I took as reference to understand how a sub scope (or) dummy scope (just {}
) present within the function, impacts the structure of the stack frame.
下面是代码,我将其作为参考来理解函数中的子作用域(或)虚拟作用域(仅为{})如何影响堆栈框架的结构。
#include <stdio.h>
int main()
{
int main_scope=0; /*Scope and life time of the variable is throughout main*/
{
//From below two statements I assume that there
//is no seperate "stack frame" created for just
//braces {}.So we are free access (scope exists) and
//modify (lifetime exists) the variable "main_scope"
//anywhere within main
main_scope++;
printf("main_scope++:(%d)\n",main_scope);
//I expected this statement to throw an error saying
//"Multiple definition for "main_scope".But it isn't????
int main_scope=2;
printf("Value of redefined main_scope:(%d)\n",main_scope);
}
printf("Finally main_scope in %s:(%d)\n",__FUNCTION__,main_scope);
return 0;
}
Sample output
main_scope++:(1)
Value of redefined main_scope:(2)
Finally main_scope in main:(1)
Based on the above behavior, I presume the following.
基于以上的行为,我假设如下。
- There is no stack frame creation for the scope
{}
. - 没有为范围{}创建堆栈框架。
- By this way the
auto
variables, declared/defined inmain
and those within the sub scope{}
share the same stack frame. - 通过这种方式,在main中声明/定义的自动变量和在子作用域{}中的自动变量共享相同的堆栈框架。
- So the variables declared/defined in
main
are free to be accessed anywhere within the function (even within the sub scope). - 因此,在main中声明/定义的变量可以在函数中的任何位置(甚至在子范围内)*访问。
- On the other hand the variables declared/defined in the sub scope loses its scope out of the block.But its life time is valid as long as stack frame is present.
- 另一方面,子作用域中声明的/定义的变量在块之外失去作用域。但是只要堆栈帧存在,它的生命周期是有效的。
Question: If the above points are right, then why isn't the code failing when giving multiple definitions of the same variable, one within main
and the other within {}
.
问题:如果上面的点是对的,那么为什么在给出相同变量的多个定义时,代码不出错呢,一个在main中,另一个在{}中。
2 个解决方案
#1
2
The hardware stack is irrelevant here. It can grow only once for all local variables at function entry and shrink only once at function exit or it can grow and shrink every time a new local variable is defined and destroyed when the enclosing {} for it is left.
硬件堆栈在这里无关紧要。对于函数入口的所有局部变量,它只能增长一次,在函数出口只能收缩一次,或者在每次定义并销毁一个新的局部变量时,它可以增长和收缩。
What's relevant is the "visibility" of variables.
相关的是变量的“可见性”。
int main_scope=0;
{
main_scope++;
printf("main_scope++:(%d)\n",main_scope);
// the main_scope variable that was assigned 0 is the most recent
// visible declaration of main_scope.
int main_scope=2;
// now, the previous declaration of main_scope is obscured by the new one,
// and so, you're going to access the new one
printf("Value of redefined main_scope:(%d)\n",main_scope);
}
printf("Finally main_scope in %s:(%d)\n",__FUNCTION__,main_scope);
// the previous scope inside {} is left, so, main_scope is going to be
// the one, to which we assigned 0
It is perfectly legal to define an object in an inner/sub-scope with the same name as in an outer/super-scope. The latter will be obscured for the duration of the {} scope.
在内部/子范围内与外部/超范围内的相同名称定义对象是完全合法的。后者将在{}范围的持续时间内被隐藏。
Just for the reference, there are some other places, where variable definitions can occur, e.g. inside the first expression of the for(;;)
statement: for (int i = 0; i < 10; i++) ...
. That variable i
will only be visible inside the for's body, and you can hide it there as well by defining another i
.
仅供参考,还有一些其他的地方,在其中可以发生变量定义,例如在for(;;)的第一个表达式中;我< 10;我+ +)....这个变量i只在for的主体中可见,你也可以通过定义另一个i来隐藏它。
#2
1
Local variable hides the outer variable main_scope
.
局部变量隐藏外部变量main_scope。
int main()
{
int i=1;
{
int i=2;
printf("%d", i);
/* Whenever you use i here, it's always the local one that's used.
The outer i is hidden by the local i. But the local i will be deallocated
once the scope exits.*/
}
}
is perfectly legal in C. (Note that this is illegal in C++!)
在C语言中是完全合法的(注意,这在c++中是非法的!)
In your example, certainly a stack frame is created for inner main_scope
, but it'll be deallocated once program goes out that inner scope.
在您的示例中,肯定为内部main_scope创建了堆栈框架,但一旦程序超出该内部范围,它将被释放。
#1
2
The hardware stack is irrelevant here. It can grow only once for all local variables at function entry and shrink only once at function exit or it can grow and shrink every time a new local variable is defined and destroyed when the enclosing {} for it is left.
硬件堆栈在这里无关紧要。对于函数入口的所有局部变量,它只能增长一次,在函数出口只能收缩一次,或者在每次定义并销毁一个新的局部变量时,它可以增长和收缩。
What's relevant is the "visibility" of variables.
相关的是变量的“可见性”。
int main_scope=0;
{
main_scope++;
printf("main_scope++:(%d)\n",main_scope);
// the main_scope variable that was assigned 0 is the most recent
// visible declaration of main_scope.
int main_scope=2;
// now, the previous declaration of main_scope is obscured by the new one,
// and so, you're going to access the new one
printf("Value of redefined main_scope:(%d)\n",main_scope);
}
printf("Finally main_scope in %s:(%d)\n",__FUNCTION__,main_scope);
// the previous scope inside {} is left, so, main_scope is going to be
// the one, to which we assigned 0
It is perfectly legal to define an object in an inner/sub-scope with the same name as in an outer/super-scope. The latter will be obscured for the duration of the {} scope.
在内部/子范围内与外部/超范围内的相同名称定义对象是完全合法的。后者将在{}范围的持续时间内被隐藏。
Just for the reference, there are some other places, where variable definitions can occur, e.g. inside the first expression of the for(;;)
statement: for (int i = 0; i < 10; i++) ...
. That variable i
will only be visible inside the for's body, and you can hide it there as well by defining another i
.
仅供参考,还有一些其他的地方,在其中可以发生变量定义,例如在for(;;)的第一个表达式中;我< 10;我+ +)....这个变量i只在for的主体中可见,你也可以通过定义另一个i来隐藏它。
#2
1
Local variable hides the outer variable main_scope
.
局部变量隐藏外部变量main_scope。
int main()
{
int i=1;
{
int i=2;
printf("%d", i);
/* Whenever you use i here, it's always the local one that's used.
The outer i is hidden by the local i. But the local i will be deallocated
once the scope exits.*/
}
}
is perfectly legal in C. (Note that this is illegal in C++!)
在C语言中是完全合法的(注意,这在c++中是非法的!)
In your example, certainly a stack frame is created for inner main_scope
, but it'll be deallocated once program goes out that inner scope.
在您的示例中,肯定为内部main_scope创建了堆栈框架,但一旦程序超出该内部范围,它将被释放。