简体   繁体   English

C ++中的“ new”运算符何时调用构造函数

[英]When is the constructor called by 'new' operator in C++

Since I started learning C++, I have always read that the 'new' operator calls the constructor of the object before returning the pointer to the allocated memory. 自从我开始学习C ++以来,我总是读到'new'运算符在将指针返回到分配的内存之前会调用对象的构造函数。

So, out of curiosity I checked the source code for 'new' and I found the following at http://gcc.gnu.org/viewcvs/gcc/trunk/libstdc%2B%2B-v3/libsupc%2B%2B/new_op.cc?revision=197380&view=markup 因此,出于好奇,我检查了源代码中的“ new”,并在http://gcc.gnu.org/viewcvs/gcc/trunk/libstdc%2B%2B-v3/libsupc%2B%2B/中找到了以下内容new_op.cc?revision=197380&view=markup

_GLIBCXX_WEAK_DEFINITION void *
    operator new (std::size_t sz) _GLIBCXX_THROW (std::bad_alloc)
    {
      void *p;

      /* malloc (0) is unpredictable; avoid it.  */
      if (sz == 0)
        sz = 1;
      p = (void *) malloc (sz);
      while (p == 0)
        {
          new_handler handler = std::get_new_handler ();
          if (! handler)
            _GLIBCXX_THROW_OR_ABORT(bad_alloc());
          handler ();
          p = (void *) malloc (sz);
        }

      return p;
    }

I do not see any constructor being called or any sort of mechanism to identify the type of object. 我看不到任何构造函数被调用或任何类型的机制来标识对象的类型。

So, how is this done? 那么,这是怎么做的呢? Does the compiler play some trick by calling the constructor on the allocated memory? 编译器通过在分配的内存上调用构造函数来发挥作用吗? Any help will be appreciated. 任何帮助将不胜感激。

Also, in case of new[] (at link below), no entry is being made to keep track of the number of elements in the array. 同样,在new []的情况下(在下面的链接中),不会进行任何记录来跟踪数组中元素的数量。 So, how does delete[] know how many elements to be destructed? 那么,delete []如何知道要销毁多少元素?

http://gcc.gnu.org/viewcvs/gcc/trunk/libstdc%2B%2B-v3/libsupc%2B%2B/new_opv.cc?revision=195701&view=markup http://gcc.gnu.org/viewcvs/gcc/trunk/libstdc%2B%2B-v3/libsupc%2B%2B/new_opv.cc?revision=195701&view=markup

I went through many related questions on SO and also googled it, but could not find an answer. 我在SO上经历了许多相关问题,并在Google上进行了搜索,但找不到答案。

I do not see any constructor being called or any sort of mechanism to identify the type of object. 我看不到任何构造函数被调用或任何类型的机制来标识对象的类型。

The job of the operator new function is just to allocate memory; operator new功能的工作只是分配内存; it doesn't call a constructor, and doesn't even know what type of object (if any) will be constructed in the memory. 它不调用构造函数,甚至不知道将在内存中构造哪种类型的对象(如果有)。 It's just told how many bytes to allocate. 只是告诉您要分配多少字节。

Does the compiler play some trick by calling the constructor on the allocated memory? 编译器通过在分配的内存上调用构造函数来发挥作用吗?

If by "play some trick", you mean "generate some code", then yes. 如果说“玩些花样”是指“生成一些代码”,那么是的。 If you use a new -expression to create an object, then it does two things: 如果使用new -expression创建对象,那么它会做两件事:

  • Calls operator new() to allocate enough memory for the object; 调用operator new()为该对象分配足够的内存;
  • Calls a constructor to initialise an object in that memory. 调用构造函数以初始化该内存中的对象。

Also, in case of new[] (at link below), no entry is being made to keep track of the number of elements in the array. 同样,在new []的情况下(在下面的链接中),不会进行任何记录来跟踪数组中元素的数量。 So, how does delete[] know how many elements to be destructed? 那么,delete []如何知道要销毁多少元素?

The new[] expression (not operator new[] ) will record that somewhere. new[]表达式(不是operator new[] )会将其记录在某处。 The exact details are left to the implementation. 确切的细节留给实现。

Does the compiler play some trick by calling the constructor on the allocated memory? 编译器通过在分配的内存上调用构造函数来发挥作用吗?

Yes, the operator new that is seen above is only the implementation of the part that gets the memory block. 是的,上面看到的operator new只是获取内存块的部分的实现。 When using new T in your code, the compiler invokes the appropriate operator new (size_t) implementation to get a memory block, and then calls the T constructor on it. 在代码中使用new T时,编译器将调用相应的operator new (size_t)实现来获取内存块,然后在其上调用T构造函数。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM