zoukankan      html  css  js  c++  java
  • new/delete和malloc/free的比较

    从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.

  • 相关阅读:
    WEB前端工程师 – 职业生涯规划
    求Sn=a+aa+aaa+…+aaa…a的值
    输入一行字符,分别统计出其中英文字母 空格 数字和其他字符的个数
    getchar()的用法!
    求1+2+…+n的和不大于1000的最大自然数n
    编程打印输出*金字塔
    从键盘输入一个整数,判断该数是否回文数.
    编程求"水仙花数"
    编程求出1000以内的完全数
    输入两个正整数,求它们的最大公约数和最小公倍数.
  • 原文地址:https://www.cnblogs.com/zlcxbb/p/5752187.html
Copyright © 2011-2022 走看看