Exploring the C++ series - those things about variables


Variables are a very basic concept in all programming languages, but there are also some in-depth knowledge points behind it. This article will talk to you about variables in C++.


what is a variable

A variable is the name that holds the memory address of the data.
For example, define int a=1; a is the name of the variable. After we define this variable, the system will allocate a memory address for this variable, and then the corresponding data can be written into the memory through this address. Because the computer looks for the data to be operated through a section of address, or writes data into it, but the address is usually a long and strange thing, if we programmers are asked to remember the address name of each data, it is too much Against humanity? So C++ and various high-level languages ​​have the concept of variables. We can give the memory address of the data a name we like, so that it will be convenient for us to use later.
For C++ programmers, "variable" and "object" are generally used interchangeably.


variable initialization

When defining, one or more variables can be assigned initial values, that is, initialization (initialization) .
For example, int a=1; defines a variable a and initializes its value to 1. Write two things on one line.
In addition, the initialization method of int a (1) is also OK. But there is a better way in C++11, which will be introduced later.
Note: initialization is not equal to assignment (assignment) . The meaning of initialization is to assign an initial value to a variable when it is created, and the meaning of assignment is to erase the current value of the object and replace it with a new value.

//赋值
int a;
a=1;

Initializing variables with curly braces is called list initialization . This is a new feature of C++11. When used on variables of built-in types, the compiler will report an error if list initialization is used and the initial value risks losing information. This is relatively common in type conversions. What does that mean? Let's take an example:

double ld=3.1415926536;
int a{
    
    ld}, b = {
    
    ld};    // error: 转换未执行,因为存在丢失信息的危险     
int c(ld), d = ld;      // ok: 转换执行,且确实丢失了部分值

Default initialization of variables

If no initial value is specified when defining a variable, the variable is default initialized .
1) Variables defined outside any function body are initialized to 0, such as global variables.

2) Variables inside the function body will not be initialized (uninitialized). If the built-in type object defined in the function body is not initialized, its value is undefined . Using an uninitialized variable is a wrong programming behavior and it is difficult to debug! ! !
There may be very strange phenomena! ! ! Maybe the variable contains previous data
before it is initialized and assigned . These data are messy, so if the variable is used without initialization, the result is also meaningless. Any outcome is possible . So sometimes if you can be sure that there is basically no problem with the logic of your program, but the result is not what you expected, you might as well check whether the variable has been initialized, or use it without assigning a value to it. When I was doing C++ algorithm questions before, I often got wrong results because of this. Looking at the debugger, it turns out that the initial value of a certain variable is not 0. This is the possible consequence of using uninitialized variables. After testing, the program using uninitialized variables in dev c++ will run normally, but there are potential risks. In Visual Studio 2019, it will directly give you a warning



Visual Studio 2019

3) The variables mentioned above refer to ordinary variables. For static variables , it is the variable with static added at the beginning of the definition, whether it is outside or inside the function, it is initialized to 0 by default.

4) If the object of the class is not explicitly initialized, its value is determined by the code of the class.


global variables, static variables, local variables

insert image description here

1) Global variables, static global variables, and static local variables are all stored in the static storage area (global data area). This is a static storage . When the variable is defined, a certain memory unit is allocated. Since the storage location is determined in advance (before the program runs, that is, during compilation), then simply give it a default value of 0, which will not affect the efficiency of the program anyway.
Local variables are allocated space on the stack. Generated dynamically on the stack each time a function is called . This is a dynamic storage . In order to improve efficiency , the system gives the right to initialize variables to the programmers themselves, and will not assign initial values ​​for us. However, if the programmer forgets to initialize the variable or reassign it, there may be other values ​​in the stack space allocated by the system to the variable, causing hidden dangers. So the default value of local variables is undefined.

2) The difference between global variables and global static variables
Both are stored in the static storage area.
But the scope of global variables is the whole project . It only needs to be defined in one source file to apply to all source files. Other files that do not include global variable definitions need to declare this global variable again with the extern keyword.
The scope of a static global variable is the program file in which it is defined. Cannot affect other files in the project .

3) The difference between static local variables and (dynamic) local variables
They have the same scope , and they can only be used inside functions that define themselves. But the memory release cycle is different (lifetime) . Dynamic local variables release memory when the function execution ends. Static local variables exist for as long as the program is running . It can only be initialized once . What is it that can only be initialized once? Consider the following program:

int fun(){
    
    
	static int num=0;
	return ++num;
}
int main(){
    
    
	for(int i=0;i<10;i++){
    
    
		cout<<fun()<<" ";
	}
	return 0;
}

Output results: 1 2 3 4 5 6 7 8 9 10
Note that I call the fun() function cyclically in the main function. Although there is an operation to initialize the static local variable num in the fun() function, each cycle will not Reset the value of num to 0, but save the value of num after increment. This is because num is still in memory and will not be destroyed by exiting the function. So it is only initialized when it is called for the first time, and calling the same function again after the function is executed will inherit the value of this static local variable.

Summary
Local variables -> static variables: change its storage method and lifetime
Global static variables -> global variables: change its scope

Variable declaration and definition

To support separate programming, C++ needs to separate declarations from definitions.
The declaration tells the compiler that the variable exists (making the name known to the program). A file that wants to use a name defined elsewhere must first include a declaration for that name.
Definitions are responsible for creating entities associated with names. In addition to telling the compiler that the variable exists, the definition also allocates space for the variable and initializes it

If you want to declare a variable without defining it, add the keyword extern before the variable name , and do not initialize the variable explicitly.

extern int i; // 声明但不定义 i
int j;      // 声明并定义 j
extern int k=0 //定义

If the extern statement contains the initial value, it is no longer a declaration, but a definition.

Variables can be defined only once, but can be declared multiple times.

If you want to use the same variable in multiple files, you must separate the declaration and definition. At this time, the definition of the variable must appear and can only appear in one file, and other files that use the variable must declare it, but it must not be defined repeatedly.

Scope of variable names

{ } is a block scope.
Names defined outside the function body have global scope . Once declared, the name is available throughout the program. It is best to define a variable the first time it is used. This makes it easier to find where the variable is defined, and it can also be assigned a reasonable initial value.

Once a name is declared in a scope, it is accessible in all scopes it is nested in . At the same time, it is allowed to redefine the existing names of the outer scope in the inner scope. At this time, the newly defined name in the inner scope will mask the name of the outer scope .

int main() {
    
    
	int num = 1;
	{
    
    
		int num = 2;
		cout << num << endl;
	}
	cout << num << endl;
	return 0;
}
//输出结果:
2
1

When a global variable and a local variable have the same name, the scope operator :: can be used to access the global variable . Because the global scope itself has no name, when the left side of the scope operator is empty, a request will be made to the global scope to obtain the variable corresponding to the name on the right side of the scope operator. This method can be used to access global variables when there is a local variable with the same name as the global variable in the scope. See
an example:

int num;
int main() {
    
    
	int num = 1;
	{
    
    
		int num = 2;
		cout << num << endl; 
		//注:在此代码块中无法直接访问外层值为1的num,因为重新定义了num
		// ::num只能访问此方法之外的全局变量
	}
	cout << num << endl;
	cout << ::num << endl;  //访问全局变量的num
	return 0;
}
//输出:
2
1
0

Guess you like

Origin blog.csdn.net/qq_46044366/article/details/119602816