Carousel
Carousel

Reputation: 738

Is `operator new` a part of C++ core language?

I have been told that "new-expression will call operator new to manage dynamic storage and initialize the object at the same time" many times. And I don't doubt about that. But I'm wondering that since operator new is declared in standard library header <new>, how could we still using new-expression even if we include no header files.

Is operator new a part of C++ core language or the compiler includes <new> implicitly?

Upvotes: 4

Views: 396

Answers (5)

Krzysztof Bargieł
Krzysztof Bargieł

Reputation: 143

The default allocation and deallocation functions are special components of the standard library; They have the following unique properties:

Global: All three versions of operator new are declared in the global namespace, not within the std namespace.
Implicit: The allocating versions ((1) and (2)) are implicitly declared in every translation unit of a C++ program, no matter whether header is included or not.
Replaceable: The allocating versions ((1) and (2)) are also replaceable: A program may provide its own definition that replaces the one provided by default to produce the result described above, or can overload it for specific types.

Source: http://www.cplusplus.com/reference/new/operator%20new/

Upvotes: 0

rustyx
rustyx

Reputation: 85371

Yes, operator new is part of the C++ standard.

See [expr.new].

Most of #include <new> is implicitly declared. But #include <new> declares some more versions of operator new, which are not implicit, for example placement new*:

void* operator new  (std::size_t size, void* ptr) noexcept;
void* operator new[](std::size_t size, void* ptr) noexcept;

* placement new is not recommended for daily use.

Upvotes: 4

Ferenc Deak
Ferenc Deak

Reputation: 35418

I agree, this tends to be confusing, but there are a lot of news in C++ (pun intended):

  1. One of them is the "new operator"
  2. Another one is "operator new" a function doing the actual memory allocation
  3. There is placement new
  4. and some might not like the name, but there is also array news (new[])

Not considering 3, 4 because they are worth a post of their own (What uses are there for "placement new"? and How do I use arrays in C++?), the first one ("new operator") resides in the global namespace, and it is accessible with the the scope-resolution operator (::), and the second one is the one you can overload in your classes. However regarding the relation of these two, there is an excellent explanation in MSDN (https://msdn.microsoft.com/en-us/library/kewsb8ba.aspx) I just paste it here:

The new operator invokes the function operator new. For arrays of any type, and for objects that are not of 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. When the compiler encounters the new operator to allocate an object of type type, it issues a call to type::operator new( sizeof( type ) ) or, if no user-defined operator new is defined, ::operator new( sizeof( type ) ). Therefore, the new operator can allocate the correct amount of memory for the object.

Upvotes: 2

NathanOliver
NathanOliver

Reputation: 180660

Yes operator new is part of the standard. The implementation is required to supply the operator at the global scope in each translation unit in your program. From [basic.stc.dynamic]/2

The library provides default definitions for the global allocation and deallocation functions. Some global allocation and deallocation functions are replaceable (18.6.1). A C++ program shall provide at most one definition of a replaceable allocation or deallocation function. Any such function definition replaces the default version provided in the library (17.6.4.6). The following allocation and deallocation functions (18.6) are implicitly declared in global scope in each translation unit of a program.

void* operator new(std::size_t);
void* operator new[](std::size_t);
void operator delete(void*);
void operator delete[](void*);
void operator delete(void*, std::size_t) noexcept;
void operator delete[](void*, std::size_t) noexcept;

emphasis mine

This is why you do not need to include anything to use new/new[] and delete/delete[].

Upvotes: 5

The header only provides declarations for all variants of the global operator new. You can still override them without including the header, as long as you get the operator signature right.

Upvotes: 0

Related Questions