C ++中的"new"运算符何时调用构造函数 [英] When is the constructor called by 'new' operator in C++

查看:143
本文介绍了C ++中的"new"运算符何时调用构造函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

自从我开始学习C ++以来,我总是读到'new'运算符在将指针返回到分配的内存之前调用对象的构造函数.

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.

因此,出于好奇,我检查了源代码中的"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

_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.

此外,如果使用new [](在下面的链接中),则不会进行任何条目来跟踪数组中的元素数量.那么,delete []如何知道要销毁多少元素?

Also, in case of new[] (at link below), no entry is being made to keep track of the number of elements in the array. So, how does delete[] know how many elements to be destructed?

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

我在SO上经历了许多相关问题,并在Google上进行了搜索,但找不到答案.

I went through many related questions on SO and also googled it, but could not find an answer.

推荐答案

我看不到调用任何构造函数或任何类型的机制来标识对象的类型.

I do not see any constructor being called or any sort of mechanism to identify the type of object.

operator new函数的工作仅仅是分配内存;它不调用构造函数,甚至不知道将在内存中构造哪种类型的对象(如果有).只是告诉您要分配多少字节.

The job of the operator new function is just to allocate memory; 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?

如果用玩一些把戏"来表示生成一些代码",那么是的.如果使用new表达式创建对象,则它会做两件事:

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:

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

此外,如果使用new [](在下面的链接中),则不会进行任何条目来跟踪数组中的元素数量.那么,delete []如何知道要销毁多少元素?

Also, in case of new[] (at link below), no entry is being made to keep track of the number of elements in the array. So, how does delete[] know how many elements to be destructed?

new[]表达式(不是operator new[])会将其记录在某处.确切的细节留给实现.

The new[] expression (not operator new[]) will record that somewhere. The exact details are left to the implementation.

这篇关于C ++中的"new"运算符何时调用构造函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆