C + + static keyword and variable storage location summary _c language

Source: Internet
Author: User
Today, read Bowen, see the C + + static keyword Some summary, but also involved in some of the code storage location, in order to have time to see, or to take it to the excerpt down.

There are two uses for the static of C + +: Static in process programming and static in object oriented programming. The former is applied to ordinary variables and functions and does not involve classes; the latter mainly describes the role of static in class.

static in process-oriented design
1. Static Global Variables
The variable is defined as a static global variable before the global variable, plus the keyword static. Let's first give an example of a static global variable, as follows:
Copy Code code as follows:

Example 1
#include <iostream.h>
VOID Fn ();
static int n; Defining static global Variables
void Main ()
{
n = 20;
cout<< N <<endl;
FN ();
}

VOID Fn ()
{
n++;
cout<< N <<endl;
}

Static global variables have the following characteristics
The variable allocates memory in the global data area;
Uninitialized static global variables are automatically initialized by the program to 0 (the value of the automatic variable is random unless it is explicitly initialized);
Static global variables are visible throughout the file in which they are declared, but not outside of the file;
Static variables are allocated memory in the global data area, including the static local variables to be mentioned later. For a complete program, in the memory of the

code area, global data area, heap area, stack area
The dynamic data generated by new in general program is stored in the heap area, and the automatic variables inside the function are stored in the stack area. Automatic variables typically release 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 of the exit of the function. Attentive readers may find that the code in Example 1 will
static int n; Defining static global Variables
To
int n; Defining Global Variables
The program still works.
Indeed, defining a global variable enables you to share variables in a file, but there are also benefits to defining static global variables:
Static global variables cannot be used by other files;
A variable with the same name can be defined in other files, and no conflict occurs;
You can change the example code above to read:
Copy Code code as follows:

Example 2
File1
#include <iostream.h>
VOID Fn ();
static int n; Defining static global Variables
void Main ()
{
n=20;
cout<<n<<endl;
FN ();
}

File2
#include <iostream.h>
extern int n;
VOID Fn ()
{
n++;
cout<<n<<endl;
}

When you compile and run Example 2, you will find that the code above can be compiled separately, but there are errors at run time. Try to
static int n; Defining static global Variables
To
int n; Defining Global Variables
Compile the program again to appreciate the difference between global variables and static global variables (verify sharing and protection relationships).
2. Static local Variables
Before a local variable, plus the keyword static, the variable is defined as a static local variable.
Let's first give an example of a static local variable, as follows:
Copy Code code 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 the function that allocates stack memory to the local variable whenever the program runs to that statement. However, as the program exits the function body, the system will reclaim the stack memory and the local variables should be invalidated accordingly.
But sometimes we need to save the value of a variable between two calls. The usual idea is to define a global variable to implement. But in this way, the variable is no longer the function itself, is no longer only controlled by the function, to the maintenance of the program inconvenience.
Static local variables can just solve the problem. Static local variables are saved in the global data area, not on the stack, and the value is persisted to the next call until the next time the new value is assigned.
Static local variables have the following characteristics:
The variable allocates memory in the global data area;
A static local variable is initialized for the first time when the program executes to the declaration of the object, that is, subsequent function calls are no longer initialized;
Static local variables are typically initialized at the declaration and, if not explicitly initialized, are automatically initialized to 0 by the program;
It always resides in the global data area until the program has finished running. But its scope is a local scope, and when the function or statement block that defines it ends, its scope ends;
3. Static function
By adding the static keyword before the return type of the function, the function is defined as a static function. A static function, unlike a normal function, can only be visible in the file in which it is declared and cannot be used by other files.
examples of static functions
Copy Code code as follows:

Example 4
#include <iostream.h>
static void Fn ();
void Main ()
{
FN ();
}

VOID FN ()//define static functions
{
int n=10;
cout<<n<<endl;
}

the benefits of defining static functions
Static functions cannot be used by other files;
A function with the same name can be defined in other files, and no conflict occurs;

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.