Why is C ++ static variable initialized only once?

Source: Internet
Author: User

The following content is discussed in the Forum and summarized here:

The first question is from this function:

[Cpp]
Int fun ()
{
Static int I = 0;
I ++;
Return I;
}

When this function is called repeatedly, the I value is always added, that is, the static variable is initialized only once.

I am confused about this. Do you have any questions.
When changed:
[Cpp]
Int fun ()
{
Int I = 0;
I ++;
Return I;
}

1 is returned during repeated calls. In this example, although the variable I has the same name, the address is different. This is a new variable, because after the scope, the original variable is destroyed. create a new variable.
Here we know one thing: static variables are global, and the lifecycle of the program is the same.
Then the I of the original staic still exists, but its accessible region can only be a defined scope, but not necessarily a global access. This is a difference between global variables.
Static variables must be initialized. If Initialization is not displayed, the initialization is 0, '0', or other.
Initialization starts when the program is loaded for the first time. The corresponding flag is set!

So when the variables are repeated, the compiler ignores the code for repeated initialization. Here, even C ++ can only initialize the code once!

For more information, see:
When a program is loaded, the operating system allocates memory space for the program based on the declared part of the program (this part of the data is generated by the compiler ). The memory space occupied by the program is divided into two parts: static and dynamic regions (as to why this distinction involves hardware knowledge, we recommend that LZ not study it further ): dynamic regions are used to store frequently changing data (dynamic regions are divided into two major parts: stacks and stacks. You should be familiar with these two parts ); static areas (called permanent areas in Java) are used to store data that will not change frequently, such as the Code of the program's commands (C/C ++ contains the Code Compiled by each function), user type (struct, class) declaration code, global variables, static variables ...... PS: In some examples, static variables are used as recursive counters to illustrate the characteristics of static variables. In fact, this method is not recommended because it will have a slight impact on program performance. Data in the static area is initialized when the program is loaded, and the lifetime is the full time for the program to run. In addition, a misunderstanding of LZ is corrected: any variable is initialized only once. The lifetime of a local variable ends at the end of the block. When you call this block again, the other variable is declared in principle (the allocated address is not necessarily the same ). PS: the actual memory partitions may be different for different compilers. For example, in the Small mode of TC, the heap and stack areas overlap, while in the Tiny mode, the static and dynamic areas overlap.


Author: xiaowall

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.