1. 内存分区模型
cpp程序在执行时,将内存大方向划分为4个区域
- 代码区:存放函数体的二进制代码,由操作系统进行管理
- 全局区:存放全局变量和静态变量以及常量
- 栈区:有编译器自动分配释放,存放函数的参数值,局部变量等等
- 堆区:由程序员分配和释放,若程序员不释放,程序结束时由操作系统回收
内存四区的意义:
不同区域存放的数据,赋予不同的声明周期,给我们更大的灵活编程
1.1程序运行前
在程序编译后,生成了exe可执行程序,为执行该程序前分为两个区域
代码区:
存放CUP执行的机器指令
代码是共享的,共享的目的时对于频繁被执行的程序,只需要再内存中有一份代码即可
代码是只读的,使其只读的原因是防止程序意外地修改了他的指令
全局区:
全局变量和静态变量存放于此
全局区还包含了常数区,字符串常量和其他常量也存放于此
该区域的数据再程序结束后由操作系统释放
#include <bits/stdc++.h>
using namespace std;
//全局变量
int g_a = 10;
int g_b = 10;
//全局常量
const int c_g_a = 10;
const int c_g_b = 10;
int main() {
//局部变量
int a = 10;
int b = 10;
//打印地址
cout << "局部变量a地址为: " << (int)&a << endl;
cout << "局部变量b地址为: " << (int)&b << endl;
cout << "全局变量g_a地址为: " << (int)&g_a << endl;
cout << "全局变量g_b地址为: " << (int)&g_b << endl;
//静态变量
static int s_a = 10;
static int s_b = 10;
cout << "静态变量s_a地址为: " << (int)&s_a << endl;
cout << "静态变量s_b地址为: " << (int)&s_b << endl;
cout << "字符串常量地址为: " << (int)&"hello world" << endl;
cout << "字符串常量地址为: " << (int)&"hello world1" << endl;
cout << "全局常量c_g_a地址为: " << (int)&c_g_a << endl;
cout << "全局常量c_g_b地址为: " << (int)&c_g_b << endl;
const int c_l_a = 10;
const int c_l_b = 10;
cout << "局部常量c_l_a地址为: " << (int)&c_l_a << endl;
cout << "局部常量c_l_b地址为: " << (int)&c_l_b << endl;
system("pause");
return 0;
}
总结;
- cpp中在程序运行前分为全局区和代码区
- 代码区的特点是只读和共享
- 全局区中存放全局变量,静态变量,常量
- 常量区中放const 修饰的全局常量和字符串常量
1.2 程序运行后
栈区:
由编译器自动分配释放,存放函数的参数值,局部变量等
注意事项:不要返回局部变量的地址,栈区开辟的数据由编译器自动释放
#include <bits/stdc++.h>
using namespace std;
int* func()
//int*对应的是返回的int 地址
{
int a = 10;//局部变量存放在栈上,执行完后自动释放
return &a;//返回局部变量地址
}
int main()
{
//接受func函数的返回值
int* p = func();
cout << *p << endl;//第一次可以打印是因为编译器做了保留
cout << *p << endl;
return 0;
}
堆区;
由程序员分配释放,若程序员不释放,程序结束时由操作系统回收
在 cpp中主要利用new在队中开辟内存
示例
#include <bits/stdc++.h>
using namespace std;
int* func()
{
int* a = new int(10);
return a;
}
int main()
{
int* p = func();
cout << *p << endl;
cout << *p << endl;
return 0;
}
总结:
堆区数据由程序员管理开辟和释放
堆区数据利用new关键字进行开辟内存
1.3 new操作符
cpp中利用new操作符在对去开辟数据
堆区开辟的数据,由程序员手动开辟,手动释放,释放利用操作符delete
利用new创建的数据,会返回数据对应的类型指针
#include <bits/stdc++.h>
using namespace std;
int* func()
{
int* a = new int(10);//返回指针
return a;
}
int main()
{
int* p = func();
cout << *p << endl;
cout << *p << endl;
//释放
delete p;
//cout << *p << endl;报错
return 0;
}
#include <bits/stdc++.h>
using namespace std;
int main()
{
int* arr = new int[100];
for (int i = 0; i < 10; i++)
{
arr[i] = i + 10;
}
for (int i = 0; i < 10; i++)
{
cout << arr[i] << endl;
}
delete[] arr;
return 0;
}
2. 引用
2.1 引用的基本使用
**作用:**给变量起别名
示例
:
#include <bits/stdc++.h>
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;
return 0;
}
2.2 引用注意事项
- 引用必须初始化
- 引用在初始化后,不可以改变
示例
:
#include <bits/stdc++.h>
using namespace std;
int main()
{
int a = 10;
int b = 20;
int& c = a;//引用必须初始化,且不能改变了
c = b;//这是赋值操作
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
return 0;
}
2.3 引用做函数参数
作用:函数传参时,可以利用引用技术让形参修饰实参
优点:可以简化指针修改实参
#include <bits/stdc++.h>
using namespace std;
void mySwap01(int a, int b)
{
//值传递
int temp = a;
a = b;
b = temp;
}
void mySwap02(int* a, int* b)
{
//地址传递
int temp = *a;
*a = *b;
*b = temp;
}
void mySwap03(int& a, int& b)
{
int temp = a;
a = b;
b = temp;
}
int main()
{
int a = 10;
int b = 20;
mySwap01(a, b);
cout << "a:" << a << "b:" << b << endl;
mySwap02(&a, &b);
cout << "a:" << a << "b:" << b << endl;
mySwap03(a, b);
cout << "a:" << a << "b:" << b << endl;
return 0;
}
总结:引用参数传入和按地址传入效果是一样的,引用更加简单
2.4引用做函数返回值
作用:引用时可以作为函数的返回值存在的
注意:不要返回局部变量引用
用法:函数调用作为左值
示例
#include <bits/stdc++.h>
using namespace std;
int& test01()
{
int a = 10;//局部变量
return a;
}
//返回静态变量引用
int& test02()
{
static int a = 20;//静态变量,存放在全局区,全局区上的数据在程序结束时释放
return a;
}
int main()
{
int &res = test01();
cout << res << endl;//编译器会保留一次
cout << res << endl;//这里就不可以了
int& res2 = test02();//如果函数做左值,那么必须返回引用
cout << res2 << endl;
cout << res2 << endl;
test02() = 1000;
cout << res2 << endl;
cout << res2 << endl;
return 0;
}
2.5 引用的本质
本质:引用的本质在cpp内部实现是一个指针常量
讲解示例:
#include <bits/stdc++.h>
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;
cout << a << endl;
cout << ref << endl;
func(a);
return 0;
}
结论:cpp推荐用引用技术,因为语法方便,引用本质是指针常量,但是所有指针操作编译器会帮我们做了
2.6 常量引用
**作用:**常量引用主要来修饰形参,防止误操作
在函数形参列表中,可以加const 修饰形参,防止形参改变实参
#include <bits/stdc++.h>
using namespace std;
void showValue(const int& v)
{
//v += 10;报错,
cout << v << endl;
}
int main()
{
//int& ref = 10;引用本身需要一个和法的空间,因此错误
//加入const 就可以了,编译器优化代码,int temp=10,const int &ref=temp
const int& ref = 10;
//ref = 100;//加入const 后可以不修改变量
cout << ref << endl;
int a = 10;
showValue(a);
return 0;
}
3 函数提高
3.1 函数默认参数
在cpp中,函数的形参列表中的形参可以有默认值
#include <bits/stdc++.h>
using namespace std;
int func(int a, int b = 10, int c = 10,int d=10)
{
return a + b + c+d;
}
//如果某个位置参数有默认值,那么从这个位置往后,从左向右,必须都要有默认值
//如果函数声明有默认值,函数实现的时候就不能有默认参数
int func2(int a = 10, int b = 10);
int func2(int a, int b)
{
cout << "eqwwq" << endl;
return a + b;
}
int main()
{
cout << func(20,10) << endl;//有就用实参,没有就用形参
cout << func(10) << endl;
//cout << func() << endl;
return 0;
}
3.2 函数占位参数
cpp中函数的形参列表可以有展位参数,用来做占位,调用函数时必须补齐该位置
示例
#include <bits/stdc++.h>
using namespace std;
void func(int a, int)
{
cout << "this is func" << endl;
}
int main()
{
func(10, 10);//占位参数必须补齐
system("pause");
return 0;
}
3.3函数重载
3.3.1 函数重载概述
作用:函数名可以相同,提高复用性
函数重载满足条件
- 同一个作用域下
- 函数名称相同
- 函数参数类型不同或者个数不同或者顺序不同
注意:
函数的返回值不可以作为函数重载的条件
示例
#include <bits/stdc++.h>
using namespace std;
void func()
{
cout << "func的调用" << endl;
}
void func(int a)
{
cout << "func(int a)的调用" << endl;
}
void func(double a)
{
cout << "func(double a)的调用" << endl;
}
void func(int a,double b)
{
cout << "func(int a,double b)的调用" << endl;
}
void func(double a, int b)
{
cout << "func(double a,int b)的调用" << endl;
}
//函数返回值不可以作为函数重载的条件
//int func(double a, int b)
//{
//cout << "func(double a,int b)的调用" << endl;
//}
int main()
{
func();
func(10);
func(3.14);
func(10, 3.14);
func(3.14, 10);
return 0;
}
3.3.2函数重载注意事项
- 引用作为重载条件
- 函数重载碰到函数默认参数
示例
#include <bits/stdc++.h>
using namespace std;
//函数重载注意事项
// 引用作为重载条件
void func(int& a)
{
cout << "func(int &a)的调用" << endl;
}
void func(const int& a)
{
cout << "func(const int &a)的调用" << endl;
}
//函数重载碰到函数默认参数
void func2(int a, int b = 10)
{
cout << "func2(int a, int b = 10)的调用" << endl;
}
void func2(int a)
{
cout << "func2(int a)的调用" << endl;
}
int main()
{
int a = 10;
func(a);//调用无const
func(10);//调用const
//func2(10);//出现歧义
return 0;
}