Take the 2-minute tour ×

I am having hard time digesting this syntax:

void* operator new[](std::size_t, const std::nothrow_t&) throw();

while this is still understood:

void* operator new (std::size_t size, const std::nothrow_t& nothrow_constant) throw();


  1. I thought new and subscript [] are different operators. how can we combine two operators to overload in one definition?

  2. Also the nowthrow. The following call doesnt make sense to me ( with respect to the signature of the function ).

     int * p2 = new (nothrow) int;

If anyone can give reference to related topics from bjarne stroustrup's book that would be great, not a hard requirement though.

UPDATE: Please try to answer both questions :)

share|improve this question
Stroustrup's books are not references -- the C++ standard is. You can find something quite close to the standard for free here. –  ildjarn Apr 6 '12 at 23:03
new[] is a distinct operator to new... –  Oli Charlesworth Apr 6 '12 at 23:04
I meant reference to related 'conceptual topic'. I am reading this book and just finished operator overloading and i thought i am done with everything related to that topic. But this definition throws me in tizzy. –  NulledPointer Apr 6 '12 at 23:05
Read up how the placement syntax for the new and new[] operators works, and then you'll understand how to parse the nothrow form of the operator and its corresponding allocation function. –  Kerrek SB Apr 6 '12 at 23:16
thanks @KerrekSB . "placement syntax" was something that was missing. Apparently there is a reference to it in "Classes" (10.4.11). I read it earlier but it just didnt occur to me while going over it. Its weired though that there is no mention of it in operator overloading, I think its crucial to have a chapter on new operator in "Operator overloading" chapter as well. –  NulledPointer Apr 9 '12 at 21:12
add comment

3 Answers

up vote 2 down vote accepted

The first two are the signatures of the global new operators. For what (little) it's worth, operator new is used to allocate space for a new expression like x = new T;, while operator new[] is used to allocate space for a new expression like x = new T[count];. The "little" that it's worth is for a fairly simple reason: you should never use new T[count], so how it works is almost purely a historical curiosity.

You can overload ::operator new and/or ::operator new[] to provide your own heap allocation if you want to. There's no difference between the two as far as basic requirements go -- they both just allocate and return a pointer to the amount of memory requested.

As far as nothrow goes, the size that gets passed to operator new is always computed by the compiler based on the size of the object and in the case of an array new the count you give. Therefore, the parameter you specify in the new expression turns into the second parameter that's passed to operator new.

To emphasize a point I may not have made quite clearly enough above: operator new (and operator new[]) are used by, but separate from new expressions (what you have in your code when you say something like x = new T;). operator new and operator new[] are pretty much like malloc -- they just allocate "raw" memory. A new expression1 uses one of those to allocate raw memory, then invokes the constructor to allocate an object (or more than one, in the case of new T[count];) in that memory. The two are obviously related, but equally obviously not really the same.

One other minor point: it's also possible to have an operator new (or operator new[] as a class member. This allows you to allocate memory differently for that class than for others that use the global heap. This tends to be most common with small objects that you expect to allocate in large numbers. For these, the global heap often has quite a lot of overhead that you'd prefer to avoid.

Finally, when/if you want to allocate raw memory, you can also invoke operator new directly, as in void *a = ::operator new(1234);. About the only place this is common is if you decide to implement some sort of collection class on your own (e.g., if you want a circular buffer).

share|improve this answer
add comment

new[] and [] are two completely different operators.

new[] is an allocation operator for arrays, [] is a subscript access operator for arrays.

new[] is it's own operator, it's not simply 'new' allocator combined with '[]' subscript operator.

share|improve this answer
add comment

new and new[] are two separate operators, not really related to the '[]' operator. This is best seen on the difference between delete p and delete[] p; are you deleting a single instance pointed to by p, or a whole array of instances?

Likewise, new[] is a way to allocate a whole array of objects, all in the same memory block.

share|improve this answer
add comment

Your Answer


