文章目录
- 1.new 创建堆内存对象
- 2.delete释放内存空间
- 3.malloc申请内存
- 4.free释放malloc申请的内存空间
1.new 创建堆内存对象
2.delete释放内存空间
3.malloc申请内存
4.free释放malloc申请的内存空间
#include <iostream>
#include <string>using namespace std;/**1.new 创建堆内存对象*2.delete释放内存空间* 3.malloc申请内存* 4.free释放malloc申请的内存空间* */class stu{
public:string name;int age;stu(string name,int age):name(name),age(age){cout << "有参构造函数"<<endl;}~stu(){cout<< "析构函数"<<endl;}
};int main() {int * i = new int(10);stu * s = new stu("小明",20);int * p = new int(54);delete p;p = nullptr;int *p2 = (int *)malloc;cout << p2 << endl;free(p2);return 0;
}