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

前端 未结 5 1092
暖寄归人
暖寄归人 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: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.

提交回复
热议问题