0
点赞
收藏
分享

微信扫一扫

03_ESP32+MicroPython 连接WIFI

mafa1993 2024-06-22 阅读 36

 一、内存模型分区

内存四区的意义:

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

(一)程序运行前

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

代码区:

  • 存放cpu执行的机器指令(代码的2进制)
  • 代码区是共享的,共享的目的是对于频繁执行的程序,只需要在内存中有一份代码即可
  • 代码区是只读的,使其只读的原因是防止程序意外的修改了其它指令

全局区:

  • 全局变量和静态变量存放在此
  • 全局区还包含了常量区,字符串常量和其他常量也存放在此
  • 该区域的数据在程序结束后由操作系统释放

#include<iostream>
using namespace std;
int g_a = 10;
int g_b = 10;
//const修饰的全局变量,全局常量
int c_g_a = 10;
int c_g_b= 10;
int main()
{
//全局区
//全局变量、静态变量、常量

//创建普通局部变量
int a = 10;
int b = 20;
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 int s_c = 10;
static int s_d = 10;
cout << "静态变量s_c的地址为:" << (int)&s_c << endl;
cout << "静态变量s_d的地址为:" << (int)&s_d << endl;

//常量
//字符串常量
cout<<"字符串常量的地址为:"<<(int) & "hello world"<<endl;
//const修饰的变量
//const 修饰的全局变量 
// const修饰的局部变量
const int c_l_a = 10;
const int c_l_b = 10;
cout << "const修饰的全局常量c_g_a的地址为:" << (int)&c_g_a << endl;
cout << "const修饰的全局常量c_g_b的地址为:" << (int)&c_g_b << endl;
cout << "const修饰的局部变量c_l_a的地址为:" << (int)&c_l_a << endl;
cout << "const修饰的局部变量c_l_b的地址为:" << (int)&c_l_b << endl;
system("pause");
return 0;

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

int* func(int b) {//形参数据也会放在栈区
	b = 200;
	int a = 10;//局部变量 存放在栈区,栈区的数据在函数执行完后自动释放
	return  & a;//返回局部变量的地址

}
int main() {
	//接受func函数的返回值
	int *p = func(1);
	cout << "p = " << *p << endl;
	cout << "p = " << *p << endl;
	
	system("pause");
	return 0;
}

(二)程序运行后

栈区:

  • 由编译器自动分配释放,存放函数的参数值,局部变量等
  • 注意事项:不要返回局部变量的地址,栈区开辟的数据由编译器自动释放
#include <iostream>
using namespace std;
//栈区数据的注意事项 ---不要返回局部变量的地址
//栈区的数据由编译器管理开辟和释放

int* func(int b) {//形参数据也会放在栈区
	b = 200;
	int a = 10;//局部变量 存放在栈区,栈区的数据在函数执行完后自动释放
	return  & a;//返回局部变量的地址

}
int main() {
	//接受func函数的返回值
	int *p = func(1);
	cout << "p = " << *p << endl;
	cout << "p = " << *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=" << *p << endl;
	system("pause");
	return 0;
}

 

(三)new操作符

  • c++中利用new操作符在堆区开辟数据
  • 堆区开辟的数据,由程序员手动开辟,手动释放,释放利用操作符 delete
  • 语法: new 数据类型
  • 利用new创建的数据,会返回该数据对应的类型的指针

没有进行delete清除时

 使用delete进行清除后

#include <iostream>
using namespace std;
//1、new的基本语法
int* func() {
	//在堆区创建整形数据
	//new返回的是一个该数据类型的指针
	int *p = new int(10);
	return p;

}
//2、在堆区利用new开辟数组
void test02() {
	//创建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; //释放数组需要加[]
}
void test01()
{
	int* p = func();
	cout << *p << endl;
	cout << *p << endl;
	cout << *p << endl;
	//堆区的数据 由程序员管理开辟,程序员管理释放
	//利用关键字delete释放堆区数据
	delete p;

}
int main()
{
	test01();
	test02();
	system("pause");
	return 0;

}

二、引用

(一)引用的基本使用

作用:给变量起别名

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

#include<iostream>
using namespace std;

int main() {
	//引用的基本语法
	//数据类型 &别名 =原名
	int a = 10;
	int& b = a; //b是a的引用
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;
	b = 20; //修改b的值,a也会跟着变化
	cout << "a=" << a << endl;
	cout << "b=" << b << endl;


	system("pause");
	return 0;

}

(二)引用的注意事项

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

#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; //输出20
	cout << "b=" << b << endl; //输出20
	cout << "c=" << c << endl; //输出20

	//3、引用的本质是指针,因此可以直接操作指针指向的内存空间
	system("pause");
	return 0;

}

(三)引用做函数参数

  • 作用:函数传递时,可以利用引用的技术让形参修饰实参
  • 优点:可以简化指针修改实参
#include <iostream>
using namespace std;
//交换函数

//1.值传递
void mySwap01(int a,int b) {
	int temp = a;
	a = b;
	b = temp;
}
//2.地址传递
void mySwap02(int* a, int* b) {
	int temp = *a;
	*a = *b;
	*b = temp;
}
//3.引用传递
void mySwap03(int &a, int &b) {
	int temp = a;
	a =b;
	b = temp;
}

int main() {
	int a = 10, b = 20;
	cout << "a = " << a << " b = " << b << endl;
	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;
	system("pause");
	return 0;
}

(四)引用做函数返回值

作用:引用是可以作为函数的返回值存在

注意:不要返回局部变脸引用

用法:函数调用作为左值

#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 << endl; //第一次结果正确,是因为编译器做了保留
	cout <<"ref="  << ref << endl;//第二次结果错误,是因为a的内存已经释放
	cout << ref << endl;
	cout << ref << endl;
	cout << ref << endl;
	cout << ref << endl;

	int &ref2 = test02();
	cout << ref2 << endl;
	cout << ref2 << endl;
	cout << ref2 << endl;
	test02() = 1000;//如果函数的返回值是引用,这个函数调用可以作为左值
	cout << "ref2=" << ref2 << endl;
	cout << "ref2=" << ref2 << endl;
	cout << "ref2=" << ref2 << endl;

	system("pause");
	return 0;
}

(五)引用的本质

本质:引用的本质在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; //a=20
	cout << "ref=" << ref << endl;

	system("pause");
	return 0;
}

(六)常量引用

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

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

不加const修饰 

#include <iostream>

using namespace std;
//打印数据的函数
void showValue( int & val){
	val = 1000;
	cout << "value =" << val << endl;
}
int main() {
	//常量引用
	//使用场景 用来修饰形参,防止误操作
	int a = 10;
	//加上const之后 编译器将代码修改 int temp=10;const int &ref=temp;

	const int& ref = 10;//引用必须引一块合法的内存空间
	//ref = 20;//加入const之后变为只读,不可以修改
	cout << "ref =" << ref << endl;
	int b = 100;
	showValue(b);//调用函数传递引用
	system("pause");
	return 0;
}

加入const修饰

#include <iostream>

using namespace std;
//打印数据的函数
void showValue(const int & val){
	//val = 1000; //加入const 后会报错 不能修改
	cout << "value =" << val << endl;
}
int main() {
	//常量引用
	//使用场景 用来修饰形参,防止误操作
	int a = 10;
	//加上const之后 编译器将代码修改 int temp=10;const int &ref=temp;

	const int& ref = 10;//引用必须引一块合法的内存空间
	//ref = 20;//加入const之后变为只读,不可以修改
	cout << "ref =" << ref << endl;
	int b = 100;
	showValue(b);//调用函数传递引用
	system("pause");
	return 0;
}
举报

相关推荐

0 条评论