C++ Malloc 和构造函数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2995099/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 11:43:48  来源:igfitidea点击:

Malloc and constructors

c++

提问by ckv

Unlike new and delete operators malloc does not call the constructor when an object is created. In that case how must we create an object so that the constructor will also be called.

与 new 和 delete 运算符不同,malloc 在创建对象时不会调用构造函数。在那种情况下,我们必须如何创建一个对象以便构造函数也将被调用。

回答by Michael Mrozek

Er...use new? That's kind of the point. You can also call the constructor explicitly, but there's little reason to do it that way

呃……用new吗?这就是重点。您也可以显式调用构造函数,但没有什么理由这样做

Using new/delete normally:

正常使用新建/删除:

A* a = new A();

delete a;

Calling the constructor/destructor explicitly ("placement new"):

显式调用构造函数/析构函数(“placement new”):

A* a = (A*)malloc(sizeof(A));
new (a) A();

a->~A();
free(a);

回答by warrenm

You can use "placement new" syntax to do that if you really, really need to:

如果您确实需要,您可以使用“placement new”语法来做到这一点:

MyClassName* foo = new(pointer) MyClassName();

MyClassName* foo = new(pointer) MyClassName();

where pointeris a pointer to an allocated memory location large enough to hold an instance of your object.

wherepointer是指向分配的内存位置的指针,该位置足够大以容纳对象的实例。

回答by R Samuel Klatchko

Prefer new.

喜欢new

But if for some reason you have raw memory, you can construct it with "placement new":

但是如果由于某种原因你有原始内存,你可以用“placement new”来构造它:

new (ptr) TYPE(args);

And since you won't be using delete, you'll need to call the destructor directly:

由于您不会使用 delete,因此您需要直接调用析构函数:

ptr->~TYPE();

回答by Igor Zevaka

Use placement new. The advice is handy:

使用placement new. 这个建议很方便:

ADVICE: Don't use this "placement new" syntax unless you have to. Use it only when you really care that an object is placed at a particular location in memory. For example, when your hardware has a memory-mapped I/O timer device, and you want to place a Clock object at that memory location.

建议:除非万不得已,否则不要使用这种“placement new”语法。仅当您真正关心将对象放置在内存中的特定位置时才使用它。例如,当您的硬件具有内存映射 I/O 计时器设备,并且您希望在该内存位置放置一个 Clock 对象时。

回答by CB Bailey

You mis-understand what malloc does. malloc does not create objects, it allocates memory. As it does not create objects there is no object for it to call a constructor to create.

你误解了 malloc 的作用。malloc 不创建对象,它分配内存。由于它不创建对象,因此没有对象可以调用构造函数来创建。

If you need to dynamically create an object in C++ you need to use some form of new.

如果需要在 C++ 中动态创建对象,则需要使用某种形式的 new。

回答by Alex F

Take a look at placement new operator, which constructs an object on a pre-allocated buffer.

看看placement new 运算符,它在预先分配的缓冲区上构造一个对象。

回答by Anish.A.R

class A
{
    public:
      A() { printf("Constructor of A"); }
};

int main()
{
   A* pA = (A*) malloc(sizeof(A));  // Construtor is not going to be called.

   __asm { MOV EAX, pA}             // Makes pA as "this" pointer in intel based s/m.

   A();                             // Calling constructor explicitly.

   return 0;
}