C++核心编程篇——第一篇

108 阅读4分钟

C++核心编程篇——第一篇

1、内存四区

084_01程序的内存模型-内存四区-代码区

内存分区模型 C++程序在执行时,将内存大方向划分为4给区域

  • 代码区:存放函数体的二进制代码,由操作系统进行管理的

  • 全局区:存放全局变量和静态变量以及常量

  • 栈区:由编译器自动分配释放,存放函数的参数值,局部变量等

  • 堆区:由程序员分配和释放,若程序员不释放,程序结束时由操作系统回收

  • 内存四区意义:

    不同的区域存放的数据,赋予不同的生命周期,给我们更大的灵活编程

    程序运行前

    在程序编译后,生成了exe可执行程序,未执行该程序前分为两个区域

    代码区:

    存放CPU执行的机器指令

    代码区是共享的,共享的目的是对于频繁被执行的程序,只需要在内存中有一份代码即可

    代码区是只读的,使其只读的原因是防止程序意外的修改了它的指令

085_02程序的内存模型-内存四区-全局区

全局区:

全局变量和静态变量存放在此。

全局区还包含常量区,字符串常量和其他常量也存放在此。

该区域的数据在程序结束后由操作系统释放。

#include<iostream>
#include<string>
using namespace std;
//全局变量
int g_a = 10, g_b = 20;
const int c_g_a = 30;
int main()
{
	//全局区
	//全局变量、静态变量、常量
	//创建普通局部变量
	int a = 10, b = 20;
	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, s_b = 20;
	cout << "静态变量s_a的地址为:" << (int)&s_a << endl;
	cout << "静态变量s_b的地址为:" << (int)&s_b << endl;
	//常量
	//字符串常量
	cout << "字符串常量的地址为: " << (int)&"hello world" << endl;
	//const修饰的变量
	//const 修饰的全局变量,const修饰的局部变量
	cout << "const 修饰的全局变量c_g_a的地址为:" << (int)&c_g_a << endl;
	const int c_a = 30;
	cout << "const 修饰的局部变量c_a的地址为:" << (int)&c_a << endl;
	system("pause");
	return 0;
}

在这里插入图片描述 在这里插入图片描述

总结:

  • C++中在程序运行前分为全局区和代码区
  • 代码区特点是共享和只读
  • 全局区中存放
  • 常量区存放const修饰的全局变量和字符串变量

086_03程序的模型-内存四区-栈区

程序运行后

栈区:

由编译器自动分配释放,存放函数的参数值,局部变量等

注意事项:不要返回局部变量的地址,栈区开辟的数据由编译器自动释放

#include<iostream>
#include<string>
using namespace std;
//栈区数据注意事项 --不要返回局部变量的地址
//栈区的数据由编译器管理开辟和释放

int* func(int b){     //形参数据也会放在栈区
	int a = 10;  //局部变量,存放在栈区,栈区的数据在函数执行完后自动释放
	return &a;   //返回局部变量的地址
}
int main()
{
	//接受func函数的返回值
	int *p = func(10);
	cout << *p << endl;   //第一次可以打印正确的数字,是因为编译器做了保留
	cout << *p << endl;   //第二次这个数据就不再保留了
	system("pause");
	return 0;
}

在这里插入图片描述

087_04程序的内存模型-内存四区-堆区

堆区:

由程序员分配释放,若程序员不释放,程序结束时由操作系统回收

在C++中主要利用new在堆区开辟内存

#include<iostream>
#include<string>
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;
}

在这里插入图片描述

088_05程序的内存模型-new运算符

C++中利用new操作符在堆区开辟数据

堆区开辟的数据,由程序员手动开辟,手动释放,释放利用操作符delete

语法:new 数据类型

利用new创建数据,会返回该数据对应的类型的指针

#include<iostream>
#include<string>
using namespace std;

//1、new的基本语法
int *func(){
	//在堆区创建整型数据
	//new返回的是该数据类型的指针
	int *p =new int(10);
	return p;
}
static void test01(){
	int *p = func();
	cout << *p << endl;
	cout << *p << endl;
	//堆区的数据由程序员管理开辟,程序员管理释放
	//如果想要释放堆区的数据,利用关键字delete
	delete p;
	//cout << *p << endl;//内存已经被释放,再次访问就是非法操作,会报错
}
//2、在堆区利用new开辟数组
static void test02(){
	//创建10个整型数据的数组,在堆区
	int *arr = new int[10];   //10代表数组中有10个元素
	for (int i = 0; i < 10; i++){
		arr[i] = i + 100;     //给10个元素赋值 100 ~ 109
	}
	for (int i = 0; i < 10; i++){
		cout << *arr << "  ";
		arr++;
	}
	cout << endl;
	//释放堆数组
	//释放数组的时候要加[]才可以
	delete[] arr;
}
int main()
{
	test01();
	test02();
	system("pause");
	return 0;
}

2、引用

089_06C++中引用的基本语法

引用的基本使用

作用:给变量起别名

语法:数据类型 &别名 = 原名

#include<iostream>
#include<string>
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;
}

在这里插入图片描述

090_07C++中的引用-引用的注意事项

引用注意事项

  • 引用必须初始化

  • 引用在初始化后,不可以改变

    在这里插入图片描述

#include<iostream>
#include<string>
using namespace std;
int main()
{
	//引用的注意事项
	//引用必须要初始化
	int a = 10;
	//int &b;
	int &b = a;
	//引用一旦初始化后就不可以更改
	int c = 20;
	 b = c; //赋值操作,而不是更改引用
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
	cout << "c = " << c << endl;
	system("pause");
	return 0;
}

091_08C++中的引用-引用做函数参数

作用:函数传参时,可以利用引用的技术让形参修饰实参 优点:可以简化指针修改实参

#include<iostream>
#include<string>
using namespace std;
//值传递
static void mySwap01(int a, int b){
	int temp = a;
	a = b;
	b = temp;
}
//指针传递
static void mySwap02(int *a, int *b){
	int temp = *a;
	*a = *b;
	*b = temp;
}
//引用传递
static void mySwap(int &a, int &b){
	int temp = a;
	a = b;
	b = temp;
}
int main()
{
	int a = 10, b = 20;
	mySwap(a, b);
	cout << "a = " << a << endl;
	cout << "b = " << b << endl;
	system("pause");
	return 0;
}

总结:通过引用参数产生 的效果同按地址传递是一样的,引用的语法更简单。

093_10C++中的引用-引用做函数返回值

  • 作用:引用是可以作为函数的返回值存在的
  • 注意:不要返回局部变量的引用
  • 用法:函数调用作为左值

在这里插入图片描述

#include<iostream>
#include<string>
using namespace std;
//引用做函数的返回值
//1、不要返回局部变量的引用
static int& test01(){
	int a = 10;   //局部变量存放在四区中的  栈区
	return a;
}
//2、函数的调用可以作为左值
static int& test02(){
	//静态变量,存放在全局区,全局区上的数据在程序结束后系统释放
	static int a = 10;
	return a;
}
int main()
{
	int &ref = test01();
	cout << " ref = " << ref << endl;    //第一次结果正确,是因为编译器做了保留
	cout << " ref = " << ref << endl;    //第二次结果错误,是因为a的内存已经被释放了
	int &ref2 = test02();
	test02() = 1000;
	cout << " ref2 = " << ref2 << endl;
	cout << " ref2 = " << ref2 << endl;
	system("pause");
	return 0;
}

093_10C++中的引用-引用的本质

本质:引用的本质在C++内部实现就是一个指针常量

在这里插入图片描述 在这里插入图片描述

094_11C++中的引用-常量引用

作用:常量引用主要用来修饰形参,防止误操作

在函数形参列表中,可以加const修饰形参, 防止形参改变实参

#include<iostream>
#include<string>
using namespace std;
static void showValue(const int &val){
	//val = 1000;
	cout << "val = " << val << endl;
}
int main()
{
	//常量引用
	//使用场景,用来修饰形参,防止误操作
	int a = 10;
	//加上const之后 编译器将代码修改为 int temp = 10; const int &ref = temp;
	const int &ref = 10;  //引用必须引一块合法的内存空间
	//ref = 30;    //加入const之后变为只读,不可修改
	int b = 100;
	showValue(b);
	system("pause");
	return 0;
}

3、函数高级

095_12函数高级-函数的默认参数

  • 在C++中,函数的形参列表中的形参是可以有默认值的
  • 语法:返回值类型 函数名 (参数 = 默认值){}
#include<iostream>
#include<string>
using namespace std;
//函数的默认参数 
//如果我们自己传入数据,就用自己的数据,如果没有,那么就用默认值
//语法:返回值类型  函数名(形参 = 默认值){}
static  int func(int a, int b =20 , int c = 30){
	return a + b + c;
}
//注意事项
//1、如果某个位置已经有了默认参数,
//那么从这个位置往后,从左到右都必须右默认参数
//2、如果函数的申明有了默认参数,函数实现就不能有默认参数
int main()
{
	cout << func(10,30) << endl;
	system("pause");
	return 0;
}

096_13函数高级-函数的占位参数

  • C++中函数的形参列表里可以有占位参数,用来占位,调用函数的时候必须填补该位置
  • 语法:返回值类型 函数名(数据类型){}
#include<iostream>
#include<string>
using namespace std;
//占位参数
//占位参数还可以有默认参数
static void func(int a, int =20){
	cout << "This is func" << endl;
}
int main()
{
	func(10, 2);
	system("pause");
	return 0;
}

097_14函数高级-函数重载-基本语法

作用:函数名可以相同,提高复用性

函数重载满足条件:

  • 同一个作用域下

  • 函数名称相同

  • 函数 参数类型不同,或者个数不同或者顺序不同

    注意:函数的返回值不可以作为函数重载的条件

#include<iostream>
#include<string>
using namespace std;
//函数重载满足的条件
//1、同一个作用域下
//2、函数名称相同
//3、函数参数类型不同,或者个数不同,或者顺序不同
static void func(){
	cout << "This is func" << endl;
}
static void func(int a){
	cout << "This is funcfunc(int a)" << endl;
}
static void func(double a){
	cout << "This is func(double a)" << endl;
}
static void func(double a,int b){
	cout << "This is func(double a,int b)" << endl;
}
static void func(int a, double b){
	cout << "This is func(int a, double b)" << endl;
}
int main()
{
	func();
	func(2);
	func(2.1);
	func(2.1, 2);
	func(2, 2.1);
	system("pause");
	return 0;
}

098_15函数高级-函数重载-注意事项

  • 引用作为重载的条件

  • 函数重载碰到函数默认参数

    //当函数重载碰到了默认参数,出现二义性,报错,尽量避免

#include<iostream>
#include<string>
using namespace std;
//函数重载的注意事项
//1、引用作为重载的条件
static void func(int &a){      //int &a = 10;  不合法
	cout << "func(int &a)调用" << endl;
}
static void func(const int &a){
	cout << "func(const int &a)调用" << endl;
}
//2、函数重载碰到了默认参数
static void func2(int a, int b = 10){
	cout << "func2(int a, int b = 10)的调用" << endl;
}
static void func2(int a){
	cout << "func2(int a)的调用" << endl;
}

int main()
{
	const int a = 10;
	func(a);
	int b = 10;
	func(b);
	func(10);
	//func2(10); //当函数重载碰到了默认参数,出现二义性,报错,尽量避免
	system("pause");
	return 0;
}