Automatic variables, persistence of static variables, region, and link

Source: Internet
Author: User

Disclaimer: This article may be reproduced without the consent of the author, but please keep the author and its source. Thank you.

Author: closeall

Here I want to write about the continuity, region, and link of variables. First, let's briefly introduce what these three attributes mean. Persistence

That is, the duration of the variable, that is, the time it exists in the memory. In short, the region refers to the scope of the variable, that is, the variable.

In which range is valid. Linking means whether this variable is allowed for external files. Let's talk about automatic variables first. What are automatic variables?

An automatic variable is a variable defined within a function. It is only allowed to be used within the defined function. Anywhere outside the Function

None. An automatic variable is a local variable, that is, its culture is valid within the defined function. Of course, this means there are no automatic variables.

It is connectionless because it does not allow other files to access it. Because automatic variables are invisible anywhere outside the definition of their functions,

Therefore, variables with the same name can be defined in other places outside the function or in other functions without conflict. Because

They all have their own culture, and they are not connectionless (I .e., they are not allowed to access other files ). Let's see the continuity of automatic traffic. Computing

When the machine executes this function, it creates and allocates memory for it. After the function is executed and returned, the automatic variable is destroyed. This process is successful.

Through a stack mechanism. When the memory is allocated for automatic variables, the stack is pressed, and the function returns the stack. Static variables: static variables and automatic changes

Essentially, static variables do not use the stack mechanism to use memory as automatic variables do. Instead, it allocates a fixed memory for static variables.

During the entire process of running the program, it will be maintained without being destroyed. This means that the persistence of static variables is the entire program running cycle. This

This helps us share some data. If a static variable is defined within the function, its scope is within the function and is only within the function.

But it is different from the automatic variable. When the automatic variable leaves the function, it will not be destroyed, but the static variable will not be destroyed. He is sending a letter

Number exists throughout the running cycle. The variable defined outside the function is a global variable. All files in the project can access it.

The entire project can only be defined once, and there cannot be repeated definitions. Otherwise, an error will occur. Other files must use this variable.

Extern to declare this variable. This declaration is called a reference declaration. This is very important. For example, you have not used extern to declare that

If you want to use the defined global variable in the defined file, you cannot use it in other files.

Use the static keyword to declare variables outside the function. In this way, this variable is invisible to other files, that is, its connectivity

Internal link. One thing we should note is that if you declare a variable outside the function, const int A; the connectivity of variable A is internal.

Link, which can only be used in the file that defines it. Also, if you do not initialize the static variables when defining them, the static variables will be

The initialization speed is 0;

Simply put.

Author: closeall

 

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.