Keywords related to the storage class of C language

Source: Internet
Author: User

Different data in memory storage location is different, the overall memory of the place to store data is mainly four parts: stack, heap, data segment, BSS section, these places are stored in different data, such as the stack is generally used to store local variables, heap memory needs the programmer to apply the word himself and release, It is mainly used for storing large data; The data segment is used primarily to store static local variables that display the initialized global variables and statically keyword decorations, and BSS segments are typically used to hold global variables that are not explicitly initialized or to be explicitly initialized to 0 global variables (in C, the default global variables are initialized to 0). The C language also provides keywords to modify variables to include other attributes, such as auto, static, const, register, extern, volatile, restrict.


1:auto

The Auto keyword has only one effect: The local variable is modified, the local variable represented by the Auto keyword is an automatic local variable, the automatic local variable is allocated on the stack, and the default local variable in C is the automatic local variable.


2:static

The Static keyword works in two ways:

The first is to modify local variables, to get static local variables, to assign static local variables to data segments or BSS segments, and not static local variables to be allocated on the stack.

The second is to modify global variables to get static global variables, and the comparison of static global variables and non-static global variables is as follows:

(1) Static global variables and global variables of the storage class, are assigned to the data segment or BSS segment

(2) The static global variable is the same as the life cycle of the global variable.

(3) The scope of a static local variable is the scope of the code block ({}: a block of code, a common local variable

Link properties are non-connected, the scope of the global variable is the file scope (as with the function), and the link property is an outer join.


3:const

Const is often referred to as a constant modifier, which means that the const-modified "variable" becomes a constant ( so a const is used to modify the input parameter in a function parameter, and we often think of it as an output parameter without a const-modified parameter.) However, in the C language, this constant is not completely immutable, because the const modified "variable" is not placed in the code snippet (the code snippet is read-only, the data is placed in the code snippet can not be modified), the const keyword decorated "variable" by the way the pointer can be modified, The implementation is a pointer to this "variable", and then dereference can modify the const modified "variable."


4:register

Register keyword is not commonly used, its function is also unique: Register modified variable, in the future when compiling the compiler will try to assign it to register (usually the general variable is allocated in memory), the variable is assigned to the register in its use and is allocated in memory is the same, But the efficiency of its sketches will be much higher. But since the number of registers is limited, it is only possible to put it in registers, not necessarily. We usually need to use register to modify a few variables, so we need to use caution.


5:extern

The extern keyword is used to declare a global variable, The reason is that the C language is compiled in a single. c file to compile, but a large project can not have only one. c file, this time will need to extern the variables, functions and other declarations to. c file outside, so that when compiled A.C functions and variables can be called by B.C.


6:volatile

A volatile-modified variable indicates that the variable can be modified at any time, so the compiled program reads the data directly from the variable address whenever it needs to store or read the variable, without the volatile-modified variable, and the compiler might optimize reading and storage. The value in the register may be used temporarily, and if the variable is modified by another program, the compiler may think that the variable is still not modified (because it is possible to read the value of the variable in the register, and the program modifies the value of the variable in memory).

int flag;    void Test (void) {do1 ();    while (flag==0);    Do2 (); }

only if flag is not 0 o'clock, The d02 () function can be executed, assuming that we now create an interrupt by pressing the key, and then interrupt the handler function to assign the flag value to 1 so that the D02 () function can be executed, but the compiler does not know that the value of flag has been modified by another program. The reason is that the compiler optimizes the program at compile time, so optimizations can put flag in a register, and the next time it is read, it is probably the value of flag in the register.

In general, volatile is mainly used in the following areas

(1) Variables modified in the Interrupt service program for other programs to detect

(2) The signs shared between tasks in a multitasking environment should be volatile

(3) The hardware registers of the memory mappings are usually also described as volatile, because each time they read and write may be different.

To put it simply, volatile is always about optimization, the compiler has a technique called data flow analysis, where variables in the program are assigned, where they are used, where they are invalidated, analysis results can be used for constant merging, constant propagation optimization, and further dead code elimination, but sometimes the program does not need these optimizations, This can be done by using volatile to suppress these optimizations.

The description of the volatile keyword comes mainly from the following blog

Http://www.cnblogs.com/yc_sunniwell/archive/2010/06/24/1764231.html


7:restrict

The Restrict keyword can only be used to modify pointers, not to be used to modify variables, depending on the use of reference

Http://blog.chinaunix.net/uid-22197900-id-359209.html





Keywords related to the storage class of C language

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.