简体   繁体   中英

How the default stl allocator allocates?

I have my objects use overloaded new/delete operators as I use memory pools to allocate their memory. They looks something like this:

class Foo{

 public:  
    void* operator new(size_t nbytes){

        return MemoryManager::GetInstance().AllocFooTypeMemory();

    }
    void operator delete(void* p)
    {
        MemoryManager::GetInstance().FreeFooTypeMemory(p);
    }

};

Now,my question is how it then works when I create an STL container of Foo pointers? Here it is written that the default STL allocator uses ::operator new to allocate memory in the container.When does it use it?I tried to see if Foo's new gets triggered when calling

std::vector<Foo*> fooVector;
fooVector.reserve(10);

but nothing happens.

I have several questions:

Am I right to imply that in this case the allocator allocates only memory for pointers?

Does it mean the allocator calls "new" on Foo when the container holds objects and not pointers?

Also if yes,does it mean then I don't need to write custom allocator with mem pool if I already use a pool to allocate memory in the objects(like in Foo example)?

UPDATE:

To make it clear,I take into account that only the object which overload new/delete with memory pool are related to this question.I am completely aware that the object which do not have new/deleted overload still use default heap as a source for dynamic memory allocation.

The default allocator can't use a class specific operator new , because it doesn't contain a new expression; it calls the ::operator new function directly, separating allocation and initialization.

In your case, of course, you don't have a container of Foo , but of Foo* , so anything you define in Foo is irrelevant (and you can't define an operator new for a pointer).

With regards to your specific questions:

  • In an std::vector<Foo*> , the vector will only allocate pointers. How could it do anything else?

  • If the container contains Foo , rather than Foo* , the issue is more complicated; the default allocator will use ::operator new to allocate the memory, and ::new (size_t, void*)... to construct the object.

  • You don't want to use a memory pool for objects in a container. If the container is std::vector , the objects will be in contiguous memory, which generally shouldn't be taken from the pool, and for other types of containers, what is allocated is often not a Foo at all, but some more complex type which contains a Foo .

Overaloading new and delete operator, in your case, applies to Foo class only. That means, following calls use your custom new and deleteL:

Foo * p - new Foo();
delete p;

And rest of the dynamic memory allocations using new and delete use the default, not yours. Instead, you may try to overalod global new and delete operator instead of being Foo specific.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM