Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why do C++ allocators avoid in-place reallocation

My understanding of realloc was that, if memory was available contiguously beyond the point allocated, it can try to extend the current allocation without copying.

While reading this https://github.com/facebook/folly/blob/master/folly/docs/FBVector.md I came to know that most allocators avoid inplace reallocation

. Many memory allocators do not support in- place reallocation, although most of them could. This comes from the now notorious design of realloc() to opaquely perform either in-place reallocation or an allocate-memcpy-deallocate cycle. Such lack of control subsequently forced all clib-based allocator designs to avoid in-place reallocation, and that includes C++'s new and std:allocator.

As answered in another question (Why is there no reallocation functionality in C++ allocators?) about the lack of reallocators in C++ where the accepted answer mentions that such an allocator would have prohibited realloc use form C library but does not answer why not try to make realloc that expands the current memory if possible?

like image 863
sukunrt Avatar asked Aug 31 '14 20:08

sukunrt


1 Answers

Why not make one? Actually, there are proposals to do exactly that:

  • 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
  • http://www.open-std.org/jtc1/sc22/wg21/docs/papers/2013/n3495.htm
like image 159
Ben Voigt Avatar answered Sep 25 '22 15:09

Ben Voigt