zoukankan      html  css  js  c++  java
  • new delete malloc free区别

    回答此问题时,照下面顺序回答:

    1、malloc  free是库函数,new  delete是运算符。

    2、malloc free只是申请/释放内存,不能构造和析构对象;new  free可以申请/释放内存,构造/析构对象。

    3、举例说明第2点:

    #include <iostream>
    #include <stdlib.h>
    using namespace std;
    
    class Test
    {
    public:
    	int a;
    	Test()
    	{
    		a = 1;
    	}
    };
    
    int main()
    {
    	Test *pTestNew = new Test[3];
    	Test *pTestMalloc = static_cast<Test*>(malloc(3 * sizeof(Test)));
    
    	cout << pTestNew[0].a << endl;    	//1
    	cout << pTestMalloc[0].a << endl; 	//随机数
    
    	return 0;
    }
    

    4、malloc申请内存失败返回NULL,new申请内存失败则抛出异常(1993年以前也是返回0,现在要使用返回0时,new的入口点采用nothrow对象)。

    class widget { ... };
    
    widget *pw1 = new widget;// 分配失败抛出std::bad_alloc if
    
    if (pw1 == 0) ...	// 这个检查一定失败
    	widget *pw2 = new (nothrow) widget; 	// 若分配失败返回0
    	
    if (pw2 == 0) ...	// 这个检查可能会成功
    

    5、new operator = operator new + placement new

    new operator就是上面讲的new.

    new operator(申请内存+构造对象) = operator new(申请内存) + placement new(构造对象)

    (1)operator new若一次没申请成功,则一直在申请内存,直到new_handler为空抛出异常。

    operator new的伪代码看起来会象下面这样:
    void * operator new(size_t size)        // operator new还可能有其它参数
    {                                      

      if (size == 0) {                      // 处理0字节请求时,
        size = 1;                           // 把它当作1个字节请求来处理
      }                                    
      while (1) {
        分配size字节内存;

        if (分配成功)
          return (指向内存的指针);

        // 分配不成功,找出当前出错处理函数
        new_handler globalhandler = set_new_handler(0);
        set_new_handler(globalhandler);

        if (globalhandler) (*globalhandler)();
        else throw std::bad_alloc();
      }
    }

    (2)placement new的使用。placement new构造对象,已申请的内存buffer指向该构造函数。

    class Widget
    {
    public:
    	Widget(int widgetSize);
    };
    
    Widget* constructWidgetInBuffer(void *buffer, int widgetSize)
    {
    	return new (buffer) Widget(widgetSize);
    }
  • 相关阅读:
    111
    python 错误宝典
    Node.js Web开发:Connect
    Node.js 中的重要API:HTTP
    Node.js 中的重要API:TCP
    Node.js 中的重要API:命令行工具以及FS API 首个Node应用
    Node.js 中的JS
    Node.js 阻塞式IO与非阻塞式IO与错误处理
    Learning Vue.js 2
    A1046——入门模拟 Shortest Distance
  • 原文地址:https://www.cnblogs.com/helloweworld/p/3064387.html
Copyright © 2011-2022 走看看