Static local variable (process-oriented static keyword)

Source: Internet
Author: User

For a complete program, the distribution in memory is as follows:

The dynamic data generated by new in the general program is stored in the heap area, and the automatic variables inside the function are stored in the stack area. Automatic variables generally free up space with the function's exit, and static data (even static local variables inside the function) is stored in the global data area. The data in the global data area does not free up space because of the function's exit.

Static local Variables

Before a local variable, the variable is defined as a static local variable, plus the keyword static.

Let's first give an example of a static local variable, as follows:

  

1#include <iostream>2 using namespacestd;3 4 voidfn ();5 6 intMain ()7 {8 fn ();9 fn ();Ten fn (); One       return 0;  A }     -  - voidfn () the { -       Static intn =Ten; -cout<<n<<Endl; -n++;  +}

A variable is usually defined within the function body and allocates stack memory to the local variable whenever the program runs to the statement. However, as the program exits the function body, the system will retract the stack memory, and the local variables are invalidated accordingly.

Sometimes we need to save the value of a variable between two calls. The usual practice is to define a global variable to implement. In this way, the variables are no longer part of the function itself, and are no longer only controlled by the functions, which inconvenience the maintenance of the program.

Static local variables can solve this problem. Static local variables are saved in the global data area instead of saved in the stack, and each time the value is saved to the next call until the next assignment.

Static local variables have the following characteristics:

(1) The variable allocates memory in the global data area;

(2) A static local variable is first initialized when the program executes to the declaration of the object, that is, the subsequent function call is no longer initialized;

(3) Static local variables are generally initialized at the declaration, if not display initialization, the program will be automatically initialized to 0;

(4) It always stops at the global data area until the program finishes running. But its scope is local scope, when the function or statement block that defines it ends, its scope ends;

Static local variable (process-oriented static keyword)

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.