NP Rooski  Z
NP Rooski Z

Reputation: 3657

operator new[] definition breakdown

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

Question:

  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 :)

Upvotes: 1

Views: 184

Answers (3)

Jerry Coffin
Jerry Coffin

Reputation: 490108

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

Upvotes: 2

Jirka Hanika
Jirka Hanika

Reputation: 13529

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.

Upvotes: 1

Faris M
Faris M

Reputation: 540

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.

Upvotes: 3

Related Questions