new 和 new[1] 和有什么区别?
new
和 new[1]
之间有什么区别?我可以将 delete
与 new[1]
一起使用吗?
编辑
好吧好吧,我应该提供背景,对此感到抱歉。我正在使用 VS 2010 评估 BoundsChecker,当我在 new[1] 上使用 delete[] 时,它抱怨内存泄漏。所以理论上我知道应该如何使用 new 和 delete 对,但这种特殊情况让我对幕后的事情感到困惑。知道发生了什么吗?
What is difference between new
and new[1]
? Can I use delete
with new[1]
?
Edit
Well well well, I should've provided the background, sorry for that. I was evaluating BoundsChecker at work with VS 2010 and it complained about a memory leak when I used delete[] on new[1]. So in theory I know how the new and delete pair should be used but this particular situation confused me about the things under the hood. Any idea whats happening?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
艾德和艾克斯是对的,但幕后还有更多的事情发生。
如果先使用new,然后使用delete,则delete调用将执行一个析构函数。
如果使用new[],就必须使用delete[],但是delete[]怎么知道要调用多少个析构函数呢?可能有 2 个实例的数组,或者 2000 个实例之一?
一些(可能是大多数或全部)编译器所做的就是在返回给您的内存之前存储实例的数量。
因此,如果您调用 new[5],那么 new 将像这样分配内存:
您将得到一个返回到 instance1 的指针。
如果您稍后调用delete[],delete[]将使用该数字(在本例中为5)来查看在释放内存之前需要调用多少个析构函数。
请注意,如果将 new 与 delete[] 混合使用,或将 new[] 与 delete 混合使用,则可能会出现严重错误,因为数字可能会丢失,或者数字可能不正确。
如果将 new[1] 与 delete 混合使用有效,那么您可能只是幸运,但不要依赖它。
Ed and aix are right, but there is much more going on underneath the hood.
If you use new, then delete, the delete call will execute one destructor.
If you use new[], you must use delete[], but how can delete[] know how much destructors to call? There might be an array of 2 instances, or one of 2000 instances?
What some (possibly most or all) compilers do, is to store the number of instances right before the memory it returns to you.
So if you call new[5], then new will allocate memory like this:
And you get a pointer back to instance1.
If you later call delete[], delete[] will use the number (in this case 5) to see how many destructors it needs to call before freeing the memory.
Notice that if you mix new with delete[], or new[] with delete, it can go horribly wrong, because the number might be missing, or the number might be incorrect.
If mixing new[1] with delete works, you might be just lucky, but don't rely on it.
new
创建一个实例,而new[1]
创建一个单元素数组。与new
相比,new[1]
几乎肯定会产生(小)内存开销来存储数组的大小。您不能将非默认构造函数与new[]
一起使用。new
必须与delete
一起使用。new[]
必须与delete[]
一起使用。new
creates an instance, whereasnew[1]
creates a single-element array.new[1]
will almost certainly incur (small) memory overhead compared tonew
to store the size of the array. You can't use non-default constructors withnew[]
.new
must be used withdelete
.new[]
must be used withdelete[]
.new[] 是一个运算符,用于分配和初始化对象数组并返回指向第一个元素的指针。这与 new 运算符不同,new 运算符具有相同的行为,但针对的是一次分配,而不是数组。对于释放和对象销毁,new 应与delete 一起使用,new[] 与delete[] 一起使用。
(请参阅此处了解更多详细信息)
new[] is an operator to allocate and initialize an array of objects and return a pointer to the first element. This differs from the new operator which has the same behavior but for one allocation, not an array. For deallocation and object destruction, new should be used with delete and new[] with delete[].
(see here for more details)
'new[1]' 创建一个包含一项的数组。需要使用
delete[]
来释放内存。new
只是创建一个对象。与它一起使用delete
。'new[1]' creates an array of one item. Need to use
delete[]
to free the memory.new
just creates an object. Usedelete
with it.