Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Difference between "new" operator and "new" function [duplicate]

Tags:

c++

operators

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.

like image 468
UncaAlby Avatar asked Aug 12 '12 07:08

UncaAlby


People also ask

What is the difference between operator new and the new operator?

new vs operator new in C++ When you create a new object, memory is allocated using operator new function and then the constructor is invoked to initialize the memory. Here, The new operator does both the allocation and the initialization, where as the operator new only does the allocation.

What is the new operator?

The new operator lets developers create an instance of a user-defined object type or of one of the built-in object types that has a constructor function.

Is new A function or operator?

The new operator invokes the function operator new . For arrays of any type, and for objects that aren't class , struct , or union types, a global function, ::operator new , is called to allocate storage. Class-type objects can define their own operator new static member function on a per-class basis.

What is difference between new and delete operators?

Difference Between new and delete Operator in C++ In brief, new is an operator in C++ that allocates memory for an object or an array of objects. In contrast, delete is an operator in C++ that deallocates a block of memory previously allocated for an object created using the new operator.


2 Answers

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.

like image 145
bames53 Avatar answered Oct 24 '22 17:10

bames53


Basically:- Function: "operator new"

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

"new operator":

Example* eg = new Example();
like image 35
Kiran Solkar Avatar answered Oct 24 '22 17:10

Kiran Solkar