C++ static keyword and variable storage location summary

  • 2020-04-01 21:25:49
  • OfStack

When I read the blog today, I saw a summary of the static keyword of c++, and also mentioned the storage location of some code. In order to have time to look at it, I'd better copy it down.

There are two USES of static in C++ : static in procedural programming and static in object-oriented programming. The former applies to ordinary variables and functions, not to classes; The latter mainly explains the role of static in a class.

Static in process oriented design
1. Static global variables
A global variable is defined as a static global variable by adding the keyword static in front of it. Let's first take an example of a static global variable, as follows:
 
//Example 1 
#include <iostream.h> 
void fn(); 
static int n; //Define static global variables
void main() 
{ 
n = 20; 
cout<< n <<endl; 
fn(); 
} 

void fn() 
{ 
n++; 
cout<< n <<endl; 
} 

Static global variables have the following characteristics :
This variable allocates memory in the global data area;
An uninitialized static global variable is automatically initialized to 0 (the value of the automatic variable is random unless it is explicitly initialized).
A static global variable is declared to be visible to its entire file and not to be visible outside the file.
Static variables allocate memory in the global data area, including the static local variables mentioned later. For a complete program, in memory

Code area, global data area, heap area, stack area
The dynamic data generated by new for general programs is stored in the heap, and the automatic variables inside the function are stored in the stack. Automatic variables typically free up space as the function exits, and static data (even static local variables within the function) is stored in the global data area. The data in the global data area does not free up space because the function exits. Careful readers may notice that the code in Example 1
Static int n; // define static global variables
Instead of
Int n; // define global variables
The program still works fine.
Sure, defining global variables allows you to share variables in files, but defining static global variables has the following benefits:
Static global variables cannot be used by other files;
Variables with the same name can be defined in other files without conflicts.
You can change the above sample code to look like this:
 
//Example 2 
//File1 
#include <iostream.h> 
void fn(); 
static int n; //Define static global variables
void main() 
{ 
n=20; 
cout<<n<<endl; 
fn(); 
} 

//File2 
#include <iostream.h> 
extern int n; 
void fn() 
{ 
n++; 
cout<<n<<endl; 
} 

Compile and run Example 2, and you will see that the above code can be compiled separately, but there is an error at run time. Try to
Static int n; // define static global variables
Instead of
Int n; // define global variables
Compile and run the program again, paying attention to the difference between global variables and static global variables (verifying Shared and protected relationships).
2. Static local variables
Before a local variable, add the keyword static, and the variable is defined as a static local variable.
Let's first take an example of a static local variable, as follows:
 
//Example 3 
#include <iostream.h> 
void fn(); 
void main() 
{ 
fn(); 
fn(); 
fn(); 
} 

void fn() 
{ 
static n=10; 
cout<<n<<endl; 
n++; 
} 

Typically, a variable is defined in the body of a function, and stack memory is allocated to that local variable whenever the program runs to that statement. However, as the program exits the function body, the system will withdraw the stack memory and local variables will be invalidated accordingly.
But sometimes we need to save the value of the variable between calls. The general idea is to define a global variable to implement. But as a result, variables no longer belong to the function itself, and are no longer only controlled by the function, bringing inconvenience to the maintenance of the program.
Static local variables can solve this problem. Static local variables are stored in the global data area rather than on the stack, with each value held until the next call until the next assignment.
Static local variables have the following characteristics:
This variable allocates memory in the global data area;
Static local variables are initialized for the first time when the program executes to the declaration of the object, that is, subsequent function calls are not initialized;
Static local variables are generally initialized in the declaration, if not explicitly initialized, will be automatically initialized to 0 by the program;
It stays in the global data area until the program runs. But its scope is local scope, when the function or statement block that defines it ends, its scope ends;
3. Static functions
A function is defined as static by adding the static keyword to its return type. Unlike normal functions, static functions are visible only in the file in which they are declared and cannot be used by other files.
Examples of static functions :
 
//Example 4 
#include <iostream.h> 
static void fn();//Declare static functions
void main() 
{ 
fn(); 
} 

void fn()//Define a static function
{ 
int n=10; 
cout<<n<<endl; 
} 

The benefits of defining static functions :
Static functions cannot be used by other files;
Functions with the same name can be defined in other files without conflicts.

Related articles: