用于堆上内存分配的新运算符
我正在看新操作员的签名。即:
void* operator new (std::size_t size) throw (std::bad_alloc);
但是当我们使用这个运算符时,我们从不使用强制转换。即
int *arr = new int;
,在这种情况下,C++ 如何将 void*
类型的指针转换为 int*
。因为,即使 malloc
返回一个 void*
并且我们需要显式使用强制转换。
I was looking at the signature of new operator. Which is:
void* operator new (std::size_t size) throw (std::bad_alloc);
But when we use this operator, we never use a cast. i.e
int *arr = new int;
So, how does C++ convert a pointer of type void*
to int*
in this case. Because, even malloc
returns a void*
and we need to explicitly use a cast.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
在 C++ 中,
operator new
和new
运算符之间存在非常微妙的差异。 (再读一遍……顺序很重要!)operator new
函数是 C++malloc
函数的 C++ 模拟。它是一个原始内存分配器,其职责只是生成一个用于构造对象的内存块。它不调用任何构造函数,因为那不是它的工作。通常,您不会在 C++ 代码中看到直接使用operator new
;看起来有点奇怪。例如:new
运算符是一个关键字,负责为对象分配内存并调用其构造函数。这是 C++ 代码中最常遇到的情况。当您编写时,您正在使用 new 运算符来分配新整数。在内部,
new
运算符的工作原理大致如下:operator new
分配内存来保存所请求的对象。operator delete
释放上述内存,然后传播异常。由于
new
运算符和operator new
是分开的,因此可以使用new
关键字来构造对象,而无需实际分配任何内存。例如,著名的placement new允许您在用户提供的内存中的任意内存地址构建对象。例如:通过定义自定义
operator new
函数来重载new
运算符,可以让您以这种方式使用new
;您指定如何进行分配,C++ 编译器会将其连接到new
运算符中。如果您好奇的话,
delete
关键字的工作方式相同。有一个名为operator delete
的释放函数负责处理内存,还有一个delete
运算符负责调用对象析构函数并释放内存。但是,operator new
和operator delete
可以在这些上下文之外使用,以代替 C 的malloc
和free
,例如。There is a very subtle difference in C++ between
operator new
and thenew
operator. (Read that over again... the ordering is important!)The function
operator new
is the C++ analog of C'smalloc
function. It's a raw memory allocator whose responsibility is solely to produce a block of memory on which to construct objects. It doesn't invoke any constructors, because that's not its job. Usually, you will not seeoperator new
used directly in C++ code; it looks a bit weird. For example:The
new
operator is a keyword that is responsible for allocating memory for an object and invoking its constructor. This is what's encountered most commonly in C++ code. When you writeYou are using the new operator to allocate a new integer. Internally, the
new
operator works roughly like this:operator new
.operator delete
, then propagate the exception.Because the
new
operator andoperator new
are separate, it's possible to use thenew
keyword to construct objects without actually allocating any memory. For example, the famous placement new allows you to build an object at an arbitrary memory address in user-provided memory. For example:Overloading the
new
operator by defining a customoperator new
function lets you usenew
in this way; you specify how the allocation occurs, and the C++ compiler will wire it into thenew
operator.In case you're curious, the
delete
keyword works in a same way. There's a deallocation function calledoperator delete
responsible for disposing of memory, and also adelete
operator responsible for invoking object destructors and freeing memory. However,operator new
andoperator delete
can be used outside of these contexts in place of C'smalloc
andfree
, for example.您将
new
表达式与operator new()
函数混淆了。当前者被编译时,编译器会生成对operator new()函数的调用,并传递足够的大小来保存new表达式中提到的类型,然后传递一个指针返回该类型。You confuse
new
expression withoperator new()
function. When the former is compiled the compiler among other stuff generates a call tooperator new()
function and passes size enough to hold the type mentioned in thenew
expression and then a pointer of that type is returned.