Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>The first two are the signatures of the global new operators. For what (little) it's worth, <code>operator new</code> is used to allocate space for a new expression like <code>x = new T;</code>, while <code>operator new[]</code> is used to allocate space for a new expression like <code>x = new T[count];</code>. The "little" that it's worth is for a fairly simple reason: you should never use <code>new T[count]</code>, so how it works is almost purely a historical curiosity.</p> <p>You can overload <code>::operator new</code> and/or <code>::operator new[]</code> to provide your own heap allocation if you want to. There's <em>no difference</em> between the two as far as basic requirements go -- they both just allocate and return a pointer to the amount of memory requested.</p> <p>As far as <code>nothrow</code> goes, the size that gets passed to <code>operator new</code> 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 <code>operator new</code>.</p> <p>To emphasize a point I may not have made quite clearly enough above: <code>operator new</code> (and <code>operator new[]</code>) are used by, but separate from new expressions (what you have in your code when you say something like <code>x = new T;</code>). <code>operator new</code> and <code>operator new[]</code> are pretty much like <code>malloc</code> -- they just allocate "raw" memory. A new expression<sup>1</sup> uses one of those to allocate raw memory, then invokes the constructor to allocate an object (or more than one, in the case of <code>new T[count];</code>) in that memory. The two are obviously related, but equally obviously not really the same.</p> <p>One other minor point: it's also possible to have an <code>operator new</code> (or <code>operator new[]</code> 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.</p> <p>Finally, when/if you want to allocate raw memory, you can also invoke <code>operator new</code> directly, as in <code>void *a = ::operator new(1234);</code>. 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).</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload