没有默认构造函数的类数组的运算符 new

发布于 2024-08-27 00:43:46 字数 684 浏览 10 评论 0原文

对于没有默认构造函数的类,可以使用operator new和placement new来声明该类的数组。

当我阅读更有效的C++中的代码时,我发现代码如下(我修改了一些部分)......

我的问题是,为什么在operator new后面需要[]?

我测试了一下,没有它,它仍然有效。有谁能解释一下吗?

class A {
    public:
    int i;

    A(int i):i(i) {}
};

int main()
{
      void *rawMemory = operator new[] (10 * sizeof(A));   // Why [] needed here?
      A *p = static_cast<A*>(rawMemory);

      for(int i = 0 ; i < 10 ; i++ ) {

            new(&p[i])A(i); 

      }

      for(int i = 0 ; i < 10 ; i++ ) {

            cout<<p[i].i<<endl;

      }

      for(int i = 0 ; i < 10 ; i++ ) {

            p[i].~A();

      }

    return 0;
}

For a class without default constructor, operator new and placement new can be used to declare an array of such class.

When I read the code in More Effective C++, I found the code as below(I modified some part).....

My question is, why [] after the operator new is needed?

I test it without it, it still works. Can any body explain that?

class A {
    public:
    int i;

    A(int i):i(i) {}
};

int main()
{
      void *rawMemory = operator new[] (10 * sizeof(A));   // Why [] needed here?
      A *p = static_cast<A*>(rawMemory);

      for(int i = 0 ; i < 10 ; i++ ) {

            new(&p[i])A(i); 

      }

      for(int i = 0 ; i < 10 ; i++ ) {

            cout<<p[i].i<<endl;

      }

      for(int i = 0 ; i < 10 ; i++ ) {

            p[i].~A();

      }

    return 0;
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

錯遇了你 2024-09-03 00:43:46

我很惊讶Effective C++ 会建议你使用像void* 这样的hackish 东西。

new[] 做了一件非常具体的事情:它分配一个动态大小的数组。用它分配的数组应该传递给delete[]delete[] 然后读取隐藏数字以查找数组中有多少元素,并销毁对象,就像使用 p[i] 所做的那样。~ A();

然而,这种用法与此不兼容。数组的大小是静态的,如果不正确使用 new[](无运算符),就无法获取隐藏数字或动态大小破坏,因此需要默认值构造函数。 C++ 的真正弱点。

如果您按照其他人的建议在 main 末尾调用 delete[],您的代码可能会崩溃。 相反,您需要使用 运算符delete[],这看起来像是一个拼写错误,只是一个等待发生的意外。

如果必须使用此技巧,请使用非数组运算符 new 和运算符删除以及充足的注释。但我不认为这是特别有效的 C++。

I'm surprised that Effective C++ would be advising you to use something as hackish as a void*.

new[] does a very specific thing: it allocates a dynamically sized array. An array allocated with it should be passed to delete[]. delete[] then reads a hidden number to find how many elements are in the array, and destroys the objects as you have done with p[i].~A();.

However, this usage is incompatible with that. The array is statically sized, and there's no way to get that hidden number or dynamic-size destruction without properly using new[] (no operator), in turn requiring a default constructor. A genuine weakness of C++.

If you called delete[] at the end of main as others have suggested, your code could crash. Instead you need to use operator delete[], which looks like a typo and is just an accident waiting to happen.

Use non-array operator new and operator delete and ample comments if you must use this trick. But I wouldn't consider this particularly effective C++.

命硬 2024-09-03 00:43:46

不需要。运算符 new 和运算符 new[] 之间的唯一区别是,第一个是通过使用关键字 new 来调用的,另一个是通过关键字 new[] 来调用的。两者都分配原始内存。

只要确保当您最终释放内存(这里的代码只是泄漏)时,您调用了与 new 或 new[] 匹配的 delete 或 delete[] 。

It's not needed. The only difference between operator new and operator new[] is that the first is called by usage of keyword new and the other by keyword new[]. Both allocate raw memory.

Just make sure when you finally free the memory (your code here just leaks) that you call the delete or delete[] that matches new or new[].

悲念泪 2024-09-03 00:43:46

在这种情况下并不严格需要它。它们都将分配相同数量的内存,但其中一个将需要 delete,而一个将在最后需要 delete[]。使用 new[] 可以让你的意图更加清晰,这就是这里使用它的原因。

It isn't strictly needed in this case. They both will allocate the same amount of memory, but one will require delete and one will require delete[] at the end. Using new[] makes your intent somewhat more clear, which is why it is used here.

心在旅行 2024-09-03 00:43:46

它并不是真正需要的——它只是让您有机会为数组分配内存,与单个对象的内存分开,如果您选择这样做的话。

It's not really needed -- it just gives you a chance to allocate memory for arrays separately from memory for single objects, if you choose to do so.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文