草庐IT

allocation

全部标签

c++ - std::vector 构造函数中的 bad_alloc

std::vector有一个构造函数,其中传递size_typecount的单个参数应该使用count默认构造的元素来调整vector的大小。但以下代码在错误转换后失败并出现bad_alloc异常:#includestructInner{intfoo;charbuf[256];};templatestructOuter{typedefstd::vectorBufContainer;typedeftypenameBufContainer::size_typeBufIndex;BufContainerbufs1;BufContainerbufs2;constBufIndexBUFCOUNT

c++ - std::vector 构造函数中的 bad_alloc

std::vector有一个构造函数,其中传递size_typecount的单个参数应该使用count默认构造的元素来调整vector的大小。但以下代码在错误转换后失败并出现bad_alloc异常:#includestructInner{intfoo;charbuf[256];};templatestructOuter{typedefstd::vectorBufContainer;typedeftypenameBufContainer::size_typeBufIndex;BufContainerbufs1;BufContainerbufs2;constBufIndexBUFCOUNT

c++ - 为什么 C++14 中没有 std::allocate_unique 函数?

为什么shared_ptr有allocate_shared而unique_ptr没有allocate_unique?我想使用我自己的分配器创建一个unique_ptr:我必须自己分配缓冲区然后将它分配给一个unique_ptr吗?这似乎是一个明显的习语。 最佳答案 doIhavetoallocatethebuffermyselfandthenassignittoaunique_ptr?不仅仅是一个缓冲区,一个指向对象的指针。但是对象可能需要被分配器销毁,内存肯定需要被分配器释放,所以你还需要将unique_ptr传递给分配器。它不知

c++ - 为什么 C++14 中没有 std::allocate_unique 函数?

为什么shared_ptr有allocate_shared而unique_ptr没有allocate_unique?我想使用我自己的分配器创建一个unique_ptr:我必须自己分配缓冲区然后将它分配给一个unique_ptr吗?这似乎是一个明显的习语。 最佳答案 doIhavetoallocatethebuffermyselfandthenassignittoaunique_ptr?不仅仅是一个缓冲区,一个指向对象的指针。但是对象可能需要被分配器销毁,内存肯定需要被分配器释放,所以你还需要将unique_ptr传递给分配器。它不知

c++ - 为什么不从 std::allocator 继承

我像这样创建了自己的分配器:templateclassBasicAllocator{public:typedefsize_tsize_type;typedefptrdiff_tdifference_type;typedefT*pointer;typedefconstT*const_pointer;typedefT&reference;typedefconstT&const_reference;typedefTvalue_type;BasicAllocator()throw(){};BasicAllocator(constBasicAllocator&other)throw(){};te

c++ - 为什么不从 std::allocator 继承

我像这样创建了自己的分配器:templateclassBasicAllocator{public:typedefsize_tsize_type;typedefptrdiff_tdifference_type;typedefT*pointer;typedefconstT*const_pointer;typedefT&reference;typedefconstT&const_reference;typedefTvalue_type;BasicAllocator()throw(){};BasicAllocator(constBasicAllocator&other)throw(){};te

c++ - 为什么 malloc(0) 的返回值是实现定义的?

ISO/IEC9899:TC2(即C99标准),§7.20.3规定:Ifthesizeofthespacerequestediszero,thebehaviorisimplementation-defined:eitheranullpointerisreturned,orthebehaviorisasifthesizeweresomenonzerovalue,exceptthatthereturnedpointershallnotbeusedtoaccessanobject.换句话说,malloc(0)可能返回NULL或我不能取消引用的有效指针。这种行为背后的原因是什么?仅仅定义mal

c++ - 为什么 malloc(0) 的返回值是实现定义的?

ISO/IEC9899:TC2(即C99标准),§7.20.3规定:Ifthesizeofthespacerequestediszero,thebehaviorisimplementation-defined:eitheranullpointerisreturned,orthebehaviorisasifthesizeweresomenonzerovalue,exceptthatthereturnedpointershallnotbeusedtoaccessanobject.换句话说,malloc(0)可能返回NULL或我不能取消引用的有效指针。这种行为背后的原因是什么?仅仅定义mal

c++ - C++库应如何允许自定义分配器?

在C语言中,对于库而言,允许用户使用全局函数指针来自定义与malloc()类似的功能以及与free()类似的功能的自定义内存分配很简单。例如,SQLite使用这种方法。C++使事情变得有些复杂,因为分配和初始化通常是融合在一起的。从本质上讲,我们希望获得仅对库重写operatornew和operatordelete的行为,但是实际上无法做到这一点(我很确定,但不是100%)。应该如何在C++中完成?Here是用某种功能new复制Lib::make表达式的某些语义的工具。我不知道这是否有用,但只是为了好玩,here是一个更复杂的版本,它也尝试复制new[]表达式的语义。这是一个面向目标的

c++ - C++库应如何允许自定义分配器?

在C语言中,对于库而言,允许用户使用全局函数指针来自定义与malloc()类似的功能以及与free()类似的功能的自定义内存分配很简单。例如,SQLite使用这种方法。C++使事情变得有些复杂,因为分配和初始化通常是融合在一起的。从本质上讲,我们希望获得仅对库重写operatornew和operatordelete的行为,但是实际上无法做到这一点(我很确定,但不是100%)。应该如何在C++中完成?Here是用某种功能new复制Lib::make表达式的某些语义的工具。我不知道这是否有用,但只是为了好玩,here是一个更复杂的版本,它也尝试复制new[]表达式的语义。这是一个面向目标的