malloc/free and new/delete in C++
来源:互联网 发布:网络用语和谐 编辑:程序博客网 时间:2024/06/16 05:18
刚从深圳某车机方案公司出来,现在在一家做通讯模块的公司上班。第一天上班就一位毕业生写的代码存在很多的内存泄漏问题。另外,看了一些代码发现存在内存申请与分配又存在不一致的问题。刚好看到“malloc/free and new/delete in C++”这篇文章故收下了。
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.
{
public :
Obj(void){ cout << “Initialization” << endl; }
~Obj(void){ cout << “Destroy” << endl; }
void Initialize(void){ cout << “Initialization” << endl; }
void Destroy(void){ cout << “Destroy” << endl; }
};
void UseMallocFree(void)
{
Obj *a = (obj *)malloc(sizeof(obj)); // allocate memory
a->Initialize(); // initialize
//…
a->Destroy(); // clean up
free(a); // free allocated memory
}
void UseNewDelete(void)
{
Obj *a = new Obj; // allocate memory and initialize
//…
delete a; // 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 and new/delete in C++
- malloc/free and new/delete in C++
- malloc/free and new/delete in C++
- malloc, free, new and delete
- malloc/free and new/delete
- 关于new/delete and malloc/free
- Difference between new (delete) and malloc (free)
- 浅谈malloc-free and new-delete
- malloc/free and new/delete 工作机制
- C++malloc/free和new/delete
- C/C++中new/delete malloc/free
- new delete malloc free
- malloc/free new/delete
- malloc, free, new , delete
- new delete malloc free
- new,delete, malloc, free
- new delete malloc free
- new/delete malloc free
- HDU 3746 珠子问题
- ARM协处理器主要用途 及其 指令CDP LDC STC MCR MRC介绍
- 菜鸟第一次wp项目总结
- 让你的从godaddy注册的域名指向一个IP
- expdp 详解及实例
- malloc/free and new/delete in C++
- C++ 强制类型转换
- 字符串数组
- Firefox+Selenium IDE
- windbg符号文件路径设置
- TextSwitcher滑动动画改变内容
- C 资源
- ceph文件系统测试
- 支撑5亿用户、1.5亿活跃用户的Twitter最新架构详解及相关实现