一维指针内存分配
int *p = new int; //分配一个内存 delete p;
int *p = new int();//分配一个内存,默认初始化为0 delete p;
int *p = new int(50);//分配一个内存,初始化为50 delete p;
int *p = new int[n];//分配n个内存 delete [ ]p;
int *p = new int[n]();//分配n个内存,全部初始化为0 delete [ ]p;
int *p = new int[8]{1,2,3,4,5,6,7,8}//分配n个内存,依次初始化 delete [ ]p;
二维指针内存分配
int **P = new int*[n]; //分配n个一维指针
for(int i = 0;i<n;i++)
{
p[i] = new int[m]; //逐一给每个一维指针分配m个内存
}
for(i = 0;i<n;i++)
{
delete [ ]p[i];
}
delete [ ]p;