运算符new()和运算符new []()之间的区别? [英] Difference between operator new() and operator new[]()?

查看:141
本文介绍了运算符new()和运算符new []()之间的区别?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

fncs之间是否有区别:运算符new和运算符new [](不是new和new []运算符)?当然没有通话语法?我问是因为我可以用:: operator new(sizeof(T)* numberOfObject)为我的obj分配X个字节数,然后用数组表示法访问它们,所以:: operator new []有什么大不了.只是语法糖吗?

Is there any difference between fncs: operator new and operator new[] (NOT new and new[] operators)? Except of course call syntax? I'm asking because I can allocate X number of bytes for my objs with ::operator new(sizeof(T)*numberOfObject) and then access them with array notation, so what's the big deal with ::operator new[]. Is it only syntactic sugar?

#include <new>
#include <iostream>
#include <malloc.h>

using namespace std;
struct X
{
  int data_;
  X(int v):data_(v){}
};
int _tmain(int argc, _TCHAR* argv[])
{
  unsigned no = 10;
  void* vp = ::operator new(sizeof(X) * no);
  cout << "Mem reserved: " << _msize(vp) << '\n';
  X* xp = static_cast<X*>(vp);
  for (unsigned i = 0; i < no; ++i)
  {
    new (xp + i) X(i);
  }
  for (unsigned i = 0; i < no; ++i)
  {
    cout << (xp[i]).data_ << '\n';
  }
  for (unsigned i = 0; i < no; ++i)
  {
    (xp + i)->~X();
  }
  ::operator delete(vp);
  return 0;
}

推荐答案

这些函数(operator new等)通常不打算显式调用,而是由new/new[]表达式(对称地)隐式使用. ,operator delete/operator delete[]函数由delete/delete[]表达式隐式调用).对于非数组类型,使用new语法的表达式将隐式调用operator new函数,而具有new[]的表达式将隐式调用operator new[].

These functions (operator new etc.) are not generally intended to be called explicitly, but rather used implicitly by new/new[] expressions (symmetrically, operator delete/operator delete[] functions are invoked implicitly by delete/delete[] expressions). An expression that uses new syntax for non-array type will implicitly call operator new function, while an expression with new[] will implicitly call operator new[].

这里重要的细节是,通常由delete[]表达式销毁由new[]表达式创建的数组.后者将需要知道要销毁的对象的数量(如果对象具有非平凡的析构函数),即必须以某种方式将此信息从new[]表达式(已知)传递到相应的delete[]表达式(何时需要).在典型的实现中,此信息存储在new[]表达式分配的块内,这就是为什么在对operator new[]的隐式调用中请求的内存大小通常比更大的原因.元素和元素大小.多余的空间用于存储家庭信息(即元素数).以后的delete[]表达式将检索该家庭信息,并使用它来调用正确数量的析构函数,然后才通过调用operator delete[]释放内存.

The important detail here is that an array created by new[] expression will normally be destroyed later by delete[] expression. The latter will need to know the number of objects to destruct (if the objects have non-trivial destructors), i.e. this information has to be passed somehow from new[] expression (when it was known) to the corresponding delete[] expression (when it is needed). In a typical implementation this information is stored inside the block allocated by new[] expression, which is why the memory size requested in the implicit call to operator new[] is normally greater than the product of the number of elements and the element size. The extra space is used to store the household information (number of elements, namely). Later delete[] expression will retrieve that household information and use it to invoke the correct number of destructors before actually freeing the memory by calling operator delete[].

在您的示例中,您没有使用任何这些机制.在您的示例中,您正在显式调用内存分配函数,手动执行构造并完全忽略销毁步骤(这是可以的,因为您的对象具有琐碎的析构函数),这意味着至少出于销毁的目的,您不需要跟踪确切的销毁步骤.数组中元素的数量.无论如何,您都可以在no变量中手动跟踪该数字.

In your example you are not using any of these mechanisms. In your example you are calling memory allocation functions explicitly, perform construction manually and completely ignore the destruction step (which is OK, since your object has trivial destructor), which means that at least for destruction purposes you don't need to track the exact number of elements in the array. In any case, you keep track of that number manually, in a no variable.

但是,在一般情况下是不可能的.通常,代码将使用new[]表达式和delete[]表达式,并且元素的数量必须以某种方式从new[]delete[],这意味着它必须在内部存储,这就是为什么需要这样做的原因用于数组的专用内存分配功能-operator new[].它不等于仅将上述乘积作为尺寸的operator new.

However, in general case it is not possible. In general case the code will use new[] expressions and delete[] expressions and the number of elements will have to get from new[] to delete[] somehow, meaning that it has to be stored internally, which is why there's a need for a dedicated memory allocation function for arrays - operator new[]. It is not equivalent to a mere operator new with the aforementioned product as size.

这篇关于运算符new()和运算符new []()之间的区别?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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