Realloc- in Standard allocator

后端 未结 3 1927
旧时难觅i
旧时难觅i 2021-02-02 16:41

For the Standard allocator interface for use in, say, std::vector, is re-allocation supported? I have a rather specific use-case in which being able to reallo

相关标签:
3条回答
  • 2021-02-02 16:53

    We tried but failed:

    http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n1953.html

    http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2006/n2045.html

    Edit: I know what you meant. ;-)

    0 讨论(0)
  • 2021-02-02 17:02

    You can put a bool flag in the structure you are saving in the std::vector to indicate it its logically deleted or not. To delete an element set this flag to true but don't physically delete it. To reallocate an element, look for an flag that's true, set it to false to show its not deleted and that use it.

    0 讨论(0)
  • 2021-02-02 17:13

    I believe that realloc() is not part of the STL allocator interface. But realloc() is always a crap-shoot anyway, since you don't really know whether your OS will expand your allocation or move you to a new one. Actual performance is very OS-dependent. If you know you want to reallocate, you might as well just alloc a bigger chunk of memory in advance, which luckily the STL makes easy.

    Do you have a use case where this would be undesirable?

    0 讨论(0)
提交回复
热议问题