Callling object constructor/destructor with a custom allocator

11,441

Solution 1

In essence, when you use a new expression like: T *t = new T;, it's roughly equivalent to:

void *temp = operator new(sizeof(T));
T *t = new(temp) T;

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: delete t;, it's roughly equivalent to:

t->~T();
operator delete(t);

So, if you overload new and delete for a particular class:

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

Then when you use a new expression, it'll invoke the class' operator new to allocate the memory, and that will call malloc, so T *t = new T(); will end up allocating memory via malloc (and likewise, when you delete it, it'll use operator delete, which will call free).

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.

Ignoring, for the moment, the details about what names are used for things, the Allocator class in the standard library mostly does the same, so with a little renaming of the functions in the T class above, you'd be about half done writing a standard allocator. To go with the allocation and deletion, it has a function to rebind 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 operator new and operator delete instead of malloc and free like I've used above.

Solution 2

With a placement new you can pass an already allocated memory location to the new operator. Then new will construct the object at the given place without doing an allocation on itself.

Edit:

This is how it could be implemented:

int main(void){
    // get memory
    void * mem_t = SomeAllocationFunction(sizeof(SomeClass));
    // construct instance
    SomeClass* t = new(mem_t) SomeClass;

    // more code

    // clean up instance
    t->~SomeClass();
    return 0;
}
Share:
11,441
mmurphy
Author by

mmurphy

Updated on June 05, 2022

Comments

  • mmurphy
    mmurphy almost 2 years

    I have been looking into custom allocators and I quite often see them using some kind of function to allocate memory. For testing purposes and further educate my self, I tried to make a "simple" example of doing so. However, there is one fundamental thing I am understand on how to do. One of the key differences in malloc vs new is that with new the constructor is called. What if I wanted to write my own allocator that was essentially replacing new, how would I get the constructor to be called when using malloc?

    I understand that on classes I can overload new and delete for the class, so I suppose a big part of the question is, how is new calling the objects constructor during allocation? Similarly, I am interested in how delete is calling the destructor.

    I created a sample test code that I was hoping to have the SomeClass constructor called during allocation, but I don't see how.

    #include <malloc.h>
    
    void* SomeAllocationFunction(size_t size) {
        return malloc(size);
    }
    
    class SomeClass
    {
    public:
        SomeClass() {
            int con = 1000;
        }
    
        ~SomeClass() {
            int des = 80;
        }
    };
    
    int main(void){
        SomeClass* t = (SomeClass*)SomeAllocationFunction(sizeof(SomeClass));
        return 0;
    }
    

    (As a note, I know I can just use new. However, for the purposes of learning I am trying to create a custom allocator that does not just call new or placement new).

  • josephthomas
    josephthomas about 12 years
    The poster said he does not want to call new.
  • bjhend
    bjhend about 12 years
    Constructors don't have a name (opposed to destructors) so they cannot be called directly. A placement new is the only way to properly construct a class instance if you want it to occupy a given memory location.
  • mmurphy
    mmurphy about 12 years
    I specifically asked without using new.
  • mmurphy
    mmurphy about 12 years
    I specifically asked without using new.
  • qehgt
    qehgt about 12 years
    @mmurphy placement new and new are different operators.
  • mmurphy
    mmurphy about 12 years
    When I said new I meant all types of new. I updated my question to represent that.
  • qehgt
    qehgt about 12 years
    @mmurphy There is no way to call constructor SomeClass::SomeClass without new or placement new.
  • mmurphy
    mmurphy about 12 years
    If that is true, then how come for example MSVC's new does not call placement new in its version of new (in new.cpp, you can get it by stepping into a new call).
  • mmurphy
    mmurphy about 12 years
    If placement new is what I need to use, then how come for example MSVC's new does not call placement new in its version of new (in new.cpp, you can get it by stepping into a new call).
  • qehgt
    qehgt about 12 years
    Non-placement version of new does follow things. First, it allocates memory (and you saw it in new.cpp, it's part of the RTL). Second, call the constructor of the class (compiler generates this code, you can see it in disassembler). There is no need to call placement new from new.cpp
  • Jerry Coffin
    Jerry Coffin about 12 years
    @mmurphy: Roughly similar to, definitely not identical to.
  • stonemetal
    stonemetal about 12 years
    Yes but placement new is a language primitive, there is no way to implement it in the language.
  • ulatekh
    ulatekh almost 10 years
    @stonemetal: Placement-new is not a language primitive, i.e. void *operator new (size_t bytes, void *place) { return place; }