Difference between 'new operator' and '

2019-01-02 17:32发布

问题:

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

回答1:

I usually try to phrase things differently to differentiate between the two a bit better, but it's a good question in any case.

Operator new is a function that allocates raw memory -- at least conceptually, it's not much different from malloc(). Though it's fairly unusual unless you're writing something like your own container, you can call operator new directly, like:

char *x = static_cast<char *>(operator new(100));

It's also possible to overload operator new either globally, or for a specific class. IIRC, the signature is:

void *operator new(size_t);

Of course, if you overload an operator new (either global or for a class), you'll also want/need to overload the matching operator delete as well. For what it's worth, there's also a separate operator new[] that's used to allocate memory for arrays -- but you're almost certainly better off ignoring that whole mess completely.

The new operator is what you normally use to create an object from the free store:

my_class *x = new my_class(0);

The difference between the two is that operator new just allocates raw memory, nothing else. The new operator starts by using operator new to allocate memory, but then it invokes the constructor for the right type of object, so the result is a real live object created in that memory. If that object contains any other objects (either embedded or as base classes) those constructors as invoked as well.



回答2:

"operator new"

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

"new operator":

Foo* foo = new Foo();

In this example, new Foo() calls Foo::operator new()

In other words, "new operator" calls "operator new()" just like the + operator calls operator +()



回答3:

Following is the quote from More Effective C++ book from Scott Meyers:

The new operator calls a function to perform the requisite memory allocation, and you can rewrite or overload that function to change its behavior. The name of the function the new operator calls to allocate memory is operator new.



回答4:

There's no difference between "new operator" and "operator new". Both refer to the same thing: the overloadable/replaceable operator new function that typically performs raw memory allocation for objects created by new-expressions.

Note also that neither term is present in the language specification (which is the defining source of the official terminology).

When you use new in your program to create an object, it is called new-expression. New-expression consists of keyword new and additional syntactic parts defined by the grammar. No part of this expression's syntax is ever referred to as an "operator".

The raw memory allocation function operator new is officially referred to as just "operator new function". Note that the words operator and new in this sequence are just two separate C++ language keywords. They don't form an English term "operator new". Nowhere in the language specification you'll find any references to "operator new" as an English term. Every time this is just a combination of two independent keywords that produce declaration syntax for a memory allocation function.

Again, in resume: formally in C++ there's no such English language terms as "operator new" or "new operator". The former sequence is present in the language specification as a mere combination of keywords, not as an English term. The latter is not present at all.



回答5:

When you create a new object the memory is allocated using operator new then the constructor is invoked to initialise the memory. The new operator does both the allocation and the initialisation, where as the operator new only does the allocation.



回答6:

The OP's question is not phrased properly. It's better to phase as 'Difference between 'operator new' and 'new expression'?' Note 'operator new' often refers to 'operator new function' as well.

And there are plenty correct answer around, below is mine:

1> 'new expression' call 'operator new' to allocate raw memory,then call constructor

apple * p = new apple(); //new expression

2> 'operator new' only allocate raw memory, not much difference than malloc

void* mem = operator new(sizeof(apple)); //just like calling malloc()
apple* p2 = new(mem) apple(1); //call construct here using placement new.


回答7:

The new operator: C++ supports dynamic allocation of objects using the new operator. The new operator allocate memory for objects from a pool called the free store. The new operator calls the special function operator new.

operator new: If the request is for zero bytes of storage, operator new returns a pointer to a distinct object (that is, repeated calls to operator new return different pointers). If there is insufficient memory for the allocation request, operator new returns NULL or throws an exception. The first argument to operator new must be of type size_t (a type defined in STDDEF.H), and the return type is always void *.

Here is a MSDN links for more details:

The operator new Function

The new Operator



回答8:

  1. new is an operator as well as a keyword.

    see [1] In 2.13 && In 2.12.

  2. new does two things: T* t = new T(arg);

    1)allocate memory for the object: void* ptr = operator new(sizeof(T));

    //operator new is a function(just like malloc in c), not an operator.(see [1] In 3.7.4 ). But Item 7 [2] said it is an operator too. In my opinion, the difference between operator and function is small, and you can see it when you recall that overloading operator is implemented by functions.

    //we can overload this operator/function(operator new) doing what we want just here.

    2)initialize the object in the allocated memory: call T::T(arg) on ptr

    //only compiler can do this. Neither me nor you can.

    //also compiler will invoke member objects' constructors and base class's constructor if T has them. And this invocation is recursive. Only compiler can do that.

  3. So, operator new does part of the missions of new, and only in this part we can do something.

    [1]: ISO/IEC, N3690. http://ww.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3690.pdf

    [2]: Meyers, Scott. Effective C++, 3rd.



标签: