Analysis of C + + auto and register and static keyword

Source: Internet
Author: User

"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

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.