动态空间申请
new和delete操作基本类型空间
new申请堆区空间, delete释放空间
int *p = NULL;
p = new int; //从堆区申请int类型大小的空间
*p = 100;
cout << "*p = " << *p << endl;
//释放空间
delete p;
//new 申请空间时初始化空间
int *p1 = NULL;
p1 = new int(150);
cout << "*p1 = " << *p1 << endl;
delete p1;
new和delete 操作数组空间
new与[]结合表示申请数组空间 delete释放时
int *arr = NULL;
arr = new int[5]{10, 20, 30, 40, 50};
int n = _msize(arr)/sizeof(arr);
for(int i=0;i<n;i++)
{
cout << arr[i] << endl;
}
//如果new有[] 那么delete也需要[]
delete[] arr;
标签:11,arr,int,申请,空间,new,动态,delete
From: https://www.cnblogs.com/mzx233/p/17743962.html