#include<iostream>
using namespace std;
/*
1.3 new操作符
利用new在堆区创建数据时,会返回该数据对应的指针
new出来的堆区数据,手动释放用delete
*/
int * func(){
int * p = new int(10);
return p;
}
void test_1(){
int * p = func();
cout << *p << endl;
cout << *p << endl;
cout << *p << endl;
delete p; // 手动释放堆区变量
//cout << *p << endl;
//cout << *p << endl;
}
void test_2(){ // 在堆区利用new开辟数组
int * arr = new int[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; // 手动释放堆区数组
}
int main(){
test_1();
test_2();
return 0;
}
![](https://img-blog.csdnimg.cn/20210312130832431.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L0hBSUZFSTY2Ng==,size_16,color_FFFFFF,t_70)