1.申请一个数据
int *func()
{
// 在堆区创建整型数据
//new返回是该数据类型的指针
int *p=new int(10);
return p;
}
void test1()
{
int *p=func();
cout<<*p<<endl;
cout<<*p<<endl;
//堆区的数据 由程序员管理开辟,程序员管理释放
//如果想要释放堆区的数据,可利用关键字 delete
delete p;
}
2.申请一个数组
void test2()
{
int i=0;
int *arry=new int[10];
for(i=0;i<10;i++)
arry[i]=100+i;
for(i=0;i<10;i++)
cout<<arry[i]<<" "<<endl;
//delete用来释放堆区数组 且释放的时候需要在前面加上一个中括号
delete[] arry;
}