[C++] C++ reference detailed explanation ③ (The return value of a function cannot be a reference or pointer of a "local variable" | It is meaningless to use a reference or pointer of a "local variable" in a function as a function return value)





1. The function return value cannot be a reference or pointer to a "local variable"




1. References are usually rvalues


When using references before, they were used as rvalues, and references were only used as lvalues ​​when they were declared and initialized at the same time .

// 定义变量 a
int a = 10;

// 定义变量 a 的引用 b
int& b = a;

After the reference is declared and initialized, it has never been an rvalue, because the essence of the reference is a pointer constant, which cannot be changed;


2. Function return value characteristics


Function return values ​​are rarely references or pointers;

The calculation result of the function is often returned by borrowing the address/reference in the parameter. The
return value of the function generally returns an int type value. If the int is 0, it means success, and if the int is other values, it is an error code;


3. The reference or pointer of the "local variable" in the function is meaningless as the return value of the function


If you want to use references or pointers as the calculation results of functions, you generally use references and pointers as incoming parameters;

In the main function, call the function, create a variable, pass the address/reference of the variable into the function, and directly modify the passed-in actual parameter through the pointer symbol or reference in the function , that is, modify the value in the memory pointed to by the address/reference Data, this operation can modify the variable value in the external main function;


If you want to return an address/reference as a return value in a function ,

Whose address/reference is this,

  • If it is the address/reference of a variable created in the internal stack memory of the function , then the function execution ends, and when the function returns, the stack memory is directly reclaimed, and the memory space pointed to by the address/reference may be a random value;
  • If it is the address/reference of the variable in the external main function , it must be passed in from the parameter, then this address/reference does not need to be returned, and the internal modification of the function is directly reflected in the external variable;

Therefore, returning the address/reference of a local variable is meaningless,

A general function only returns an int value, indicating whether the function is successfully executed , and if the execution fails, it returns an error code (at which step the execution failed);


If you want to return a reference/pointer in a function, the reference/pointer of the function local variable cannot be returned,

Even if the reference/pointer is forcibly returned, it is the stack memory address where the current local variable is allocated,

After the function is executed, the stack memory corresponding to the function will be reclaimed, and the corresponding local invariant address has no meaning.

At this time, hold another meaningless reference/pointer, and the value taken out is a random meaningless value;





2. Code example - "local variable" reference or pointer for function return value test



The following int& getNum2()function, returns a reference, which is a reference to a local variable;

The following int* getNum3()function, returns a pointer, which is a pointer to a local variable;

The above two functions are meaningless. After obtaining the reference or pointer of the "local variable" returned by the function, and then obtaining the address, it is found that all obtained are random values, which are meaningless values;

num21 = -858993460 , *num3 = -858993460

Code example:

// 包含 C++ 头文件
#include "iostream"

// 使用 std 标准命名空间
//		该命名空间中 , 定义了很多标准定义
using namespace std;

// 导入 C 头文件
#include <stdio.h>

// 返回值是普通变量
int getNum()
{
    
    
	int num = 10;
	return num;
}

// 返回值是引用
int& getNum2()
{
    
    
	// 此处的 num 是临时变量
	// 该临时变量占用的 栈内存 空间 
	// 在函数执行完毕后 , 会被回收 , 失效
	int num = 20;
	int& a = num;
	return a;
}

// 返回值是指针类型
int* getNum3()
{
    
    
	int num = 30;
	return &num;
}

int main()
{
    
    
	// 函数返回 int 类型变量 赋值给 int 类型变量 num1
	int num1 = getNum();

	// 函数返回 int 类型引用
	// 将 引用 赋值给 num2 变量
	// 此处 使用 变量 接收引用值 , 
	// 会自动将引用值对应的内存数据 10 取出来 , 赋值给变量
	int num2 = getNum2();

	// 将 int 类型引用 赋值给 num21 int 类型引用
	// 这里只能记录地址 , 没有将值取出来保存
	// 该地址马上就要被其它数据覆盖了
	int& num21 = getNum2();

	// 将 返回的 指针赋值给 int 类型指针
	// 这里只能记录地址 , 没有将值取出来保存
	// 该地址马上就要被其它数据覆盖了
	int* num3 = getNum3();

	// 再次调用一次 , 覆盖被回收的栈内存数据
	// 这一步主要是混淆栈内存 , 使栈内存混乱
	int num4 = getNum();

	// 打印计算结果
	printf("num1 = %d , num2 = %d , num21 = %d , *num3 = %d , num4 = %d \n", num1, num2, num21, *num3 , num4);


	// 控制台暂停 , 按任意键继续向后执行
	system("pause");
    return 0;
}

Results of the :

num1 = 10 , num2 = 20 , num21 = -858993460 , *num3 = -858993460 , num4 = 10
Press any key to continue . . .

insert image description here

Guess you like

Origin blog.csdn.net/han1202012/article/details/132483858