Why is there no reallocation functionality in C++ allocators?

前端 未结 5 1065
暖寄归人
暖寄归人 2020-11-29 06:56

In C the standard memory handling functions are malloc(), realloc() and free(). However, C++ stdlib allocators only parallel two of t

相关标签:
5条回答
  • 2020-11-29 07:37

    I guess this is one of the things where god went wrong, but I was just too lazy to write to the standards committee.

    There should have been a realloc for array allocations:

    p = renew(p) [128];

    or something like that.

    0 讨论(0)
  • 2020-11-29 07:41

    What you're asking for is essentially what vector::reserve does. Without move semantics for objects, there's no way to reallocate memory and move the objects around without doing a copy and destroy.

    0 讨论(0)
  • 2020-11-29 07:43

    From: http://www.sgi.com/tech/stl/alloc.html

    This is probably the most questionable design decision. It would have probably been a bit more useful to provide a version of reallocate that either changed the size of the existing object without copying or returned NULL. This would have made it directly useful for objects with copy constructors. It would also have avoided unnecessary copying in cases in which the original object had not been completely filled in.

    Unfortunately, this would have prohibited use of realloc from the C library. This in turn would have added complexity to many allocator implementations, and would have made interaction with memory-debugging tools more difficult. Thus we decided against this alternative.

    0 讨论(0)
  • 2020-11-29 07:47

    Because of the object oriented nature of C++, and the inclusion of the various standard container types, I think it's simply that less focus was placed on direction memory management than in C. I agree that there are cases that a realloc() would be useful, but the pressure to remedy this is minimal, as almost all of the resulting functionality can be gained by using containers instead.

    0 讨论(0)
  • 2020-11-29 07:50

    This is actually a design flaw that Alexandrescu points out with the standard allocators (not operator new[]/delete[] but what were originally the stl allocators used to implement std::vector, e.g.).

    A realloc can occur significantly faster than a malloc, memcpy, and free. However, while the actual memory block can be resized, it can also move memory to a new location. In the latter case, if the memory block consists of non-PODs, all objects will need to be destroyed and copy-constructed after the realloc.

    The main thing the standard library needs to accommodate this as a possibility is a reallocate function as part of the standard allocator's public interface. A class like std::vector could certainly use it even if the default implementation is to malloc the newly sized block and free the old one. It would need to be a function that is capable of destroying and copy-constructing the objects in memory though, it cannot treat the memory in an opaque fashion if it did this. There's a little complexity involved there and would require some more template work which may be why it was omitted from the standard library.

    std::vector<...>::reserve is not sufficient: it addresses a different case where the size of the container can be anticipated. For truly variable-sized lists, a realloc solution could make contiguous containers like std::vector a lot faster, especially if it can deal with realloc cases where the memory block was successfully resized without being moved, in which case it can omit calling copy constructors and destructors for the objects in memory.

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