-->

Difference between “new” operator and “new” functi

2020-08-24 04:38发布

问题:

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?

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.

回答1:

The new operator and operator new are not the same thing.

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 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.

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&);

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;

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

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.



回答2:

Basically:- Function: "operator new"

 class Example 
{ public:
         void* operator new( size_t );
 }

"new operator":

Example* eg = new Example();


回答3:

I continued that the "new" operator could be overloaded if you needed a custom allocation

You are almost right. new is a keyword for an operator, which is used for memory allocation.

Why is this an operator ?
On need basis, it can be overloaded as a function both at global scope or class scope (but NOT namespace scope!). This wouldn't have been possible if it was a function.



回答4:

The difference is in how they function. The initial allocation part is, per standard I believe, the same. That is, using syntax new vs operator new() explicitly is very much the same. The difference, is using new initializes or constructs the new object. There is also 3 different versions of ::operator new() and there is various syntaxes to utilize them as well (i.e., placement new).



回答5:

There is no new function. My guess is that they wanted you to say that one of these allocated the memory (the standard uses allocator function, function new operator and new operator for it) and that the other used the first to allocate memory, then called the constructor (the standard uses new expression) and the deallocator function (aka function delete operator or delete operator) to free the memory if the constructor exited with an exception.