没有默认构造函数的类数组的运算符 new
对于没有默认构造函数的类,可以使用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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我很惊讶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 todelete[]
.delete[]
then reads a hidden number to find how many elements are in the array, and destroys the objects as you have done withp[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[]
(nooperator
), in turn requiring a default constructor. A genuine weakness of C++.If you called
delete[]
at the end ofmain
as others have suggested, your code could crash. Instead you need to useoperator delete[]
, which looks like a typo and is just an accident waiting to happen.Use non-array
operator new
andoperator delete
and ample comments if you must use this trick. But I wouldn't consider this particularly effective C++.不需要。运算符 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[].
在这种情况下并不严格需要它。它们都将分配相同数量的内存,但其中一个将需要
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 requiredelete[]
at the end. Usingnew[]
makes your intent somewhat more clear, which is why it is used here.它并不是真正需要的——它只是让您有机会为数组分配内存,与单个对象的内存分开,如果您选择这样做的话。
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.