Callling object constructor/destructor with a custom allocator

后端 未结 3 1562
一个人的身影
一个人的身影 2020-12-30 08:25

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 tri

3条回答
  •  被撕碎了的回忆
    2020-12-30 09:06

    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.

提交回复
热议问题