jjzjj

aligned_allocator

全部标签

c++ - 标准中哪里说 `new` 返回的每个分配都与 `std::max_align_t` 对齐?

Inthisvideo,在大约6.39处,演示者似乎在说new总是返回与std::max_align_t对齐的内存,这是有道理的,因为operatornew对分配的变量类型一无所知。也就是说,编译器必须选择最严格的对齐方式。但我在标准中找不到这个。演示者还说,当new用于分配char或unsignedchar数组时,此规则不适用。在这种情况下,对齐取决于大小。但这对我来说也不清楚。 最佳答案 这是在[basic.stc.dynamic.allocation]/2中:Theallocationfunctionattemptstoall

c++ - 如何使我的 uninitialised_allocator 安全?

来自thisquestion,我想用unitialised_allocator比如说,std::vector避免在构建时对元素进行默认初始化(或resize()的std::vector(有关用例,另请参阅here)。我当前的设计如下所示://basedonadesignbyJaredHoberocktemplatestructuninitialised_allocator:base_allocator::templaterebind::other{//addedbyWalterQ:ISTHISTHECORRECTCONDITION?static_assert(std::is_trivi

c++ - 如何防止在 boost::fast_pool_allocator 管理的对象上调用析构函数?

我想利用boost::fast_pool_allocator的以下广告功能(参见theBoostdocumentationforBoostPool):Forexample,youcouldhaveasituationwhereyouwanttoallocateabunchofsmallobjectsatonepoint,andthenreachapointinyourprogramwherenoneofthemareneededanymore.Usingpoolinterfaces,youcanchoosetoruntheirdestructorsorjustdropthemoffin

c++ - C++ 中 SSE 的内存对齐,_aligned_malloc 等效?

我想知道如何将此C代码转换为C++以实现内存对齐。float*pResult=(float*)_aligned_malloc(length*sizeof(float),16);我看过here然后我试了这个float*pResult=(float*)__attribute__((aligned(16)));还有这个float*pResult=__attribute__((aligned(16)));但两者都给出了类似的错误。error:expectedprimary-expressionbefore'__attribute__'|error:expected','or';'before'

c++ - 为什么 std::allocator 要求 propagate_on_container_move_assignment 为真?

根据当前标准(20.7.9),std::allocator有一个成员propagate_on_container_move_assignment设置为true_type:templateclassallocator{public:typedefsize_tsize_type;typedefptrdiff_tdifference_type;typedefT*pointer;typedefconstT*const_pointer;typedefT&reference;typedefconstT&const_reference;typedefTvalue_type;templatestruc

c++ - 程序以 std::bad_alloc 终止

我正在运行一个C++程序,它在任意点死于std::bad_alloc,这取决于指定的输入。以下是关于该计划的一些观察/要点:对于较短的运行(运行时间取决于输入),程序会正常完成。问题只出现在较大的运行中。该程序没有任何可检测到的内存泄漏。这已通过Valgrind/Memcheck进行了较小的运行检查。此外,我的整个代码没有任何指针(所有动态分配都是由库完成的,例如,在std::vector和std::string中;它是这些库类中的分配失败),因此内存泄漏的可能性极小。几个对象在循环中分配,然后移动到容器中。这些对象中有几个是为了在程序快结束之前一直存在。我怀疑堆碎片可能是个问题(请参

c++ - 为什么在抛出 'std::bad_alloc' 实例后调用终止?

每1秒,函数工作一次。我的系统是linux。奔跑突然死亡。-----global-------staticintarrayNum[33000];-------------------function(){unsignedshortint**US_INT;US_INT=newunsignedshortint*[255];for(inti=0;i程序停止。和留言↓在抛出“std::bad_alloc”的实例后终止调用what():std::bad_alloc 最佳答案 bad_alloc异常是由内存分配失败触发的(因此您的new之一)。

c++ - std::allocator 是否处理 C++17 中的过度对齐类型?

C++17引入了std::aligned_alloc和对齐感知new可以进行过度对齐分配,但是std::allocator?它是否处理过度对齐的类型? 最佳答案 在N4659(C++17DIS)中,23.10.9.1[allocator.members],bullet2T*allocate(size_tn);Returns:Apointertotheinitialelementofanarrayofstorageofsizen*sizeof(T),alignedappropriatelyforobjectsoftypeT.与C++1

c++ - boost::container::allocator_traits::is_partially_propagable 是什么意思?

我很想理解boost::container::allocator_traits当我遇到boost::container::allocator_traits::is_partially_propagable时。我在网上找不到任何其他关于它的文档,我可以理解boost::container::allocator_traits除了is_partially_propagable和storage_is_unpropagable之外的所有其他成员。编辑:以及,它们是如何实现的以及在编写容器时如何使用它们? 最佳答案 它(is_partially

c++ - 抛出 "bad_alloc"的最常见原因是什么?

是否有更频繁的内存相关错误抛出bad_alloc?我知道这意味着内存分配失败,但在代码中导致这种情况的最常见错误是什么? 最佳答案 编辑:其他评论者指出了一些有趣的场景。为了完整起见,我将它们添加到我的回复中。情况一:内存不足我的理解是,只要运算符new和new[]无法为对象或变量分配内存,就会抛出bad_alloc。如果您新建了一堆对象并忘记在它们超出范围之前删除它们(即,您的代码疯狂地泄漏),就会发生这种情况。案例2:一举分配大量内存分配大块内存(如1000x1000x1000double矩阵的情况)可能会失败,因为它需要一个该