简体   繁体   English

关于静态变量的混淆

[英]confusion about static variables

I have a confusion in the concepts of static integer.When i initialize a static integer in main function ie 我对静态整数的概念感到困惑。当我在main函数中初始化一个静态整数时

static int i;

Now the static integer is assigned a value 0.Now in the next step: 现在,静态整数被赋值为0.现在下一步:

i++;

i becomes 1. 我变成了1。

Now the program terminates. 现在程序终止了。 I want to know what program will produce on its' next run. 我想知道在下一次运行中会产生什么程序。 Also, what would happen if the entire program is closed? 此外,如果整个程序关闭会发生什么? I understand that the first line is static int i; 我明白第一行是静态int i; thus the next value when the function is next run should retain the value of i when it was previously run. 因此,下次运行该函数时的下一个值应保留i先前运行时的值。 If so, what is the advantage of making a variable static? 如果是这样,变量静态的优势是什么? Does the variable have a time limit or can it be stored forever? 变量是否有时间限制,还是可以永久存储? What would the value be if I ran the function again? 如果我再次运行该功能会有什么价值?

In C, "static" means that the variable has local scope within global storage. 在C中,“static”表示变量在全局存储中具有局部范围。

The scope of variables in C is a block. C中的变量范围是一个块。 In other words variables can be used inside the block they are declared. 换句话说,变量可以在声明它们的块内使用。 And normally they just keep their values until the block ends, being lost after that. 通常他们只是保持他们的价值直到阻止结束,之后就会丢失。 Example: 例:

{
   int a; 
   // Only a can be used here
   {
      int b;
      // a and b can be used here
      {
         int c;
         // a,b and c can be used here
      }
      //just a and b can be used here. c is not available anymore
    }
    // only a can be used here. Neither b nor c are available anymore
 }

This is true except for global variables that can be used all along the program. 除了可以在整个程序中使用的全局变量之外,这是正确的。

The other exception is the static variable. 另一个例外是静态变量。 It is only seen inside the block but keeps its value after the block is over. 它只能在块内部看到,但在块结束后保持其值。

This means that if you declare a static variable inside a function, it will maintain its value between function calls. 这意味着如果在函数内部声明一个静态变量,它将在函数调用之间保持其值。

For example, the function below has a local variable. 例如,下面的函数有一个局部变量。 Local variables have scope of block (this means you can only access the variable 'var' inside the block {} it is declared, in the case below inside the function): 局部变量具有块的范围(这意味着您只能访问它声明的块{}内的变量'var',在函数内部的情况下):

void countFunction(void)
{
  int var = 0;
  var = var + 1;
  printf("Value is %d\n", var);
}

Once the variable is not static, every time you call the function it will print "Value is 1" because the variable is stored in the stack that is allocated on the function call and deallocated after the function returns. 一旦变量不是静态的,每次调用函数时它都会打印“Value is 1”,因为变量存储在函数调用中分配的堆栈中,并在函数返回后释放。

If you change var to be static, 如果将var更改为静态,

void countFunction(void)
{
  static int var = 0;
  var = var + 1;
  printf("Value is %d\n", var);
}

The first time you call the function var will be initialized as 0 and the function will show "Value is 1". 第一次调用函数var时,将初始化为0,函数将显示“Value is 1”。 Nevertheless, at the second time, var will be already allocated and at a global area. 然而,在第二次,var将已经分配到全球区域。 It will not be initialized again and the function will display "Value is 2". 它不会再次初始化,函数将显示“Value is 2”。

This within the program execution. 这在程序执行中。

Although a static variable is allocated as long as your program executes, it does not keep its value after your program finishes (the program will free all of its memory). 虽然只要程序执行就会分配静态变量,但是在程序完成后它不会保留其值(程序将释放所有内存)。 The only way to keep any value for the next run is to store it at a non-volatile media like the disk. 为下次运行保留任何值的唯一方法是将其存储在非易失性介质(如磁盘)中。

Hope it helps. 希望能帮助到你。

It will be 0 again because with termination of program all memory/variables are lost. 它将再次为0 ,因为程序终止时所有内存/变量都将丢失。

so what is the advantage of making it static? 那么让它静止的优势是什么?

It is useful as long as you do not terminate your program :) It can for example be used to count how many times a function is called if you use a static variable in that function and increment it with each call. 只要你不终止你的程序就很有用:)例如,如果在该函数中使用静态变量并在每次调用时递增它,它可以用来计算调用函数的次数。 This is just a simple example but there could be more practical uses of it of course. 这只是一个简单的例子,但当然可以有更实际的用途。

More Readings: 更多读物:

C Static Variables C静态变量

The value of i will be 0 when you start the application the next time. 下次启动应用程序时, i的值将为0。 The inialization to 0 is a default inialization (which is very uncommon in the rest of C) that happens at program startup. inialization为0是在程序启动时发生的默认的inialization(在C的其余部分非常罕见)。

How'd you imagine it would be stored anywhere between execution? 你怎么想象它会被存储在执行之间的任何地方? Whenever your application is terminated, it's memory will be reclaimed by the Operating System. 每当您的应用程序终止时,操作系统都会回收它的内存。 Therefore the memory block that stored i will be lost too. 因此,存储i的内存块也会丢失。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM