1005------C Language-----------The analysis of C language variable storage mode

Source: Internet
Author: User

0. Background

While looking at the seventh chapter of Apue, the author analyzes the effect of setjmp and longjmp on different storage type variables, and sees register,auto,volatile,static And so on these keywords is very vague, may also be too little use, but think of as C language Foundation good I still can not accept any do not understand the knowledge point, then smoothed the idea.

Basic properties of 1.C language variables

In the C language, each variable has 3 properties :

(1 ) Type, is the commonly used int, char, etc., it determines the variable storage length and operation mode;

(2) scope, according to the position of the variable is divided into global variables and local variables;

(3) storage mode , i.e. the lifetime of the variable, is divided into static storage mode and dynamic storage mode.

2. Static storage mode and dynamic storage mode

(1) Static storage means that variables always occupy a fixed-size storage unit during the whole process of program execution , until the program runs to the end to be released.

(2) Dynamically stored variables are the storage units of variables that are dynamically allocated and recycled by the system while the program is running, when the functions that define them are allocated memory, and when the function that defines them returns, the system reclaims the memory of the variables.

3. Keywords related to storage mode

(1)auto: Automatic variable, by default , all variables are auto, which is the most widely used in C language. Automatic variables are local variables, stored in dynamic storage, the system does not initialize it by default, uninitialized static variables point to an unexpected, meaningless value. Only when you use it, that is, when the function that defines the variable is called, it allocates a storage unit and begins its lifetime. The function call ends, freeing the storage unit and ending the lifetime.

(2)register: Register variable, as auto, is a local variable, dynamic storage mode. The register variable requests the compiler to store variables in the internal CPU register as much as possible rather than through memory access to raise efficiency . Note that for register variables, many C compilers do not actually place register variables inside the CPU, but rather as automatic variable processing. Using register simply prompts the compiler to define the variable as a register variable, expecting the compiler to use it as a register variable, but not necessarily to meet expectations. Because it depends on whether the compiler supports it, the second is to see if there is any available register space inside the CPU.

(3)static variable, which is divided into static global variables and static local variables. We all know that a local variable is stored dynamically, and a static modifier is called a local variable, which causes the local variable to have the lifetime of the static storage, from the allocated memory unit until the end of the program run. A static global variable allows the global variable to be used only in the file in which it is defined, and for a generic global variable to be accessed in another file requires only an external reference declaration (extern). In addition, the basic type of static variable system will automatically assign its initial value, static storage mode is not equal to static variables.

(4)extern: An external variable, a modified variable or function, indicating that the variable or function is foreign, that is, defined in another file , this is more common.

(5)volatile: volatile variable, which is seldom seen, volatile variables are used to prevent the compiler from optimizing , telling the compiler to go to the memory each time to take a value, not from the register or cache, One of the compiler optimizations here is to save memory variables in registers to improve efficiency, which is prohibited by adding volatile in front of variables.

4. References


(2) http://blog.csdn.net/ouyangzhengxuan/article/details/7325505

(3) http://www.worlduc.com/blog2012.aspx?bid=3243095

(4) "C-Language depth analysis"

1005------C language-----------C language variable storage parsing

Related Article

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.