是否保证 std::vector 每个分配的具有分配功能的内存也将通过单个解除分配调用立即解除分配?

Is it guaranteed that std::vector each allocated memory with allocate function, will also deallocate at once, with single deallocate call?

我正在为 std::vector 编写自定义分配器,想知道 std::vector 如何使用指定的分配器?可以为 5 个对象分配内存,然后部分释放内存,例如首先 2 个元素,然后 3 个元素...(我看不出有任何理由不这样做)。

标准中有没有提到它?我找不到任何东西。

Is it guaranteed that std::vector each allocated memory with allocate function, will also deallocate at once, with single deallocate call?

是的。 std::allocator::deallocate的原型是

void deallocate( pointer p, size_type n );

May it allocate memory for 5 objects, and then deallocate memory partially, for example at first 2 element, then 3 elements... (I don't see any reason not to do so).

但是 n 不能与之前传递给匹配的 allocate 函数的不同,因为 explained in the spec.:

Deallocates the storage referenced by the pointer p, which must be a pointer obtained by an earlier call to allocate(). The argument n must be equal to the first argument of the call to allocate() that originally produced p.

基本上,deallocate 的参数是

  1. allocate
  2. 返回的任何内容
  3. 获取对应的n