栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++ 内存管理之operator new

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++ 内存管理之operator new

C++ 内存管理之operator new

C++提供了new和delete方式来申请和释放堆区内存,但new和delete是指的expression,而底层是operator new函数。下面来区分一下这几个概念。

  • new/delete expression

    new expression动态分配内存,delete expression释放内存。网上也有说是new operator的,这种说法属实容易让人误解​.​.​.

    • new expression的行为分析

      1. 通过调用合适的内存分配函数,也就是operator new()

      2. void指针转型和初始化对象,类似于

        A* p = static_cast(::operator new(size))p->A();
        

        但是用户不能直接这样调用,只能用placement new,即new(static_castp) A()

      A* p = new A;
      	  // allocates memory by calling: operator new (sizeof(A))
            // and then constructs an object at the newly allocated space
      
    • delete expression的行为分析

      1. 调用析构
      2. 释放内存
    • placement new

      在已分配的内存空间上,初始化对象(调用构造函数)

      如果给new-expression提供了placement-params(指针),这个参数会作为placement new版本的operator new的额外参数,具体为void* operator new(std::size_t, void*),这个函数只是返回第二个参数。

      注意这种方式,需要手动调析构函数,否则可能会内存泄露

      // within any block scope...
      {
          alignas(T) unsigned char buf[sizeof(T)];
          // Statically allocate the storage with automatic storage duration
          // which is large enough for any object of type `T`.
          T* tptr = new(buf) T; // Construct a `T` object, placing it directly into your 
                                // pre-allocated storage at memory address `buf`.
          tptr->~T();           // You must **manually** call the object's destructor
                                // if its side effects is depended by the program.
      }                         // Leaving this block scope automatically deallocates `buf`.
      
  • operator new

    allocation functions,中给出了三个版本

    throwing (1)	
    void* operator new (std::size_t size);
    nothrow (2)	
    void* operator new (std::size_t size, const std::nothrow_t& nothrow_value) noexcept;
    placement (3)	
    void* operator new (std::size_t size, void* ptr) noexcept;
    

    第一个版本,分配size字节的空间,失败throw std::bad_alloc

    第二个版本,分配size字节的空间,不抛出异常,return null

    第三个版本,直接返回ptr,当使用new-expression调用此版本时,是placement new

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/699274.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号