标签:new delete
new 与delete 是c++中的概念。
new 可以用于在堆上申请任何类型的变量的内存空间!
int *p=new int(3)//初始化为3的一个整形
char* p = new char(‘s‘);//new个字符
float* p1 = new float(0.55);
int* p2 = new int[5]{ 0, 1, 2, 3, 4 };//new个数组并赋初值
C *p3 = new C(4);
delete p;
delete p1;
delete [] p2;//删除这个数组
delete p3;
new 会自动调用构造函数。而delete直接调用析构函数。
注意:malloc 和 free 和new 和delete 在基础类型中可以混用,但是类类型只有用new delete
标签:new delete
原文地址:http://ji123.blog.51cto.com/11333309/1919767