“新的"和“新的"之间的区别.运算符和“新"功能 [英] Difference between "new" operator and "new" function
问题描述
面试问题:新"运算符和新"功能有什么区别?
Interview question: what's the difference between "new" operator and "new" function?
我回答说,他们运行相同的代码没有什么区别,但是面试官一直在针刺我,因为那是错误的答案.
I answered there is no difference, that they run the same code, but interviewer kept needling me like that was the wrong answer.
是错误的答案吗?还是面试官只是和我一起玩游戏?
Is it the wrong answer? Or was the interviewer just playing games with me?
如果答案错误,那么正确的答案是什么?
If it's the wrong answer, what's the right answer?
我继续说,如果您需要自定义分配,则可以将"new"运算符重载,但是他想知道如何重载它.当然,我没有这个答案,从来没有需要,但是我告诉他我可以在10分钟内查找出来(这是永远在面试中的正确答案).
I continued that the "new" operator could be overloaded if you needed a custom allocation, but then he wanted to know how to overload it. Of course I didn't have that answer, having never had the need, but I told him I could look it up in 10 minutes (which is never the right answer in an interview).
所以无论如何,在对新"运算符与新"功能进行了一些研究之后,没有看到任何真正令人满意的答案,我想我会问一个具体的问题.
So anyhow, having done some research on "new" operator vs. "new" function and not seeing any really satisfying answers, I thought I'd ask the specific question.
推荐答案
new
运算符和operator new
是不同的东西.
The new
operator and operator new
are not the same thing.
new
运算符调用operator new
函数来分配内存,然后根据分配的类型和使用的语法来初始化或调用分配的内存上的构造函数.换句话说,operator new
仅构成new
运算符操作的一部分.
The new
operator calls an operator new
function to allocate memory, and then, depending on the type allocated and the syntax used, initializes or calls a constructor on the allocated memory. In other words, operator new
forms only a part of the operation of the new
operator.
operator new
是由new
运算符调用的分配内存的函数.有一个默认实现operator new
,可以将其替换为 ,这与重载不是一回事.还可以为特定类型实现operator new
,以仅处理该类型的对象或operator new
的分配.可以重载,并且可以使用new
运算符的placement new形式来选择重载.
operator new
is the function called to allocate memory by the new
operator. There's a default implementation of operator new
which can be replaced, which is not the same thing as overloading. operator new
can also be implemented for a particular type to handle allocations only of objects of that type, or operator new
can be overloaded and the overload can be selected by using the placement new form of the new
operator.
operator new
的默认实现可以通过定义具有以下签名的函数来替换:
The default implementations of operator new
can be replaced by defining functions with the following signatures:
void *operator new(std::size_t size);
void *operator new(std::size_t size, const std::nothrow_t&);
void *operator new[](std::size_t size);
void *operator new[](std::size_t size, const std::nothrow_t&);
为operator new
提供替换或重载时,应提供相应的operator delete
功能:
When you provide a replacement or overload for operator new
you should provide corresponding operator delete
functions:
void operator delete(void* ptr) noexcept;
void operator delete(void* ptr, const std::nothrow_t&) noexcept;
void operator delete[](void* ptr) noexcept;
void operator delete[](void* ptr, const std::nothrow_t&) noexcept;
要提供operator new
的重载以用于new
运算符的放置形式,您可以添加其他参数(operator new
和operator delete
的空位版本可以做到这一点).
To provide an overload of operator new
for use with the placement form of the new
operator you can add additional arguments (the nothrow versions of operator new
and operator delete
do this).
struct my_type {};
void *operator new(std::size_t size, const my_type&);
void operator delete(void *ptr, const my_type&);
new (my_type()) int(10); // allocate an int using the operator new that takes a my_type object
delete
运算符没有放置删除"形式.提供operator delete
的重载是因为如果在内存的初始化/构造过程中发生错误(例如,在调用operator new
之后由new
运算符调用的构造函数),则相应的operator delete
被调用在重新引发异常之前存在.否则,将引发异常时不调用operator delete
并内存泄漏.
There is no 'placement delete' form of the delete
operator. The overload of operator delete
is provided because if an error occurs during the initialization/construction of the memory (e.g., the constructor called by the new
operator after operator new
has been called) the corresponding operator delete
is called if it exists before re-throwing the exception. Otherwise operator delete
is not called and the memory leaks when the exception is thrown.
这篇关于“新的"和“新的"之间的区别.运算符和“新"功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!