[虎头虎脑,没有烦恼]c++内存四区,new,引用,函数

⭐️大一小何,还在学习当中,欢迎交流指正~ 

新年快乐,愿万事胜意,不负韶华。

目录

代码区

全局区

堆区

栈区

new运算符

引用

引用的基本使用

引用的的注意事项 

引用做函数参数 

 值传递

地址传递

 引用传递

引用做函数返回值

引用的本质 

常量引用 

 函数提高

函数的默认参数 

函数的占位参数

函数重载

基本语法

注意事项

结语


代码区

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

存放CPU执行的机器指令


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

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

全局区

存放全局变量和静态变量

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

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

不在全局区中:局部变量,const修饰的局部变量(局部常量)

在全局区中:全局变量,静态变量 (static关键字),常量(字符串常量,const修饰的全局常量)

#include<iostream>
using namespace std;
//全局变量
int g_a = 5;
int g_b = 5;

//全局常量
const int c_g_a = 5;
const int c_g_b = 5;

int main()
{
	//局部变量
	int a = 5;
	int b = 5;
	cout << "局部变量a的地址:" << (int)&a << endl;
	cout << "局部变量b的地址:" << (int)&b << endl;
	
	cout << "*****************************" << endl;

	cout << "全局变量g_a的地址:" << (int)&g_a << endl;
	cout << "全局常量g_b的地址:" << (int)&g_b << endl;

	cout << "*****************************" << endl;
    //静态变量
	static int s_a = 5;
	static int s_b = 5;

	cout << "静态变量s_a的地址:"<<(int )&s_a << endl;
	cout << "静态变量s_b的地址:"<< (int)&s_b << endl;

	cout << "*****************************" << endl;
	
	cout << "字符串常量地址:" <<(int )&"xiaohe" << endl;
	cout << "字符串常量地址:" <<(int )&"xiaoheh" << endl;

	cout << "*****************************" << endl;

	cout << "全局常量c_g_a的地址:" << (int)&c_g_a << endl;
	cout << "全局常量c_g_b的地址:" << (int)&c_g_b << endl;

	cout << "*****************************" << endl;
    //局部常量
	const int c_l_a = 5;
	const int c_l_b = 5;

	cout << "局部常量c_l_a的地址:" << (int)&c_l_b << endl;
	cout << "局部常量c_l_b的地址:" << (int)&c_l_b << endl;


	
	system("pause");
	return 0;
}

即 打对号的两个部分存放在栈区 ,其他的均放在全局区(地址值差值小可以体现)

堆区

是由操作者释放,若操作者不释放,程序结束时由操作系统回收

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

#include<iostream>
using namespace std;
int * func() {
	//利用new关键字,可以将数据开辟到堆区
	//指针本身也是局部变量,放在栈上,指针保存的数据放在堆区

	int*p = new int(10);
	return p;
}

int main() {
	//在堆区开辟数据
	int* p = func();
	cout << *p << endl;//10
	cout << *p << endl;//10
	cout << *p << endl;//10
	cout << *p << endl;//10

	system("pause");
	return 0;
}

栈区

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

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


#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;
}

new运算符

#include<iostream>
using namespace std;

//1.new的基本语法

int* func()

{//new返回的是该数据类型的指针
//new返回的是 该数据类型的指针

	int * p = new int(10);
	return p;
}
void test01()
{
	int* p = func();
	cout << *p << endl;
	cout << *p << endl;
	cout << *p << endl;
	cout << *p << endl;
	//堆区的数据,由程序员管理开辟,程序员管理释放
	//如果想要释放堆区的数据,利用关键字delete
	delete p;
	
}

//2.在栈区利用new开辟数组
void test02()
{
	//创建十个整型数据的数组,在堆区
	int * arr = new int[10];
	for (int i = 0;i < 10;i++)
	{
		arr[i] = i + 100;//给十个元素赋值 100~109

	}
	for (int i = 0;i < 10;i++)
	{
		cout << arr[i] << endl;
	}
	//释放堆区数组
	//释放数组的时候,要加[]才可以
	delete[] arr;
}
int main() {
	
	//test01();
	test02();
	system("pause");

	return 0;

}

引用

引用的基本使用

作用:给变量起别名

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

#include<iostream>
using namespace std;

int main() {
	int a = 10;
	int& b = a;
	cout << a << endl;//10
	cout << b << endl;//10
	b = 20;
	cout << a << endl;//20
	cout << b << endl;//20
	system("pause");
	return 0;
}

a与b操纵的是同一块地址 

引用的的注意事项 

1.引用必须要初始化

int &b;//哒咩,这是错误的

2.引用一旦初始化后,就不可以更改了;

#include<iostream>
using namespace std;

int main() {
	int a = 10;
	//引用必须初始化
	//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

	system("pause");
	return 0;
}

引用做函数参数 

 值传递

#include<iostream>
using namespace std;
//交换函数
//1.值传递
void mySwap01(int a,int b){
	int temp = a;
	a = b;
	b = temp;
	cout << "swap01 a =" << a << endl;//20
	cout << "swap01 b =" << b << endl;//10
}
//形参数值改变,实参数值不变

int main() {
	int a = 10;
	int b = 20;
	mySwap01(a, b);	//值传递,形参不会修饰实参
	cout << " a =" << a << endl;//10
	cout << " b =" << b << endl;//20

	system("pause");
	return 0;
}

地址传递

#include<iostream>
using namespace std;
//交换函数

//2.地址传递
void mySwap02(int* a, int* b) {
	int temp = *a;
	*a = *b;
	*b = temp;
}

//3.引用传递

int main() {
	int a = 10;
	int b = 20;
	mySwap02(& a, & b);	//地址传递,形参会修饰实参的
	cout << " a =" << a << endl;//20
	cout << " b =" << b << endl;//10

	system("pause");
	return 0;
}

 引用传递

引用的作用:函数传参时,可以利用引用的技术让形参修饰实参

优点:可以简化指针修改实参

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

#include<iostream>
using namespace std;
//交换函数


//3.引用传递
void mySwap03(int &a,int &b) {
	int temp = a;
	a = b;
	b = temp;

}

int main() {
	int a = 10;
	int b = 20;
	mySwap03(a,b);//引用传递,形参会修饰实参
	cout << " a =" << a << endl;//20
	cout << " b =" << b << endl;//10

	system("pause");
	return 0;
}

上面的“a”相当于下面a数据的别名,对上面的ab改变可以操控下面的数据 

引用做函数返回值

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

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

用法:函数调用作为左值

第一个和第二个输出结果会因编译器的不同从而造成输出结果不同的!

像vs2022,在运行之前就把a的内存释放了,其他版本的vs做的处理也不尽相同

//返回局部变量引用
int& test01() {
    int a = 10; //局部变量      栈区:函数体结束释放
    return a;
}
​
//返回静态变量引用
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 << "ref2 = " << ref2 << endl;//20
    cout << "ref2 = " << ref2 << endl;//20
​
    test02() = 1000;//如果函数的返回值是个引用,这个函数调用可以作为左值
    //相当于a的引用test02()等于1000
​
    cout << "ref2 = " << ref2 << endl;//1000
    cout << "ref2 = " << ref2 << endl;//1000
​
    system("pause");
​
    return 0;
}

引用的本质 

#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 << "a = " << a << endl;

	func(a);
	return 0;
}

结论:c++推荐用引用技术,因为语法方便,引用本质是指针常量,但是所有的指针操作编译器都可以做;

常量引用 

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

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

#include<iostream>
using namespace std;
//打印数据函数
void showValue(const int& val) {
	//val = 1000;有const修饰,val量不可改变
	cout << "val = " << val << endl;

}
int main() {
	//常量引用
	//使用场景,用来修饰形参,防止误操作
	//int a = 10;
	// //int& ref = 10;哒咩,引用本身需要一个合法的内存空间,因此错误!
	//加上const之后,编译器将代码修改 int temp = 10;const int& ref = temp;编译器会优化代码
	const int& ref = 10;
	//ref = 100;哒咩,加入const后不可以修改变量
	int a = 100;
	showValue(a);
	cout << "a = " << a << endl;
	system("pause");
	return 0;
}

 函数提高

函数的默认参数 

在c++中,函数的形参列表是可以有默认值的

语法:返回值类型  函数名  (参数 = 默认值){}

 

#include<iostream>
using namespace std;

//函数的默认参数
//如果我们自己传入数据,就用自己的数据,如果没有,那么就用默认值
//语法:  返回值类型  函数名  (形参 = 默认值){}
int func1(int a,int b = 20,int c = 30) {
	return a + b + c;
}
//注意事项
//1.如果某个位置已经有了默认参数,那么从这个位置往后,从左到右都必须有默认值
//int func2(int a=10, int b, int c) {
//	return a + b + c;哒咩的,a有了默认参数,b和c一定要有默认参数
//}
//2.如果函数声明有默认参数,函数实现就不能有默认参数
//声明和实现只能有一个有默认参数
int func2(int a=10,int b=10);

int func2(int a,int b) {
	return a + b;
}
int main() {

	cout << func2() << endl;
	system("pause");
	return 0;
}

函数的占位参数

c++中函数的形参列表里可以有占位参数,用来做占位,调用函数时必须填补该位置

语法:返回值类型  函数名  (数据类型){}

 

#include<iostream>
using namespace std;

//占位参数
//返回值类型  函数名 (数据类型){}
//占位参数还可以有默认参数
void func(int a,int ) {
	cout << "this is func" << endl;

}
void func2(int a,int =10) {
	cout << "this is func2" << endl;

}
int main() {
	func(10,10);
	func2(10);
	system("pause");
	return 0;
}

函数重载

基本语法

函数重载概述

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

函数重载满足条件:

同一个作用域下

函数名称相同

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

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

#include<iostream>
using namespace std;

//函数重载
//可以让函数名相同,提高复用性
//函数重载的满足条件
//1.同一个作用域下
//2.函数名称相同
//3.函数参数类型不同,或者个数不同,或者顺序不同
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);
	system("pause");
	return 0;
}

 

注意事项

引用作为函数重载的条件

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

 

#include<iostream>
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 = 10 )" << endl;
}
void func2(int a) {
	cout << "func2(int a )" << endl;
}
int main() {
	//1.引用作为重载的条件
	int a = 10;
	func(a);//func(int &a)的调用
	func(10);//func(const int &a)的调用
	

	//2.函数重载碰到默认参数
	func2(10);//哒咩!当函数重载碰到默认参数,出现二义性(歧义),会报错,尽量避免这种情况

	system("pause");
	return 0;
}

结语

冲冲冲~

  • 18
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 14
    评论
评论 14
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

超级小何

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值