源码
// 09NewDelete.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include "iostream.h"
#include "stdlib.h"
class Test
{
int *p;
public:
Test()
{
p = new int;
}
~Test()
{
delete p;
}
};
int main(int argc, char* argv[])
{
Test* p1 = (Test*)malloc(sizeof(Test));
Test* p2 = new Test;
Test* p3 = new Test;
//问题:上述两个过程,有何不同?
delete p2;
free(p3);
//问题:上述两个释放,有何不同?
return 0;
}
问题:上述两个过程,有何不同?
前者没有调用构造函数
问题:上述两个释放,有何不同?
后者没有调用析构函数
本文通过一个C++示例对比了使用new和malloc分配内存的区别,以及delete和free释放内存的不同之处,强调了构造函数和析构函数的作用。
6860

被折叠的 条评论
为什么被折叠?



