Does STL Vector use 'new' and 'delete' for memory allocation by default?

前端 未结 5 1720
小鲜肉
小鲜肉 2021-02-08 14:57

I am working on a plugin for an application, where the memory should be allocated by the Application and keep track of it. Hence, memory handles should be obtained from the host

5条回答
  •  既然无缘
    2021-02-08 15:33

    STL containers use an allocator they are given at construction time, with a default allocator that uses operator new and operator delete.

    If you find the default is not working for you, you can provide a custom allocator that conforms to the container's requirements. There are some real-world examples cited here.

    I would measure performance using the default first, and optimize only if you really need to. The allocator abstraction offers you a relatively clean way to fine-tune here without major redesign. How you use the vector could have far more performance impact than the underlying allocator (reserve() in advance, avoid insert and removal in the middle of the range of elements, handle copy construction of elements efficiently - the standard caveats).

提交回复
热议问题