Register Keyword: The request compiler lets the variable be placed directly inside the CPU internal registers, rather than through memory addressing access, fast.
In C, the register-modified variable cannot take the address, and the address of the register variable is in C language error.
int main ()
{
register int a=0;
printf ("&a=%x\n", &a);
return 0;
}
Compile Error!
The same code we put in the C + + compilation environment, found that the compilation is through!
#include <iostream>
using namespace Std;
int main ()
{
register int a=0;
printf ("&a=%x\n", &a);
System ("pause");
return 0;
}
Operation Result:
&a=45fa78
Please press any key to continue ...
The Register keyword is still supported in C + +, the C + + compiler has its own optimizations, it is possible to optimize without register, and the address of the register variable can be obtained in C + +, and the C + + compiler discovers the address of the register variable in the program. The register of the variable is not valid. The early C compiler does not optimize the code, so the register variable is a good addition.
Let's talk about how the C + + compiler is optimized:
#include <iostream>
using namespace Std;
int main ()
{
register int a=0;
printf ("&a=%x\n", &a);
for (int i=0;i<1000;i++)
{
printf ("i:%d\n", I);
}
System ("pause");
return 0;
}
Like for loops where I is frequently used, the C + + compiler optimizes the use of the variable I in the register to speed up the execution of the program. That is, C + + is optimized even without the keyword register.
C + + Note 010:c++ extensions to C--register keyword enhancements