C ++ variable storage type

Source: Internet
Author: User

The storage type is used to divide variables based on the time when the variables exist (that is, the lifetime. The Storage types of variables can be classified into static storage and dynamic storage. For dynamic storage variables, a storage space is allocated only when the program runs to the variable. When the program runs to the end of the scope of the variable, the allocated storage space is automatically reclaimed, therefore, its lifetime is the scope. At the beginning of the program execution, a bucket is allocated to it, and the storage space of the variable is reclaimed until the program ends. This variable is called a static bucket, its life cycle is the life cycle of the entire program execution.

In C ++, the storage types of variables include automatic type, register type, static type, and external type.

  • Auto: the auto type can only be a local variable and belongs to the dynamic storage type.
  • Static storage variable (static): static, that is, when the program is running, static variables always occupy a storage space. Static variables can only be used within the scope of their functions. Local static variables are used to use the value of the variable obtained after the last call when the function is called next time.
  • Register variable: it is a dynamic storage type. The Compiler does not allocate memory space for register-type variables, but directly uses the CPU registers to increase the access speed of such variables. It is mainly used to control the number of cycles and other variables that do not need to save values for a long time.
  • External type variable (extern): The External type variable must be a global variable. In C ++, the external type variable must be 1, in the same source program, when the variable is used before the global definition, the variable must be declared as an external class variable before use. 2. When a program consists of multiple files, if you want to reference the global variables defined in another source file in one source file, you must make an external declaration on the referenced variables before referencing.

In addition, static global variables can only be used by files.

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.