标准库(STL)容器支持一种形式的nothrow分配? [英] Do Standard Library (STL) Containers support a form of nothrow allocation?

查看:98
本文介绍了标准库(STL)容器支持一种形式的nothrow分配?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

new 运算符(或对于POD,malloc / calloc)支持在分配大块内存时简单而有效的失败形式。

The new operator (or for PODs, malloc/calloc) support a simple and efficient form of failing when allocating large chunks of memory.

假设我们有这个:

const size_t sz = GetPotentiallyLargeBufferSize(); // 1M - 1000M
T* p = new (nothrow) T[sz];
if(!p) {
  return sorry_not_enough_mem_would_you_like_to_try_again;
}
...

:容器,或者我总是必须用 std :: vector 和朋友处理一个(预期的)异常?

Is there any such construct for the std::containers, or will I always have to handle an (expected!!) exception with std::vector and friends?

可能有一种方法来编写一个预分配内存的自定义分配器,然后将这个自定义分配器传递给向量,这样只要向量不要求

Would there maybe be a way to write a custom allocator that preallocates the memory and then pass this custom allocator to the vector, so that as long as the vector does not ask for more memory than you put into the allocator beforehand, it will not fail?

之后,除了正常的保留函数之外,真正需要的是成员函数 bool std :: vector :: reserve(std :: nothrow){...} 。但是,因为这只会有意义,如果分配器被扩展,以允许nothrow分配,它只是不会发生。看起来(nothrow)new对于某些事情是有好处的: - )

Afterthought: What really would be needed were a member function bool std::vector::reserve(std::nothrow) {...} in addition to the normal reserve function. But since that would only make sense if allocators were extended too to allow for nothrow allocation, it just won't happen. Seems (nothrow) new is good for something after all :-)

编辑: em>我甚至问这个:

As to why I'm even asking this:

我在调试时想到这个问题(第一次机会/调试器的第二次机会异常处理):如果我设置了调试器一次抓住任何bad_alloc,因为我正在测试低内存条件,如果它也捕获那些已经很好预期和在代码中处理的bad_alloc异常将是恼人的。这不是/不是一个真正的大问题,但它只是发生在我说布道,例外是为例外情况,我已经期望发生每一个奇怪的调用在代码中是不寻常的。

I thought of this question while debugging (1st chance / 2nd chance exception handling of the debugger): If I've set my debugger to 1st-chance catch any bad_alloc because I'm testing for low-memory conditions, it would be annoying if it also caught those bad_alloc exceptions that are already well-expected and handled in the code. It wasn't/isn't a really big problem but it just occurred to me that the sermon goes that exceptions are for exceptional circumstances, and something I already expect to happen every odd call in the code is not exceptional.

如果 new(nothrow)有合法用途,那么vector-nothrow-reserve也会有。

If new (nothrow) has it's legitimate uses, the a vector-nothrow-reserve also would have.

推荐答案

默认情况下,标准的STL容器类使用 std :: allocator 罩子来做他们的分配,这就是为什么如果没有内存可以抛出 std :: bad_alloc 。有趣的是,分配器的C ++ ISO规范声明任何分配器类型的返回值必须是一个指向一个能够容纳一些元素的内存块的指针,这自动排除你构建一个自定义分配器可能会使用 nothrow 版本的 new 来执行这些类型的静默分配失败。但是,如果没有可用的内存,你可以构建一个自定义的分配器来终止程序,从那时起,当没有剩余内存时,返回的内存是有效的。 : - )

By default, the standard STL container classes use the std::allocator class under the hood to do their allocation, which is why they can throw std::bad_alloc if there's no memory available. Interestingly, the C++ ISO specification on allocators states that the return value of any allocator type must be a pointer to a block of memory capable of holding some number of elements, which automatically precludes you from building a custom allocator that could potentially use the nothrow version of new to have these sorts of silent allocation failures. You could, however, build a custom allocator that terminated the program if no memory was available, since then it's vacuously true that the returned memory is valid when no memory is left. :-)

简而言之,默认情况下,标准容器抛出异常,任何方式你可以尝试使用自定义分配器来自定义异常, t符合规范。

In short, the standard containers throw exceptions by default, and any way you might try to customize them with a custom allocator to prevent exceptions from being thrown won't conform to the spec.

这篇关于标准库(STL)容器支持一种形式的nothrow分配?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆