static

Excerpted from: https://blog.csdn.net/Kendiv/article/details/675941

""Memory"" about static

We can use it as button detection,,, because the button must have a defibrillation and debounce process

Example program one
#include <iostream>

using namespace std;

void staticLocalVar()
{
 static int a = 0; // Initialize once during runtime, and do not initialize when called next time
 cout<<"a="<<a<<endl;
 ++a;
}

int main()
{
 staticLocalVar(); // first call, output a=0
 staticLocalVar(); // second call, remember the value of the first exit, output a=1
 return 0;
}

 

 

1. "Memory", a very important point in program operation is repeatability, and the "memory" of static variables destroys this repeatability, resulting in different results from different times to running.
2. "Lifetime" Globality and uniqueness. The storage space of ordinary local variables is allocated on the stack, so each time a function is called, the allocated space may be different, and static has the characteristics of global uniqueness, and each time it is called, it points to the same A piece of memory, which creates a very important problem - non-reentrancy!!!
So in multi-threaded programming or recursive programming, special attention should be paid to this problem.

 

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324993228&siteId=291194637