c++ - 为什么 C++ 分配器避免就地重新分配

标签 c++ memory memory-management stdvector

我对 realloc 的理解是,如果内存在分配点之外连续可用,它可以尝试扩展当前分配而不进行复制。

阅读本文时 https://github.com/facebook/folly/blob/master/folly/docs/FBVector.md我开始知道大多数分配器都避免就地重新分配

. 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.

正如在另一个关于 C++ 中缺少重新分配器的问题 ( Why is there no reallocation functionality in C++ allocators? ) 中所回答的那样,接受的答案提到这样的分配器会禁止 realloc 使用 form C 库,但没有回答为什么不尝试使 realloc 扩展当前内存(如果可能)?

最佳答案

关于c++ - 为什么 C++ 分配器避免就地重新分配,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25596218/

相关文章:

c++ - fork() 到底在做什么?在我的 C++ 程序中

c++ - 将 C 实现的静态分配工作空间转换为 C++

c - 当我溢出分配的字符数组时,为什么我的 C 程序不会崩溃?

c++ - 编译器是否优化对常量变量的引用?

java - 没有数据成员的类在java中有内存占用吗?

windows - Windows 上的\Device\PhysicalMemory 需要澄清

c++ - 初始化条件变量和/或互斥体时出现问题——在 IPC 设置中

c++ - linux-get C++ 中 PID 的任务状态

c++ - 语法错误 : missing ';' before identifier

c++ - 如何找出我的 Linux 应用程序的内存占用情况?