1.2 程序运行后
栈区:
由编译器自动分配和释放,存放函数的参数值,和局部变量等
注意事项:不要返回局部变量的地址,栈区开辟的数据由编译器自动释放
#include <iostream>
using namespace std;
//栈区数据的注意事项 ———— 不要返回局部变量的地址
//栈区的数据由编译器开辟管理释放
int * func( int b)//形参数据也会放在栈区
{
b = 100;
int a = 10; //局部变量 存放在栈区,栈区的数据在函数执行完之后自动释放
return &a; //返回局部变量的地址
}
int main()
{
//接收func的返回值
int* p = func(1);
cout << *p << endl;//第一次可以打印正确的数字是因为编译器做了保留
cout << *p << endl;//第二次这个数据不再保留
system("pause");
return 0;
}
堆区:
-
由程序员分配释放,若程序员不释放,程序结束时由操作系统回收
-
在C++中主要利用new在堆区开辟内存
#include <iostream>
using namespace std;
int * func()
{
//利用new关键字可以将数据开辟到堆区
//指针 本质也是局部变量,放在栈上
int * p = new int(10);
return p;
}
int main()
{
//在堆区开辟数据
int* p = func();
cout << *p << endl;
cout << *p << endl;
cout << *p << endl;
system("pause");
return 0;
}
1.3 new操作符
C++中利用new操作符在堆区开辟数据
堆区开辟的数据,由程序员手动开辟,手动释放,释放利用操作符delete
语法:new 数据类型
利用new创建的数据,会返回改数据对应的类型的指针
#include <iostream>
using namespace std;
//1、new的基本语法
int* func()
{
//在堆区创建整型数据
//new返回是 该数据类型的指针
int* p = new int(10);
return p;
}
void test01()
{
int* p = func();
cout << *p << endl;
cout << *p << endl;
cout << *p << endl;
//堆区的数据 由程序员管理开辟,程序员管理释放
//如果想释放堆区的数据,利用关键字delete
delete p;
//cout << *p << endl;//内存已经被释放,再次访问就是非法操作,会报错
}
//2、在堆区利用new开辟数组
void test02()
{
//创建10整型数据的数组,在堆区
int * arr = new int[10];//数组有十个元素
for (int i = 0; i < 10; i++)
{
arr[i] = i + 100;//给10个元素赋值,100~109
}
for (int i = 0; i < 10; i++)
{
cout << arr[i] << endl;
}
//释放堆区数组
//释放数组的时候,要加[]才可以
delete[] arr;
}
int main()
{
test01();
test02();
system("pause");
return 0;
}
2 引用
2.1引用的基本使用
作用:给变量起别名
语法:数据类型 &别名 = 原名
#include <iostream>
using namespace std;
int main()
{
//引用的基本语法
//数据类型 &别名 = 原名
int a = 10;
//创建引用
int& b = a;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
b = 100;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
system("pause");
return 0;
}
2.2 引用注意事项
-
引用必须初始化
-
引用在初始化之后,不可以改变
#include <iostream>
using namespace std;
int main()
{
int a = 10;
//1、引用必须初始化
//int& b;//错误
int& b = a;
//2、引用初始化之后,就不能改变
int c = 20;
b = c;//赋值操作,而不是更改引用
cout << "a=" << a << endl;
cout << "b=" << b << endl;
cout << "c=" << c << endl;
system("pause");
return 0;
}
2.3 引用做函数参数
作用:函数传参时,可以利用引用的技术让形参修饰实参
优点:可以简化指针修改实参
#include <iostream>
using namespace std;
//交换函数
//1、值传递
void Swap1(int a, int b)
{
int temp = a;
a = b;
b = temp;
}
//2、地址传递
void Swap2(int* a, int* b)
{
int temp = *a;
*a = *b;
*b = temp;
}
//3、引用传递
void Swap3(int& a, int& b)
{
int temp = a;
a = b;
b = temp;
}
int main()
{
int a = 10;
int b = 20;
//Swap1(a, b);//值传递,形参不会改变实参
//Swap2(&a, &b);//地址传递,形参会改变实参
Swap3(a, b);//引用传递,形参会改变形参,上面函数的a和b是这里a和b的别名
cout << "a=" << a << endl;
cout << "b=" << b << endl;
system("pause");
return 0;
}
2.4 引用做函数返回值
作用:引用是可以作为函数的返回值存在的
注意:不要返回局部变量引用
用法:函数调用作为左值
#include <iostream>
using namespace std;
//引用做函数的返回值
//1、不要返回局部变量的引用
int& test01()
{
int a = 10;//局部变量存放在四区中的 栈区
return a;
}
//2、函数的调用可以作为左值
int& test02()
{
static int a = 20;//静态变量,存放在全局区,全局区上的数据在程序结束后系统释放
return a;
}
int main()
{
//int& ref = test01();
//cout << "ref=" << ref << endl;//第一次结果正确,是因为编译器做了保留
//cout << "ref=" << ref << endl;//第二次结果错误,因为a的内存已经释放
int& ref2 = test02();
cout << "ref02=" << ref2 << endl;
cout << "ref02=" << ref2 << endl;
test02() = 1000;
cout << "ref02=" << ref2 << endl;
cout << "ref02=" << ref2 << endl;
system("pause");
return 0;
}
2.5 引用的本质
本质:引用的本质在C++内部实现是一个指针常量
#include <iostream>
using namespace std;
//发现是引用,转换为 int* const ref = &a;
void func(int& ref)
{
ref = 100;//ref是引用,转换为 *ref = 100
}
int main()
{
int a = 10;
//自动转换为 int* const ref = &a;指针常量是指针指向不可改变,也说明为什么引用不能改变
int& ref = a;
ref = 20;//内部发现ref是引用,自动帮我们转换为:*ref = 20;
cout << "a:" << a << endl;
cout << "ref:" << ref << endl;
func(a);
system("pause");
return 0;
}
2.6 常量引用
作用:常量引用主要用来修饰形参,防止误操作
在函数形参列表中,可以加const修饰形参,防止形参改变实参
#include <iostream>
using namespace std;
//打印数据函数
void showValue( const int& val)
{
//val = 1000;//加上const 防止误操作
cout << "val=" << val << endl;
}
int main()
{
//常量引用
//使用场景:用来修饰形参,防止误操作
//int a = 10;
//int& ref = 10;//引用必须引用一块合法的内存空间
//const int& ref = 10;//加上const之后 编译器将代码修改 int temp = 10; const int & ref = temp;
//ref = 20;//加入const之后变为只读,不可修改
int a = 100;
showValue(a);
cout << "a=" << a << endl;
system("pause");
return 0;
}