"Comparative analysis of Register/auto"
#include <iostream> using namespace std; int main () { int i,sum=0; for (i=0;i<=100;i++) sum+=i; cout<< "The sum=" <<sum<<endl; }
#include <iostream> using namespace std; int main () { register int i; int sum=0; for (i=0;i<=100;i++) sum+=i; cout<< "The sum=" <<sum<<endl; }
<span style= "font-family: ' Times New Roman '; font-size:18px; Background-color:rgb (255, 255, 255); " > int i in the front of the program is the default self-active variable (auto), the following register int i is a register variable </span>
Register/auto Difference
The active variable is allocated temporary storage space at the time of the program call, which is also called from memory, which is different from register maximum, the value of register variable is stored in the register of CPU. When called directly from the Register to remove the participation operation, stored in the register in the value of the call takes a short time, save inside from, such as the above simple program with the auto variable and register variable when there is not much difference, in large programs such savings talent embodied.
Register definition
To improve operational efficiency, the C + + language agrees to place the value of the local variable in the register in the operator. Take the join operation directly from the register if necessary, and do not have to be accessed in memory. Such variables are called register variables, with the keyword register to improve the efficiency of operation, the C + + language agrees to place the value of the local variable in the register of the operator, the need to remove the participating operation directly from the register, do not have to access the memory, such variables are called register variables, Use the keyword register as a description.
"Auto type (normal) static local variable has three different points"
1. Different storage space allocation, the auto type is allocated on the stack, belongs to the dynamic storage class, the dynamic storage space, the function call after the end of self-release, and static allocation in the static storage area, during the entire execution of the program is not released. The scope of the two is the same. But the life time is different.
2. Static local variables are initialized at the initial execution of the module in which they are located. and is initialized only once.
3. For local static variables, assuming that the initial value is not assigned, the compiler will voluntarily assign the initial value of 0 or null character, and the initial value of the auto type is indeterminate.
(For class object exceptions in C + +, the class object instance assumes that it is not initialized, and the default constructor is invoked on its own initiative.) Regardless of whether it is a static type)
Analysis of C + + auto and register and static keyword