Maison  >  Questions et réponses  >  le corps du texte

c++ - placement new 如何在指定内存上构造对象

// Default placement versions of operator new.
inline void* operator new(std::size_t, void* __p) _GLIBCXX_USE_NOEXCEPT
{ return __p; }
inline void* operator new[](std::size_t, void* __p) _GLIBCXX_USE_NOEXCEPT
{ return __p; }

如上,查看gcc中stl的源代码的实现方式。placement new 只是返回__p指针。若

class XX
{
    //define ctor,dctor here
    //data member
};
char buf[sizeof(XX)];
XX* ptr = new(buf) XX();

像上面这样使用placement new,请问XX是如何在那个指定的内存buf上完成构造的呢?明明只是把指针返回了啊。

PHPzPHPz2713 Il y a quelques jours522

répondre à tous(2)je répondrai

  • 伊谢尔伦

    伊谢尔伦2017-04-17 15:23:05

    首先,你这里的调用的placement new不是头文件new中定义的那个placement new。你得写::operator new(...);才行。

    然后关于这个placement new,C++标准中有如下声明(C++11):
    18.6.1.3 Placement forms [new.delete.placement]
    1 These functions are reserved, a C++ program may not define functions that displace the versions in the Standard C++ library (17.6.4). The provisions of (3.7.4) do not apply to these reserved placement forms of operator new and operator delete.

    大意是说,这些函数是保留在那的,不能动,目前没什么用。

    répondre
    0
  • 大家讲道理

    大家讲道理2017-04-17 15:23:05

    考虑结构体如何初始化的问题。
    只要分配好内存,你就可以把这段内存reinterpret_cast成你想要的类型,这个过程就让类的成员变量有了自己的内存空间,然后再调用这个类型的构造函数就可以了,原理就是这样。你自己也可以手动实现一个placement new

    répondre
    0
  • Annulerrépondre