目录
主要内容是回顾C++的知识,并记录笔记。核心编程内容主要针对面向对象编程技术。
意义:不同区域存放的的数据有不同的生命周期,给与程序员更大的编程灵活度。
四个区域分别为:代码区,全局区,栈区,堆区。
其中,程序编译生成exe文件但未执行前存在着两个区域:代码区和全局区。程序运行后产生了堆区和栈区存储。
1.代码区与全局区:
1)代码区存放着CPU执行的二进制机器指令。它的特点是共享和只读,分别是为了节省内存和防止被意外修改。
2)全局区在程序运行结束后由操作系统释放。全局区分为两个部分,第一个部分是常量区,这里主要存储了字符串常量和全局常量;第二部分则包含了全局变量和静态变量。即包括了全局不变和可变的一些部分,可以在程序还未运行之前提前创建,加速程序运行时间。
实验如下:
#include<iostream>
using namespace std;
//全局常量
const int const_global_a = 10;
const int const_global_b = 10;
//全局变量
int global_a = 10;
int global_b = 10;
//静态全局常量
const static int const_global_static_a = 10;
const static int const_global_static_b = 10;
//静态全局变量
static int global_static_a = 10;
static int global_static_b = 10;
int main() {
//局部变量
int local_a = 10;
int local_b = 10;
//静态局部变量
static int static_local_a = 10;
static int static_local_b = 10;
//静态局部常量
const static int const_local_static_a = 10;
const static int const_local_static_b = 10;
//局部常量
const int const_local_a = 10;
const int const_local_b = 10;
cout << "局部变量local_a的地址为: " << (int)&local_a << endl;
cout << "局部变量local_b的地址为: " << (int)&local_b << endl;
cout << "局部常量const_local_a的地址为: " << (int)&const_local_a << endl;
cout << "局部常量const_local_b的地址为: " << (int)&const_local_b << endl;
cout << "全局变量global_a的地址为: " << (int)&global_a << endl;
cout << "全局变量global_b的地址为: " << (int)&global_b << endl;
cout << "静态局部变量static_local_a的地址为: " << (int)&static_local_a << endl;
cout << "静态局部变量static_local_b的地址为: " << (int)&static_local_b << endl;
cout << "静态局部常量const_local_static_a的地址为: " << (int)&const_local_static_a << endl;
cout << "静态局部常量const_local_static_b的地址为: " << (int)&const_local_static_b << endl;
cout << "静态全局变量global_static_a的地址为: " << (int)&global_static_a << endl;
cout << "静态全局变量global_static_b的地址为: " << (int)&global_static_b << endl;
cout << "静态全局常量const_global_static_a的地址为: " << (int)&const_global_static_a << endl;
cout << "静态全局常量const_global_static_b的地址为: " << (int)&const_global_static_b << endl;
cout << "字符串常量Hello的地址为: " << (int)&"Hello" << endl;
cout << "字符串常量Hello的地址为: " << (int)&"Hello" << endl;
cout << "字符串常量Worold的地址为: " << (int)&"Worold" << endl;
cout << "全局常量const_global_a的地址为: " << (int)&const_global_a << endl;
cout << "全局常量const_global_b的地址为: " << (int)&const_global_b << endl;
}
2.栈区:
由编译器自动分配与释放,存放函数的参数值和局部变量等。
因为是编译器自动释放,所以千万不要返回局部变量的地址,这本身就是非法操作。
#include<iostream>
using namespace std;
int* func() {
int a = 10;
return &a;
}
int main() {
int* p = func();
cout << *p << endl;
cout << *p << endl;
}
第一次能正确输出是因为编译器自动做了一次保留,但第二次调用就出现问题了!
3.堆区:
堆区由程序员自动分配,如果运行中没有释放则在程序结束后由操作系统回收。
在C++中主要用new操作符在堆区开辟内存,并用delete操作符释放。利用new常见的数据会返回其指针。
语法: new 数据结构、new 数据结构[]、delete、delete[]
#include<iostream>
using namespace std;
int* func() {
int* a = new int(10);
return a;
}
int main() {
int* arr = new int[10];
int* p = func();
cout << *p << endl;
cout << *p << endl;
cout << endl;
//利用delete释放堆区数据
delete p;
//cout << *p << endl; 如果运行就会报错,释放后的空间访问非法。
for (int i = 0; i < 10; i++) arr[i] = 10 + i;
for (int i = 0; i < 10; i++) cout<<arr[i]<<endl;
//删除数组需要使用delete[]
delete[] arr;
}