Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>In essence, when you use a new expression like: <code>T *t = new T;</code>, it's roughly equivalent to:</p> <pre><code>void *temp = operator new(sizeof(T)); T *t = new(temp) T; </code></pre> <p>So, first it allocates some raw memory using the allocation function, then it constructs an object in that memory. Likewise, when you use a delete expression like: <code>delete t;</code>, it's roughly equivalent to:</p> <pre><code>t-&gt;~T(); operator delete(t); </code></pre> <p>So, if you overload <code>new</code> and <code>delete</code> for a particular class:</p> <pre><code>class T { int data; public: // I've made these static explicitly, but they'll be static even if you don't. static void *operator new(size_t size) { return malloc(size); } static void operator delete(void *block) { free(block); } }; </code></pre> <p>Then when you use a new expression, it'll invoke the class' <code>operator new</code> to allocate the memory, and that will call <code>malloc</code>, so <code>T *t = new T();</code> will end up allocating memory via <code>malloc</code> (and likewise, when you <code>delete</code> it, it'll use <code>operator delete</code>, which will call <code>free</code>).</p> <p>At least as the term is normally used, an Allocator is quite similar, except that it's used by a container instead of other code. It also encapsulates the allocation function and deletion function into a class, so when you pass one to the container, you only have to pass one object, and there's little chance of an allocation and delete function getting mismatched.</p> <p>Ignoring, for the moment, the details about what names are used for things, the Allocator class in the standard library <em>mostly</em> does the same, so with a little renaming of the functions in the <code>T</code> class above, you'd be about half done writing a standard allocator. To go with the allocation and deletion, it has a function to <code>rebind</code> some memory (change a block of memory to another type), create an object in place (basically just a wrapper around a placement new) and destroy an object (again, trivial wrapper around destructor invocation). Of course, it uses <code>operator new</code> and <code>operator delete</code> instead of <code>malloc</code> and <code>free</code> like I've used above.</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.
 

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