核心编程笔记 p84—p98
文章目录
内存四区—代码区、全局区、栈区、堆区
(通过不同区的管理,提高管理、运行效率)
代码区
存放函数体的二进制代码,由操作系统进行管理
我们键入的二进制代码,在程序运行前,编译后产生exe可执行程序。
1、共享:内存里有一份就行啦,防止资源浪费
(公告一份就可以让大家看到内容和要求)
2、只读:防止被意外修改
(公告是规定好了的,贴在墙上大家都不可以改)
全局区:
存放全局变量和静态变量以及常量
存放全局变量、静态变量(static修饰的全局变量或者局部变量)、常量(字符串常量,const修饰的全局变量)
栈区:
由编译器自动分配释放, 存放函数的参数值,局部变量等
注意事项:不要返回局部变量的地址,栈区开辟的数据由编译器自动释放
堆区:
由程序员分配和释放,若程序员不释放,程序结束时由操作系统回收
在C++中主要利用new在堆区开辟内存
几种变量
全局变量、局部变量、静态变量、全局常量、局部常量、全局常量、局部常量
//全局变量 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;
}
new操作符
堆区开辟的数据,由程序员手动开辟 new,手动释放,释放利用操作符 delete
语法: new 数据类型
利用new创建的数据,会返回该数据对应的类型的指针
int* func()//必须是int* 要与返回值p匹配
{
//利用new关键字,可以将数据开辟到堆区
//指针 本身就是局部变量(在主函数以外定义的变量),指针保存的数据因为有new所以是放在堆区
int* p = new int(10);
//new 完了以后并不是把数据本身给*p,而是把堆区对应的地址反映给*p
//new完以后是指针 需要用*p来接收,并且类型应该相同
return p;
}
void test01()
{
int *p = func();//在堆区开辟数据
cout << *p << endl;
cout << *p << endl;
cout << *p << endl;
delete p;//用delete释放*p这块内存
//cout << *p << endl;//再次访问即为非法操作 会报错
}
//在堆区开辟数组
void test02()
{
//在堆区创建十个整型数据的数组
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] << " ";
}
delete[] arr;//释放数组时 要加[]才可以,告诉编译器这是一个数组
}
int main()
{
//test01();
test02();
system("pause");
system("cls");
}
引用:给变量起别名
引用的基本语法
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");
system("cls");
}
引用的注意事项
int main()
{
int a = 10;
int &b = a;
//引用必须初始化
//int &b;
//引用在初始化后,不可以改变
int c = 20;
//int &b = c;
b = c;//这里是赋值 而不是更改引用
cout << "a=" << a << endl;
cout << "b=" << b << endl;
cout << "c=" << c << endl;
system("pause");
system("cls");
}
引用做函数参数
//交换函数
//1、值传递
void swap01(int a,int b)
{
int temp = a;
a = b;
b = temp;
cout << "swap01定义函数 a=" <<a<< endl;
cout << "swap01定义函数 b=" << b<<endl;
}
//地址传递
void swap02(int *a, int*b)
{
int temp = *a;
*a =* b;
*b = temp;
}
//引用传递 &a &b
void swap03(int &a, int &b)
{
int temp = a;
a = b;
b = temp;
}
int main()
{
int a = 10;
int b = 20;
swap01(a, b);//值传递-->形参不会修饰实参!
cout << "swap01主函数 a=" << a<<endl;
cout << "swap01主函数 b=" << b<<endl;
swap02(&a, &b);//地址传递会修饰实参!
cout << " swap02 a=" << a << endl;
cout << "swap02 b=" << b << endl;
swap03(a, b);//引用传递会修饰实参!
cout << "swap03 a=" << a << endl;
cout << "swap03 b=" << b << endl;
system("pause");
system("cls");
}
引用做函数返回值
//引用做函数的返回值
//1、不要返回局部变量的引用
int& test01()
{
int a = 10;//局部变量(因为a在函数体test01内定义!)--->存放在四区中的栈区
return a;
}
//函数的调用可以作为左值
int& test02()
{
static int b = 20;
return b;
}
int main()
{
int &ref1 = test01();
cout << "ref1= " << ref1 << endl;//第一次结果正确,是因为编译器做了保留
cout << "ref1= " << ref1<< endl;//第二次结果出现乱码,是因为a的内存已经释放
int &ref2 = test02();//把test02的引用&--->a---> 相当于a=1000(用引用ref2是一样的操作)
cout << "ref2= " << ref2 << endl;
cout << "ref2= " << ref2 << endl;
test02() = 1000;//如果函数做左值,那么必须返回引用
cout << "ref2= " << ref2 << endl;
cout << "ref2= " << ref2 << endl;
system("pause");
system("cls");
}
引用的本质、常量引用
本质:应用的本质在c++内部实现是一个指针常量—>自动解引用
//发现是引用,转换为 int* const ref = &a;
void func(int& ref)
{
ref = 100; // ref是引用,转换为*ref = 100
}
int main()
{
int a = 10;
//自动转换为 int* const ref = &a; 指针常量是指针指向不可改,加了const变为只读,从而也说明为什么引用不可更改
int& ref = a;
ref = 20; //内部发现ref是引用,自动帮我们转换为: *ref = 20;
cout << "a:" << a << endl;
cout << "ref:" << ref << endl;
func(a);
return 0;
}
函数提高
函数默认参数
//函数默认参数
//如果我们自己传入数据,就用自己的数据,如果没有,就用默认值
int func1(int a,int b,int c=30)
{
return a + b + c;
}
//注意事项1、如果某个位置已经有了默认参数,那么先从这个位置往后,从左往右都必须有默认值
int func2(int a, int b=20 , int c=30)
{
return a + b + c;
}
//注意事项2、如果函数声明有默认参数,函数实现就不能有默认参数
//声明和实现只能有一个有默认参数
//声明:
int func3(int a = 10, int b = 10);
//实现:
int func3(int a ,int b)
{
return a + b;
}
int main()
{
cout << func1(10, 20) << endl;//默认c=10,因为在函数体内已经默认形参c=10
cout << func2(10, 20) << endl;//默认c=10,因为在函数体内已经默认形参c=10
cout << func3() << endl;
system("pause");
system("cls");
}
函数占位参数
//占位参数
//返回值类型 函数名(数据类型){}
//目前阶段的占位函数 我们还用不到,后面课程中会用到
//占位参数还可以有默认参数
void func(int a,int = 10)
{
cout << "this is a toy" << endl;
}
int main()
{
func(10,10);
func(10);//因为占位参数有默认参数,所以也可以不存了
system("pause");
system("cls");
}
函数重载
函数名相同,以此提高复用
函数重载概述、满足条件
//函数重(chong)载:可以让函数名相同,提高复用性
/*
函数重载的满足条件:
1、在同一个作用域下:都在主函数前或者后?(现在都属于全局作用域)
2、函数名称相同
3、函数参数类型,或者个数,或者顺序不同
*/
void func()
{
cout << "func 的调用" << endl;
}
void func(int a)
{
cout << "func int a的调用" << endl;
}
void func(int a,int b)
{
cout << "func int a,int b的调用" << endl;
}
void func(double a, int b)
{
cout << "func double a,int b的调用" << endl;
}
void func(int a,double b)
{
cout << "func int a,double b的调用" << endl;
}
//注意事项:函数的返回值不可以作为函数重载的条件,会导致混乱! void-->int
//int func(int a, double b)
//{
// cout << "func int a,double b的调用" << endl;
//}
int main()
{
func();
func(10);
func(10, 10);
func(3.14, 10);
func(10, 3.14);
system("pause");
system("cls");
}
函数重载注意事项
//函数重载的注意事项
//1、引用作为重载的条件
void func(int &a)
{
cout << "func (int &a)的调用" << endl;
}
void func(const int &a)
{
cout << "func (const int &a)的调用" << endl;
//加上const之后,编译器(优化)将代码修改 相当于int temp = 10; const int &ref = temp;所以合法
}
//2、函数重载遇到默认参数
void func2(int a)
{
cout << "func2(int a)的调用" << endl;
}
void func2(int a,int b=10)//func(10)就可以调用这俩函数,就会报错了
{
cout << "func2(int a)的调用" << endl;
}
int main()
{
int a = 10;
func(a);
//a是一个变量,可读可写,而加const则是使a只可读不可写,因此仍然会走可读可写的不加const
//func(10);
//因为10直接放到可读可写的func中,int &a=10;不合法
//而第二种加上const之后,则是因为编译器(优化)将代码修改 相当于int temp = 10; const int &ref = temp;所以合法
//func2(10);//碰到默认参数 就会出现二义性,要尽量避免这种情况
system("pause");
system("cls");
}
消化以后接下来学习总结类和对象,加油~