C Language Learning Notes (2): volatile and register type modifiers

Source: Internet
Author: User
Tags modifiers

1.volatile

Volatile is volatile, unstable meaning, volatile is a keyword, is a type modifier, the variable that it modifies can be changed by some compiler unknown factors, such as operating system, hardware or other threads, encountered this keyword declaration of the variable, The compiler does not optimize the code that accesses the variable to provide stable access to the special address. So what is compiler optimization?

In order to improve the efficiency of operation, the siege of the wet is a very elaborate code optimization, the program runtime Access speed optimization. Generally, it is divided into hardware optimization and software optimization . Hardware optimization, assembly line work, detailed reference to the "principle of computer composition." Software optimization, part of the program apes do code optimization (premise you have to have the idea and ability to optimize), and part of it is our compiler optimization . After so many years of development, the modern compiler has matured, ignoring the extra variables and making the code more efficient. By default, the compiler will optimize the code, in order to increase the speed of access to variables, some variables in the register (to know that the controller, the operator and the Register is the three main components of the CPU) access, rather than in memory access, That way the CPU takes things in its own house much faster than taking things from memory. Give me a little chestnut:

int i = 5;
int a = i;
......
int b = i;
The compiler found two times from the I read the data between the code, and did not operate on I, it will automatically put the last read data in B, rather than re-read from I.

The volatile keyword tells the compiler that the variable is subject to change at any time, and the value must be fetched from memory each time it is used, so the compiler-generated assembly code reads the data from the original memory address instead of reading it from the register or cache. This ensures stable access to special addresses. In other words, the state to change frequently, in order to prevent our compilation optimization caused by the access to the data is not synchronized, then we need to use! What is the specific scenario where you need to use the volatile keyword?

1, Parallel device hardware register (such as: status register);

2. A non-automatic variable (non-automatic variables) that will be accessed in an interrupt service subroutine;

3. Variables shared by several tasks in multi-threaded applications

The above mentioned non-automatic variables, here is a further explanation of several variables:

  Automatic variable : A variable that is defined and used inside a function, which is a local variable .

  non-automatic variables : There are two, one is a global variable and one is a static variable .

Global variables: Variables defined outside the function can only be defined once, cannot have duplicate definitions, or an error will occur, and other files want to use this variable, extern to declare the variable (also can be omitted, because the default is extern), this declaration is called the reference declaration. If you do not want to be accessed by another file, declare it as a static variable with the static keyword. The essential difference between a static variable and an automatic variable is that the static variable does not use the stack mechanism as an automatic variable to consume memory. Instead, it allocates fixed memory for static variables, which are persisted throughout the program's run, without destroying them. This means that the persistence of static variables is the entire cycle of program operation. This helps us to share some data. If a static variable is defined inside a function, its scope is within the function, which is only valid for use inside the function, but it is different from the automatic variable, and the automatic variable is not destroyed after it leaves the function, and the static variable is not destroyed. He will be there for the entire running cycle of the function.

2.register

This keyword requests the compiler to make the variables available in the CPU internal registers as much as possible, rather than through memory addressing, to improve efficiency. Note is as far as possible, not absolute. You think, a CPU register is just a few or dozens of, if you define a lot of register variables, it may not be exhausted can not all put these variables into the Register bar.

C Language Learning Note (2): volatile and register type modifiers

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.