为什么在C ++分配器中没有重新分配功能? [英] Why is there no reallocation functionality in C++ allocators?
问题描述
在C中,标准内存处理函数 malloc()
, realloc()
和 free()
。但是,C ++的stdlib分配器只有两个并行:没有重分配功能。当然,不可能做到与 realloc()
完全相同,因为只是复制内存不适合非聚合类型。但是,这个函数会有一个问题:
bool reallocate(pointer ptr,size_type num_now,size_type num_requested);
其中
-
ptr
; -
num_request
> =num_now
;
<之前为
num_now
个对象分配了相同的分配器和语义如下:
- 如果分配器可以扩展给定内存块
ptr
从num_now
对象到num_requested
对象,它会这样做(留下额外的内存未初始化)true
; - 否则不执行任何操作,并返回
false
。 >
授权,这不是很简单,但是我理解的分配器主要是用于容器和容器的代码通常是复杂的。 / p>
给定这样的函数, std :: vector
,可以增长如下(伪代码):
if(allocator.reallocate(buffer,capacity,new_capacity))
capacity = new_capacity; //这就是我们需要做的
else
... //使用不同的缓冲区进行标准重新分配
//复制数据并释放当前的
不能改变内存大小的分配器可以通过无条件的 return false来实现这样的函数;
。
有没有重新分配的分配器实现,这不值得麻烦呢?或者有一些我忽略的问题?
From:
http://www.sgi.com/tech/stl/alloc.html
这可能是最有问题的
设计决策。它可能有
可能更有用
提供一个版本的重新分配
或者更改
现有对象的大小而不复制或
返回NULL。这将使它
直接对有副本
构造函数的对象有用。它也将有
避免不必要的复制在
,其中原始对象没有
完全填写。
不幸的是,这将有
禁止使用realloc从C
库。这反过来会为许多分配器
实现添加
复杂性,并且会使
与内存调试
工具的交互更加困难。因此,我们决定
反对这个选择。
In C the standard memory handling functions are malloc()
, realloc()
and free()
. However, C++ stdlib allocators only parallel two of them: there is no reallocation function. Of course, it would not be possible to do exactly the same as realloc()
, because simply copying memory is not appropriate for non-aggregate types. But would there be a problem with, say, this function:
bool reallocate (pointer ptr, size_type num_now, size_type num_requested);
where
ptr
is previously allocated with the same allocator fornum_now
objects;num_requested
>=num_now
;
and semantics as follows:
- if allocator can expand given memory block at
ptr
from size fornum_now
objects tonum_requested
objects, it does so (leaving additional memory uninitialized) and returnstrue
; - else it does nothing and returns
false
.
Granted, this is not very simple, but allocators, as I understand, are mostly meant for containers and containers' code is usually complicated already.
Given such a function, std::vector
, say, could grow as follows (pseudocode):
if (allocator.reallocate (buffer, capacity, new_capacity))
capacity = new_capacity; // That's all we need to do
else
... // Do the standard reallocation by using a different buffer,
// copying data and freeing the current one
Allocators that are incapable of changing memory size altogether could just implement such a function by unconditional return false;
.
Are there so few reallocation-capable allocator implementation that it wouldn't worth it to bother? Or are there some problems I overlooked?
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.
这篇关于为什么在C ++分配器中没有重新分配功能?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!