从C++角度上说,使用new分配堆空间可以调用类的构造函数,而malloc()函数仅仅是一个函数调用,它不会调用构造函数,它所接受的参数是一个unsigned long类型。同样,delete在释放堆空间之前会调用析构函数,而free函数则不会。


new/delete

  • Allocate/release memory
    1. Memory allocated from 'Free Store'
    2. Returns a fully typed pointer.
    3. new (standard version) never returns a NULL (will throw on failure)
    4. Are called with Type-ID (compiler calculates the size)
    5. Has a version explicitly to handle arrays.
    6. Reallocating (to get more space) not handled intuitively (because of copy constructor).
    7. Whether they call malloc/free is implementation defined.
    8. Can add a new memory allocator to deal with low memory (set_new_handler)
    9. operator new/delete can be overridden legally
    10. constructor/destructor used to initialize/destroy the object

malloc/free

  • Allocates/release memory
    1. Memory allocated from 'Heap'
    2. Returns a void*
    3. Returns NULL on failure
    4. Must specify the size required in bytes.
    5. Allocating array requires manual calculation of space.
    6. Reallocating larger chunk of memory simple (No copy constructor to worry about)
    7. They will NOT call new/delete
    8. No way to splice user code into the allocation sequence to help with low memory.
    9. malloc/free can NOT be overridden legally

Table comparison of the features:

 Feature                  | new/delete                     | malloc/free
--------------------------+--------------------------------+-------------------------------
Memory allocated from | 'Free Store' | 'Heap'
Returns | Fully typed pointer | void*
On failure | Throws (never returns NULL) | Returns NULL
Required size | Calculated by compiler | Must be specified in bytes
Handling arrays | Has an explicit version | Requires manual calculations
Reallocating | Not handled intuitively | Simple (no copy constructor)
Call of reverse | Implementation defined | No
Low memory cases | Can add a new memory allocator | Not handled by user code
Overridable | Yes | No
Use of (con-)/destructor | Yes | No

Technically memory allocated by new comes from the 'Free Store' while memory allocated by malloc comes from the 'Heap'. Whether these two areas are the same is an implementation details, which is another reason that malloc and new can not be mixed.

new/delete和malloc/free的比较的更多相关文章

  1. new/delete和malloc/free的区别

    通俗易懂版本:http://zhidao.baidu.com/question/86185100 1 new/delete和malloc/free最大区别是对对象的理解. 如果你使用 Foo* foo ...

  2. c/c++ 复习基础要点01-const指针、指针函数 函数指针、new/delete与malloc/free区别与联系

    1.      引用本身是有指针实现的:引用为只读指针 例子: int d=123; int& e=d;    //引用 int * const e=d; //只读指针,e指向d,不可修改e指 ...

  3. new、delete与malloc、free的详解

    内容清单: 1.  C语言中的函数malloc和free 2.  C++中的运算符new和delete 3.  new/delete与malloc/free之间的联系和区别 4.  C/C++程序的内 ...

  4. new/delete 与 malloc/free的区别

    一.概述     在C++中,申请动态内存与释放动态内存用new/delete 与 malloc/free都可以,而且他们的存储方式相同,new/malloc申请的动态内存位于堆中,无法被操作系统自动 ...

  5. new 、 delete 、 malloc 、 free 关系

    1.new . delete . malloc . free 关系 delete 会调用对象的析构函数 , 和 new 对应, free 只会释放内存, new 调用构造函数. malloc 与 fr ...

  6. 关于new,delete,malloc,free的一些总结

    首先,new,delete都是c++的关键字并不是函数,通过特定的语法组成表达式,new可以在编译的时候确定其返回值.可以直接使用string *p=new string("asdfgh&q ...

  7. C++之new、delete 与malloc、free的异同

    在C/C++编程中常常会申请内存.而对内存的申请释放操作有两套方法: new.delete 与malloc.free.他们的使用最好是成对使用,不要去混搭---这可不是时尚界哦. 例如以下是这两组方法 ...

  8. C/C++——new/delete和malloc/free的区别

    new/delete和malloc/free的区别 扩容操作: 对于malloc是有一个realloc函数对应用于扩容的: 对于new,只能再new一个,for循环赋值过去,把原来的delete掉: ...

  9. new/delete 和malloc/free 的区别

    new/delete 和malloc/free 的区别 一.基本概念malloc/free:1.函数原型及说明:      void *malloc(long NumBytes):该函数分配了NumB ...

  10. C++学习之new与delete、malloc与free

    在C/C++的面试时,对于new/delete和malloc/free这两对的使用和区别经常被考查到,如果这种基础的问题都答不上来,估计很难过面试了.这篇文章仅仅是浅显的讲一下,仅供参考. 一.new ...

随机推荐

  1. Android使用Canvas实现跑马灯

    网上的很多的教程都是通过更改TextView的属性进行跑马灯的设计.这样做有很多的缺点: 1.如果TextView没有获取焦点,那么跑马灯的效果无法实现. 2.如果文本长度小于TextView的宽度, ...

  2. XWork容器的存储结构

    我们可以看到,在Container的默认实现,ContainerImpl中有两个实例变量.factoris和factoryNamesByType. 对象制造工厂 class ContainerImpl ...

  3. 修改量更新API

    /* Update a Modifier header of type 'PRO' (Promotion) */ l_MODIFIER_LIST_rec.active_flag := 'N'; l_M ...

  4. cocos2dv2项目转换为ARC支持的项目

    原帖地址: http://www.cnblogs.com/liufan9/archive/2013/04/08/3006580.html

  5. gtk+程序在关闭主窗口时的事件流

    当鼠标单击gtk+窗口的关闭按钮时,程序首先接收到delete_event,当该事件处理函数返回TRUE表示事件已处理禁止进一步传播,从而取消关闭操作:当返回FALSE时,事件消息进一步向上传播,此时 ...

  6. 基于ARM-contexA9-Linux驱动开发:如何获取板子上独有的ID号

    每个CPU,都有它固定的ID号,ID号就是这个CPU唯一的标识,它可能隐含着CPU的生产日期,版本号,型号等等,那么,在我们的这款友善之臂Tiny4412的板子上,我的这个CPU的ID又是多少呢?从我 ...

  7. Mybatis批量插入、批量更新

    合理的使用批量插入.更新对优化有很大的作用,速度明显快了N倍. 数据库连接串后面要新增:&allowMultiQueries=true 批量插入的最大限制主要是看你整条sql占用的大小,所以可 ...

  8. android 实现倒影

    首先,文章中出现的Gallery 已经不再适用,替代方法请看我的另一篇文章http://blog.csdn.net/xiangzhihong8/article/details/51120460 不过对 ...

  9. 基于友善之臂ARM-ContexA9-ADC驱动开发

    ADC,就是模数转换器,什么是模数转换器? 模数转换器,在电子技术中即是将模拟信号转换成数字信号,也称为数字量化. 当然还有一种叫DAC,就是数模转换,意思相反,即是将数字信号转换成模拟信号. 在友善 ...

  10. android的ndk开发简介-android学习之旅(93)

    环境搭建 1.安装ndk 2.安装cygwin (android是基于linux的Framework,运行的本地库是.SO,而不是.dll库,大部分都实在windows下开发,如果是linux就没这个 ...