C + + auto, static, register, extern differences

Source: Internet
Author: User
Tags error handling

First, a few concepts:

1. From the lifetime of the variable, can be divided into static storage mode and dynamic storage mode


static Storage: How the system allocates a fixed amount of storage space (global variables) during program run


Dynamic storage: the way in which storage space is allocated dynamically as needed during program operation (1. function parameters, 2 automatic variables, 3 field protection box when function call return address)


2. Data storage category contains four kinds:auto (auto), static (static),register(register),extern (external)


auto: keyword auto can be omitted, auto does not write implicitly determined as automatic storage category, belongs to dynamic storage mode


Static: Sometimes I want the value of the local variable of the I function to remain the original value after the function call ends, that is, the storage unit it occupies is not freed, and the next time the function is called, the variable already has a value (the value at the end of the last function call)

Example:

#include "stdafx.h" #include <iostream>using namespace Std;int fun (int a) {int b=0;static int C=3;b++;c++;return a+b +c;} int _tmain (int argc, _tchar* argv[]) {int a=100;for (int i = 0; i < 3; i++) {Cout<<fun (a) <<endl;} return 0;}

Output Result:

The value of a does not change

b The initial value is 0 for each invocation and 1 after the call

The initial value of C calls is 3, and the post-bit 4 is called.

------4,-------------5

------5,-------------6


Description

1. Static local variables are static storage classes that allocate storage units within a static storage area

2. For a static local variable is the initial value at compile time, that is, the initial value is assigned only once, when the program is running, it has the initial value, the subsequent call to the function will no longer reproduce the assigned value, but only the last function call at the end of the values. (The automatic variable assigns the initial value, not at compile time, but at the function call, and each repro call is re-assigned to the initial value)

3. If the initial value is not given, the compiler will automatically assign a value of 0 (int) or a null character (char) for a static local variable, whereas for an automatic variable it is an indeterminate value.

4. Although static local variables persist after the function call has ended, other functions cannot reference it.


Register : If some variables are used frequently, it can be declared with the keyword register (this can improve execution efficiency because the register is accessed much faster than the internal access speed)


Description

1. Only local automatic variables and formal parameters can be used as register variables, other such as global variables do not

2. The number of registers in a computer is limited, and you cannot define any number of register variables. Different systems allow the number of registers is different, the Register variable processing method is also different, some system to register variable as an automatic variable processing, allocating storage units, do not really put them in registers, some systems only allow int, Char and pointer variables are defined as register variables.

3. A local variable cannot be defined as a register variable, and a variable cannot be placed either in a static store or in a register, but only one. A variable can only be declared as a storage class

4. Today's optimized compilation system recognizes variables that are used frequently and automatically places them in registers without the need for programmer designation. Therefore, it is not necessary to actually declare variables with register.


extern  :

External variables: External variables are variables defined outside the function, scoped to the end of the program file, starting at the definition of the variable. Within this scope, global variables can be referenced by individual functions in the program. The external variables are assigned to the static store at compile time.

Use extern to declare the outer region, extending the scope of the external variable.


1. Declaring an external variable within a file

If an external variable is not defined at the beginning of the definition, its valid scope is limited to the definition to the end of the file. If the function before the definition point wants to refer to the external variable, it should be declared with the keyword extern before the reference, indicating that the variable is a defined external variable, and with this declaration, the external variable can be legitimately used from the declaration.


CTestProject.cpp: Defines the entry point of the console application. #include "stdafx.h" #include <iostream>using namespace std;int _tmain (int argc, _tchar* argv[]) {extern int A, a, b; cout<< "A:" <<A<<endl;cout<< "B:" <<b<<endl;return 0;} int a=5,b=10;

If there is no extern int A, b, then the identifier of the tampon, A, b

Of course, a better approach is to avoid adding an extern keyword to a function before the definition of an external variable is placed before all functions referencing it

#include "stdafx.h" #include <iostream>using namespace Std;int a=5,b=10;int _tmain (int argc, _tchar* argv[]) {// extern int a,b;cout<< "A:" <<A<<endl;cout<< "B:" <<b<<endl;return 0;}


2. Declaring an external variable in a program of multiple files

If your program has multiple source files, you can use the extern extension scope when you want to reference an external variable that is already defined in another file.

such as: A.C file has a variable a,b.c want to refer to variable A, only need to at the beginning of the file extern A can be (can not be separated in A.C A variable A is defined in the B.C file, otherwise a duplicate-defined error occurs when the program is connected, and when compiled and connected, the system will know that a is an external variable that has been defined elsewhere and extends the scope of the external variable defined in the other file to this file, which in this file can legitimately refer to the external variable a


extern can extend both the scope of the external variable in this file and the scope of the external variable from one file to another, the system's processing rules are:

1, first in this document to recruit the definition of external variables, if found, in this file extension scope;

2, if not found, in the connection when the external variables from other files to find the definition, if found, will extend the scope to this file;

3, if not found, press error handling


3. Static external variable (with static declaration of external variables)

You can use the static declaration if you want some external variables to be referenced only by this file in the program design and cannot be referenced by other files.

such as: A.C

static int A

void Main ()

{

...

}


B.c


extern int A;

void Fun ()

{

....

}


Global variable A in A.C cannot be used in the B.C file



C + + auto, static, register, extern differences

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.