(C beginner alert)
(C初学者警报)
Wikipedia define a static variable as, ".... a variable that has been allocated statically—whose lifetime or "extent" extends across the entire run of the program. "
*将静态变量定义为“......已经静态分配的变量 - 其生命周期或”范围“在整个程序运行中延伸。”
Then it goes on to give an example in C:
然后它继续在C中给出一个例子:
#include <stdio.h>
void func() {
static int x = 0;
/* x is initialized only once across four calls of func() and
the variable will get incremented four
times after these calls. The final value of x will be 4. */
x++;
printf("%d\n", x); // outputs the value of x
}
int main() { //int argc, char *argv[] inside the main is optional in the particular program
func(); // prints 1
func(); // prints 2
func(); // prints 3
func(); // prints 4
return 0;
}
Here's my issue: variable x
, when defined as static
, doesn't hold its value for the entire run of the program. In fact, it does quite the opposite, namely, for any subsequent call of func()
, it has the value it was assigned in the previous calling. It's only when I remove the static
keyword that x
retains its value of 0 no matter how many times func()
is called.
这是我的问题:变量x,当定义为静态时,不保持其整个程序运行的值。事实上,它完全相反,即对于func()的任何后续调用,它具有在前一次调用中分配的值。只有在我删除静态关键字时,无论调用func()多少次,x都会保留其值0。
So:
1) Is Wikipedia's explanation inaccurate/misleading, and if so, how would you better explain the nature of a static variable?
1)*的解释是否不准确/误导,如果是这样,您如何更好地解释静态变量的性质?
2) What is actually happening under the hood on the second and subsequent calls of func() such that the initialization of x to 0 is effectivey being ignored?
2)在func()的第二次和后续调用下实际发生了什么,以便x的初始化为0有效被忽略?
3 个解决方案
#1
3
You simply misunderstand the quoted explanation. What it means to say that a variable's "lifetime" extends to the whole program is that it is allocated in memory once, initialized once, and any changes made to it are reflected at the same memory location--that memory is allocated for the life of the program. Subsequent calls to the function will find the contents of that memory (the value of x) as they left it. They are still allowed to change the value, but it will always be the same piece of memory.
你只是误解了引用的解释。说变量的“生命周期”扩展到整个程序意味着它在内存中分配一次,初始化一次,对它做出的任何更改都反映在同一个内存位置 - 内存分配给生命该计划。对函数的后续调用将在它们离开时找到该内存的内容(x的值)。它们仍然允许更改值,但它始终是相同的内存。
When you remove the "static", you're telling the compiler to allocate a new variable "x" each time the function is called, assign it 0, and discard it when the function returns. On the next call, an entirely different variable X will be created, assigned 0 again, and discarded again. This variable's "lifetime" is thus only inside the function.
当您删除“静态”时,您告诉编译器每次调用该函数时都会分配一个新变量“x”,将其赋值为0,并在函数返回时将其丢弃。在下一次调用时,将创建一个完全不同的变量X,再次分配0,然后再次丢弃。因此,该变量的“生命周期”仅在函数内部。
This is an entirely separate concept from the variable's "scope", which is who can see and change the variable: it's scope is still inside the function, even when static.
这是一个完全独立于变量“范围”的概念,它可以查看和更改变量:它的范围仍然在函数内部,即使是静态的。
#2
1
The local static variables are located in the .bss
section along with the global ones. So their initialization is effectively "taken out" of the function and moved to the same place where global statics are initialized. So, basically, declaring some variable as local and static in some function is only limiting it's scope to this function, but by other means it is not different from any other static variable.
局部静态变量与全局变量一起位于.bss部分。因此,它们的初始化实际上被“取出”了函数并移动到初始化全局静态的相同位置。因此,基本上,在某些函数中将某个变量声明为局部和静态只是将其范围限制为此函数,但通过其他方式它与任何其他静态变量没有区别。
#3
1
The Wikipedia entry is neither misleading nor incorrect; it says the variable's lifetime extends across the lifetime of the program (even before func
is called), which is exactly the case. A single instance of x
is created and initialized once at program startup and will persist until the program exits, retaining the last value written to it from each call to func
. Think of it as a "global" variable, but only visible from within func
.
*条目既不误导也不错;它说变量的生命周期在程序的生命周期内延伸(甚至在调用func之前),这正是如此。单个x实例在程序启动时创建并初始化一次,并将持续到程序退出,保留从每次调用func时写入的最后一个值。将其视为“全局”变量,但仅在func中可见。
If you remove the static
storage class qualifier, then x
only exists for the lifetime of func
; each call to func
creates a new instance of x
, which is initialized to 0.
如果删除静态存储类限定符,则x仅在func的生存期内存在;每次调用func都会创建一个x的新实例,该实例初始化为0。
#1
3
You simply misunderstand the quoted explanation. What it means to say that a variable's "lifetime" extends to the whole program is that it is allocated in memory once, initialized once, and any changes made to it are reflected at the same memory location--that memory is allocated for the life of the program. Subsequent calls to the function will find the contents of that memory (the value of x) as they left it. They are still allowed to change the value, but it will always be the same piece of memory.
你只是误解了引用的解释。说变量的“生命周期”扩展到整个程序意味着它在内存中分配一次,初始化一次,对它做出的任何更改都反映在同一个内存位置 - 内存分配给生命该计划。对函数的后续调用将在它们离开时找到该内存的内容(x的值)。它们仍然允许更改值,但它始终是相同的内存。
When you remove the "static", you're telling the compiler to allocate a new variable "x" each time the function is called, assign it 0, and discard it when the function returns. On the next call, an entirely different variable X will be created, assigned 0 again, and discarded again. This variable's "lifetime" is thus only inside the function.
当您删除“静态”时,您告诉编译器每次调用该函数时都会分配一个新变量“x”,将其赋值为0,并在函数返回时将其丢弃。在下一次调用时,将创建一个完全不同的变量X,再次分配0,然后再次丢弃。因此,该变量的“生命周期”仅在函数内部。
This is an entirely separate concept from the variable's "scope", which is who can see and change the variable: it's scope is still inside the function, even when static.
这是一个完全独立于变量“范围”的概念,它可以查看和更改变量:它的范围仍然在函数内部,即使是静态的。
#2
1
The local static variables are located in the .bss
section along with the global ones. So their initialization is effectively "taken out" of the function and moved to the same place where global statics are initialized. So, basically, declaring some variable as local and static in some function is only limiting it's scope to this function, but by other means it is not different from any other static variable.
局部静态变量与全局变量一起位于.bss部分。因此,它们的初始化实际上被“取出”了函数并移动到初始化全局静态的相同位置。因此,基本上,在某些函数中将某个变量声明为局部和静态只是将其范围限制为此函数,但通过其他方式它与任何其他静态变量没有区别。
#3
1
The Wikipedia entry is neither misleading nor incorrect; it says the variable's lifetime extends across the lifetime of the program (even before func
is called), which is exactly the case. A single instance of x
is created and initialized once at program startup and will persist until the program exits, retaining the last value written to it from each call to func
. Think of it as a "global" variable, but only visible from within func
.
*条目既不误导也不错;它说变量的生命周期在程序的生命周期内延伸(甚至在调用func之前),这正是如此。单个x实例在程序启动时创建并初始化一次,并将持续到程序退出,保留从每次调用func时写入的最后一个值。将其视为“全局”变量,但仅在func中可见。
If you remove the static
storage class qualifier, then x
only exists for the lifetime of func
; each call to func
creates a new instance of x
, which is initialized to 0.
如果删除静态存储类限定符,则x仅在func的生存期内存在;每次调用func都会创建一个x的新实例,该实例初始化为0。