Overlay/static/register/Atuo/extern/volatile/const modifier usage

Source: Internet
Author: User
Tags microsoft c
Static

LStatic variables are used in a file,ProgramAllocate space at the beginning and release space at the end. The default value is0, You can change its value when using it.

LStatic variables or static functions, that is, onlyCodeIts name (variable name or function name) is invisible to other files.

LThe value of a static variable generated in the function body can only be maintained.

Int max_so_far (INT curr)// Calculate the maximum value so far (this call)

 

{

 

 Static int biggest;// This variable keeps the latest value for each call. Its validity period is equal to the validity period of the entire program.

 

 If (curr> biggest)

 

 Biggest = curr;

 

 Return biggest;

 

}

L in C ++ the member variables of the class are declared as static (called static member variable ), this means that it is shared by all instances of the class. That is to say, when an instance of a class modifies the static member variable and its value is changed to what is seen by all other instances of the class; the static member functions of the class can only access static members (variables or functions ).

LClass static member variables must be initialized within the scope of the file declared for use,Private type is no exception. For example,

 

 

 
 

Float savingsaccount: currentrate = 0.00154;

 

 

 

 

 
 

(Note: currentrate is a static member variable of the savingsaccount class)

 

 

 

 
 
Register

L Use Register The declared variables are called register variables, which are directly stored in the machine's registers if possible. 32 Bit compiler does not work, when Global Optimizations (Global Optimization) when it is enabled, it will make the choice whether to put it in its own register; however, other Register All other symbols related to the keyword are 32 The bit compiler is valid.

Auto

LIt is a storage type identifier, indicating that the variable (automatic) has a local range, block range variable Declaration (suchForThe variable declaration in the loop body. The default value isAutoStorage type.

Extern

L declare a variable or function as an external link, that is, the variable or function name is visible in other files. The modified variables (external variables) are statically allocated space, that is, allocated at the beginning of the program and released at the end. Variables or functions declared with them should be defined (implemented) in other files or other places in the same file ). Declare a variable or function in the file to be used externally by default.

L in C ++ , you can also specify the link in another language, in this case, it must be used with a specific conversion character. Currently, Microsoft C/C ++ only " C " conversion tag to support C compiler links. The following two methods are used:

UExtern"C" Statement

 

 

UExtern"C"{ Statement Block }

Volatile

LAn object can be changed by an external process (operating system, hardware, or concurrent thread). The statement syntax is as follows:

Int volatile nvint;

 

Such declaration cannot achieve the most efficient, because their values change at any time, and the system will read and write the value of this object frequently when needed.It is used only for memory unit access by asynchronous processes such as interrupt handlers.

 

Const

LConstThe modified object or variable cannot be changed. During function modification, the variable declared outside the function cannot be changed or any non-ConstFunction. You must addConstPut it behind the last parenthesis in the function parameter list.

L In C ++ Medium, use Const Declare a variable, which means the variable is a constant with the type and can be replaced # Define And # Define One more type information, and it executes the internal link, which can be declared in the header file; but in C The declaration must be placed in the source file (that is . C File), in C Medium Const Declare a variable, except that it cannot change its value, it is still a variable, such

Const int maxarray = 255;

 

Char store_char [maxarray];// C ++ is valid, and C is invalid

 

LConstPay special attention to modifying pointers. Example:

Char * const aptr = mybuf;// Constant pointer

 

 

 
 

* Aptr = 'a'; // legal

 

 

 

 
 

Aptr = yourbuf; // Error

 

 

 

 
 

Const char * bptr = mybuf;// (Pointer bptr) pointing to constant data

 

 

 
 

* Bptr = 'a'; // Error

 

 

 

 
 

Bptr = yourbuf; // legal

 

 

 

 
 

LConst cannot be used for const and destructor when modifying member functions.

 
 

Volatile indicates that the value of a variable may be changed externally. When using this variable, the optimizer must carefully read the value of this variable every time instead of using the backup stored in the register. It can be applied to basic types such as int, Char, long... and C structures and C ++ classes. When the structure or Class Object is modified using volatile, all the members of the structure or class are considered volatile. this keyword is often used in a multi-threaded environment, because the same variable may be modified by multiple threads when a multi-threaded program is compiled, and the program synchronizes various threads through this variable. Simple Example: DWORD _ stdcall threadfunc (lpvoid signal) {int * intsignal = reinterpret_cast (signal); * intsignal = 2; while (* intsignal! = 1) sleep (1000); Return 0;} set intsignal to 2 when the thread starts, and then wait until it exits when intsignal is 1. Obviously, the intsignal value must be changed externally; otherwise, the thread will not exit. But the thread does not exit during actual operation. Even if the value of this thread is changed to 1 in the external department, you can see the corresponding pseudo assembly code: mov ax, signal label: if (ax! = 1) for the C compiler, Goto label does not know that this value will be modified by other threads. Naturally, it is cached in the register. The C compiler does not have the thread concept. Volatile is used in this case. Volatile indicates that the value may be changed outside the current thread. That is to say, we need to add the volatile keyword before intsignal in threadfunc. At this time, the compiler knows that the value of this variable will change externally, so it will read the variable again every time it is accessed, as shown in the following pseudo code: Label: mov ax, signal if (ax! = 1) goto label Note: A parameter can be either const or volatile because it may be unexpectedly changed. It is const because the program should not try to modify it.

 

 

 

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.