malloc/free & new/delete的区别

作者: AwesomeAshe | 来源:发表于2016-04-01 17:39 被阅读304次

上一篇文章学习了free,那么问题来了,c++里面的new/delete似乎也是这个功能啊,所以区别是什么呢?

于是在stackoverflow上搜了一下:
malloc/free & new/delete的区别

搬运工来了。。:

  • 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

  • 分配/释放内存
    1,从free空间分配内存
    2,返回一个完全类型的指针(因为你要指明是什么类型的)
    3,new如果分配失败的话绝不会返回一个空指针NULL,而是会抛出失败异常
    4,用类型来申请就可以了(编译器自动计算大小),(注:比如 auto p=new int;
    5,对处理数组有一个显示的版本(可能是在分配一个数组的时候会特别的处理)
    6,(不确定是否准确)会重新分配来获取更大的空间而不是由于拷贝构造函数而直接处理(意会。。)
    7,实现(new/delete)的时候就决定了是否会调用malloc/free函数
    8,可以新增一个内存分配器来处理低可用内存(set_new_handler
    9,操作符new/delete可以被合法的覆盖
    10,调用(这个类型的)构造函数和析构函数来初始化/删除对象
    (也就是说new/delete的是针对对象的!这个对象可以说int,double等,也可以是自定义的class)

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

  • 1,内存来源于堆
    2,返回的是void*类型的指针
    3,如果分配失败的话,会返回一个NULL指针(所以我们在程序里面通过判断是否为空检测是否分配成功)
    4,必须要指明需要多少Byte的空间!
    5,分配数组需要人工计算需要多大空间
    6,重新分配更大的内存,不需要考虑拷贝构造函数
    7,这组函数不会调用new/delete
    8,用户代码无法帮助解决低内存的问题
    9,mallco/free不会被合法的覆盖(无视?取消?)掉

继续补充在网上看到的:

malloc与free是C++/C 语言的标准库函数,new/delete 是C++的运算符。对于非内部数据类的对象而言,光用maloc/free 无法满足动态对象的要求。对象在创建的同时要自动执行构造函数, 对象消亡之前要自动执行析构函数。由于malloc/free 是库函数而不是运算符,不在编译器控制权限之内,不能够把执行构造函数和析构函数的任务强加malloc/free。

这个很重要,new的是对象!

相关文章

  • 2018-05-09

    new delete new delete与malloc free的区别 对于基本数据类型 malloc与free...

  • malloc/free和new/delete

    1. malloc/free与new/delete的区别 1、malloc/free是C语言的标准库函数;new/...

  • 面试准备

    C++面试总结 New、Delete和malloc、free的区别? New和Delete自动调用 构造函数 和 ...

  • malloc/free & new/delete的区别

    上一篇文章学习了free,那么问题来了,c++里面的new/delete似乎也是这个功能啊,所以区别是什么呢? 于...

  • 程序设计

    1 new/delete和malloc/free区别二者都是用于分配控件和释放空间的。new/free是c++中支...

  • 网络与通信

    1 new/delete和malloc/free区别二者都是用于分配控件和释放空间的。new/free是c++中支...

  • android

    1 new/delete和malloc/free区别二者都是用于分配控件和释放空间的。new/free是c++中支...

  • C++笔试整理

    1、new 、delete 、malloc 、free 的关系malloc 与 free 是 C++/C 语言的标...

  • C++面试(1)

    1.new、delete、malloc、free关系 delete会调用对象的析构函数,和new对应free只会释...

  • C++经典面试题(最全,面中率最高)

    1.new、delete、malloc、free关系 delete会调用对象的析构函数,和new对应free只会释...

网友评论

    本文标题:malloc/free & new/delete的区别

    本文链接:https://www.haomeiwen.com/subject/tqeylttx.html