文章目录
1.内存分区模型
c++程序执行时,将内存大方向划分为4个区域
- 代码区:存放函数体的二进制代码,由操作系统进行管理
- 全局区:存放全局变量和静态变量以及常量
- 栈区:有编译器自动分配和释放,存放函数体的参数值和局部变量等
- 堆区:由程序猿分配和释放,若程序猿不释放,程序结束时由操作系统回收
内存四个区域的意义:
不同的区域存放不同的数据,给数据赋予不同的生命周期,更加灵活方便的编程。
1.1 程序运行前
在程序编译后,生成了exe可执行程序,未执行该程序前分为两个区域:
代码区:
存放CPU执行的机器指令
代码区是共享的,共享的目的是对于频繁执行的程序,只需要在内存中有一份代码即可
代码区是只读的,使其只读的原因是防止程序意外的修改了它的指令
全局区:
存放全局变量和静态变量
全局区还包含了常量区,字符串常量和其他常量也存放在此
该区域的数据在程序结束后由操作系统释放

#include<iostream>
using namespace std;
//全局变量
int g_a = 12;
int g_b = 15;
//const修饰的全局变量,全局常量
const int c_g_a = 10;
const int c_g_b = 10;
int main() {
//全局变量、静态变量、常量
//创建普通局部变量
int a = 14;
int b = 23;
cout << "局部变量a的地址为:" << (int)&a << endl;
cout << "局部变量a的地址为:" << (int)&b << endl;
cout << "全局变量g_a的地址为:" << (int)&g_a << endl;
cout << "全局变量g_b的地址为:" << (int)&g_b << endl;
//静态变量,在普通变量前面加static,属于静态变量
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;
//const修饰的变量
//const修饰的全局变量,const修饰的局部变量
cout << "全局常量c_g_a的地址为:" << (int)&c_g_a << endl;
cout << "全局常量c_g_a的地址为:" << (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;
//
}

1.2 程序运行后
栈区:
由编译器自动分配释放,存放函数的参数值,局部变量等
注意事项:不要返回局部变变量的地址,栈区开辟的数据由编译器自动释放。
#include<iostream>
using namespace std;
//栈区数据注意事项:不要返回局部变量的地址
//栈区的数据有编译器管理开辟和释放
int* func() {
int a = 10;//局部变量,存在栈区,栈区的数据在函数执行完毕后自动释放
return &a;//不要返回局部变量内存地址
}
int main() {
//接收func函数的返回值
int* p = func();
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;
system("pause");
return 0;
}
1.3 new操作符
C++中使用new操作符在堆区中开辟数据
堆区开辟的数据,由程序猿手动开辟,手动释放,释放利用操作符delete
#include<iostream>
using namespace std;
//1.new的基本语法
int* func() {
//在堆区创建整形的数据
//new返回的是该数据类型的指针
int* p = new int(10);
return p;
}
//2.在堆区利用new开辟数组
void test2() {
//创建10整形数据的数组,在堆区
int * arr = new int[10];//10代表数组有10个元素
for (int i = 0; i < 10; i++) {
arr[i] = i + 100;//给十个元素赋值
}
for (int i = 0; i < 10; i++) {
cout << arr[i] << endl;
}
//释放堆区的数组
//释放数组的时候要加[]
delete[] arr;
}
int main() {
int* p = func();
cout << *p << endl;
cout << *p << endl;
cout << *p << endl;
//堆区的数据由管理员开辟和释放
//如果想释放堆区数据,利用关键字delete
//delete p;
cout << *p << endl;
test2();
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 = 1023;
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
2.2 引用的注意事项
- 引用必须要初始化
- 引用一旦初始化后,就不可以更改了
#include<iostream>
using namespace std;
int main() {
int a = 10;
//1. 引用必须要初始化
int &b = a;
//2. 引用一旦初始化后,就不可以更改了
int c = 20;
b = c;//赋值操作,而不是改引用
cout << "a = " << a << endl;
cout << "b = " << b << endl;
cout << "c = " << c << endl;
}

2.3 引用做函数参数
作用: 函数传参时,可以使用引用的技术让形参修饰实参
优点: 可以简化指针修改实参
#include<iostream>
using namespace std;
//交换函数
//1.值传递
void mySwap01(int a, int b) {
int temp = a;
a = b;
b = temp;
cout << "mySwap01: a = " << a << endl;
cout << "mySwap01:b = " << b << endl;
}
//2.地址传递
void mySwap02(int* a, int* b) {
int temp = *a;
*a = *b;
*b = temp;
cout << "mySwap02: a = " << *a << endl;
cout << "mySwap02:b = " << *b << endl;
}
//3.引用传递
void mySwap03(int &a,int &b) {
int temp = a;
a = b;
b = temp;
cout << "mySwap01: a = " << a << endl;
cout << "mySwap01:b = " << b << endl;
}
int main() {
int a = 20;
int b = 30;
mySwap03(a, b);//引用传递,形参也会修饰实参
cout << "a = " << a << endl;
cout << "b = " << b << endl;
}
2.4 引用做函数返回值
作用: 引用是可以作为函数的返回值存在的
注意: 不要返回局部变量引用
用法: 引用调用作为左值
示例:
#include<iostream>
using namespace std;
//引用作为函数的返回值
//1.不要返回局部变量的引用
int& test01() {
int a = 10;//局部变量存在在四区中栈区
return a;
}
//2.函数的调用可以作为左值
int& test02() {
static int a = 10;//静态变量存在在全局区,全局区的数据在程序结束后由系统自动释放
return a;
}
int main() {
int& ref = test01();
cout << "ref = " << ref << endl;
cout << "ref = " << ref << endl;
int& ref2 = test02();
cout << "ref2 = " << ref2 << endl;
cout << "ref2 = " << ref2 << endl;
//函数的调用作为左值
test02() = 1000;//如果函数的返回值是一个引用,这个函数调用可以作为左值
cout << "ref2 = " << ref2 << endl;
cout << "ref2 = " << ref2 << endl;
system("pause");
return 0;
}

2.5 引用的本质
本质: 引用的本质在c++内部实现是一个指针常量
引用一旦初始化吉不能改变,原因是引用的本事是一个const修饰的指针常量,由于使用const修饰,所以不能改变。
结论: c++推荐使用引用技术,因为语法方便,引用的本质是指针常量,但是所有指针的从操作编译器都帮我们做了。
2.6 常量引用
作用: 常量引用主要用来修饰形参,防止误操作。
在形参列表中,可以加const修饰形参,防止形参改变实参
示例:
#include<iostream>
using namespace std;
//打印数据
void showVale(const int& val) {
cout << "vale = " << val << endl;
}
int main() {
//常量引用
//使用场景:用来修饰形参,防止误操作
//int a = 10;
//加上const后 编译器将代码修改, int temp = 10;const int & ref = temp;
const int& ref = 10;//引用必须引一块合法的内存空间,加入const之后变为只读,不可修改
int a = 1000;
showVale(a);
cout << "a = " << a << endl;
system("pause");
return 0;
}
3.函数提高
3.1 函数默认参数
在C++中,函数的形参列表中形参是可以有默认值的
语法:返回值类型 函数名 (参数 = 默认值)
示例:
#include<iostream>
using namespace std;
//函数默认参数
//如果传入了数据,就用传入的值,如果没有传入,则用默认值
int func(int a, int b = 20, int c = 30) {
return a + b + c;
}
//注意事项:
//1、如果某个位置已经有了默认参数,那么这个位置往后从左到右的参数都必须有默认值
//2、如果函数的申明有了默认参数,则函数的实现不能有默认参数,申明和实现只能有一个有默认参数
int func2(int a = 10 , int b = 109 );
int func2(int a, int b ) {
return a + b;
}
int main() {
int result = func(10,50);
int result2 = func2(10, 10);
cout << "结果为:" << result << endl;
cout << "结果为:" << result2 << endl;
system("pause");
}
3.2 函数占位参数
C++中函数的形参列表可以有占位参数,用来做占位,调用函数时必须填补该位置
语法:返回值类型 函数名 (函数类型){}
示例:
#include<iostream>
using namespace std;
//函数占位参数,占位参数也可以有默认值
//目前阶段的占位参数 我们还用不到,后面课程会用到
//占位参数还可以有默认参数
void func(int a, int = 10) {
cout << "this is function!\n";
}
int main() {
func(10);
system("pause");
}
3.3 函数重载
3.3.1 函数重载概述
作用: 函数名可以相同,提高复用性
函数重载满足条件:
- 同一个作用域下
- 函数名称相同
- 函数参数类型不同或者个数不同或者顺序不同
注意: 函数的返回值不可以作为函数重载的条件
示例:
3.3.2 函数重载注意事项
- 引用作为函数重载
- 函数重载碰到函数默认参数
#include <iostream>
#include <string>
using namespace std;
//函数重载的注意事项
//1.引用作为函数重载的条件
void func(int& a) {//int &a = 10 不合法
cout << "函数func(int& a)调用" << endl;
}
void func(const int& a) {//const int &a = 10;
cout << "函数func(const int& a)调用" << endl;
}
//2.函数重载碰到默认参数
void func2(int a, int b = 10) {
cout << "函数func2(int a,int b)调用" << endl;
}
void func2(int a) {
cout << "函数func2(int a)调用" << endl;
}
int main() {
//int a = 10;
//func(a);
//func(10);
func2(10, 20);//当函数重载遇到默认参数,会出现二义性,报错,尽量避免这种情况
system("pause");
}
4.类和对象
c++面向对象的三大特性:封装、继承、多态
c++认为完事万物接对象,对象上有其属性和行为
4.1 封装
4.1.1 封装的意义
封装是C++面向对象三大特性之一
封装的意义:
- 将属性和行为作为一个整体,表现生活中的事务
- 将属性和行为加以权限控制
封装的意义:
在设计类的时候,属性和行为写在一起,表现事务
语法:
class 类名{
访问权限:属性/行为
}
C++内存管理与函数高级技巧
1252

被折叠的 条评论
为什么被折叠?



