转自:http://www.pixelstech.net/article/index.php?id=1340193129
malloc and free are C++/C language standard library functions, while new/delete are operator of C++. They can be used to allocate dynamic memory and free memory in C++ programs
malloc/free can not meet the requirements of dynamic objects creation. Object needs to call the constructor to initialize the object when creating, the object needs to call the destructor before it is destroyed Since malloc() and free() are library functions rather than operators, the compiler has no control permissions on them, so it can not impose the task of object construction and destruction on malloc() and free().
Therefore, C++ has an operator new to complete object dynamic memory allocation and object initialization, and an operator delete to clean up the object and release the memory allocated to the object. Note that the new/delete are not library functions.
Let's take a look at some codes to understand how malloc/free and new/delete to do object memory management.
class Obj
{
public:
Obj(void){ cout << “Initialization” << endl; }
~Obj(void){ cout << “Destroy” << endl; }
void Initialize(void){ cout << “Initialization” << endl; }
void Destroy(void){ cout << “Destroy” << endl; }
};
voidUseMallocFree(void)
{
Obj *a = (obj *)malloc(sizeof(obj)); // allocate memory
a->Initialize(); // initialize
//…
a->Destroy(); // clean up
free(a); // free allocated memory
}
voidUseNewDelete(void)
{
Obj *a = newObj; // allocate memory and initialize
//…
deletea; // clean up and free allocated memory
}
Initialize() function in class Obj simulates the function of the constructor and Destroy() simulates the function of the destructor. In function UseMallocFree(). since malloc() and free() only take care of allocating and freeing memory, we need to call Initialize() and Destroy() to initialize the object and destroy the object. It's much simpler if we use UseNewDelete().
Since new/delete have all the functions of malloc/free, then why doesn't C++ rule out new/delete? This is because C++ programs need to call C functions frequently and C programs can only use malloc/free to manage dynamic objects.
If using free() to free the object created using new, then this object will produce error because it cannot call the destructor to destroy the object. If using delete to free the memory allocated with malloc(), theoretically the program will have no problem, but the readability is very poor. So we recommend to use new/delete as a pair and malloc/free as a pair as well.
译文:
malloc和free是C++/C语言的标准库函数,而new/delete则是C++的运算符,用来动态分配和释放内存。
malloc/free不能满足创建动态对象的要求。对象需要通过调用构造函数来创建,通过调用析构函数去删除。但是,malloc()和free()是库函数,不是运算符,而编译器是对库函数是没有控制权限的。所以,编译器无法完成对象的构造和析构任务。
因此,C++新引入了两个运算符:new用来完成对象的动态内存分配和初始化;delete用来完成对象的删除,并释放分配给对象的内存。请注意,new/delete不是库函数。
看看下面的这些代码,可以更好的了解的malloc/free和new/delete对象的内存管理。
class Obj
{
public:
Obj(void){ cout << “Initialization” << endl; }
~Obj(void){ cout << “Destroy” << endl; }
void Initialize(void){ cout << “Initialization” << endl; }
void Destroy(void){ cout << “Destroy” << endl; }
};
voidUseMallocFree(void)
{
Obj *a = (obj *)malloc(sizeof(obj)); // allocate memory
a->Initialize(); // initialize
//…
a->Destroy(); // clean up
free(a); // free allocated memory
}
voidUseNewDelete(void)
{
Obj *a = newObj; // allocate memory and initialize
//…
deletea; // clean up and free allocated memory
}
Initialize()函数在类Obj中模拟构造函数的功能,Destroy()模拟析构函数的功能。在UseMallocFree()中,因为malloc()和free()只负责分配和释放内存,我们需要调用Initialize()和Destroy()来初始化对象,并删除该对象。而使用UseNewDelete()就会简洁的多。
既然new/delete囊括了malloc/free的功能,那为什么C++不删掉malloc/free呢?这是因为C++程序经常需要调用C语言函数,而C语言程序只能使用malloc / free管理动态对象。
如果使用free()来释放通过new构造的对象,那么这个对象会产生错误,因为它不能调用析构函数删除对象。如果用delete来释放通过malloc()分配的内存,理论上没有问题,但可读性非常差。因此,我们强烈建议成对的使用new/delete,成对的使用malloc/free。