简体   繁体   English

C ++标准是否允许实现合并分配?

[英]Does the C++ standard allow for an implementation to coalesce allocations?

I was watching Jonathan Blow's video Ideas about a new programming language for games in which he discusses a common pattern in games programming he calls 'joint allocation'. 我正在观看Jonathan Blow 关于游戏新编程语言的视频,其中他讨论了游戏编程中他称之为“联合分配”的常见模式。 The idea is when you have a class with several members that are dynamically allocated arrays (could be std::vector but since they're fixed size, more like the proposed std::dynarray ) you pre-allocate enough memory to store all of the array data and perform only one allocation big enough for all the arrays rather than one for each array. 这个想法是当你有一个类有几个动态分配数组的成员(可能是std::vector但因为它们是固定大小,更像是建议的std::dynarray )你预先分配足够的内存来存储所有的数组数据只执行一个足够大的分配给所有数组而不是每个数组一个。

He proposes direct language support for this pattern which got me to wondering whether the C++ standard allows for implementations to coalesce allocations in this way? 他建议对这种模式提供直接语言支持,让我想知道C ++标准是否允许实现以这种方式合并分配? It strikes me that this would require some heroic effort from a compiler to actually implement as an optimization but I don't see an obvious reason why it couldn't be done in principle. 令我感到震惊的是,这需要编译器的一些英雄努力才能实际实现为优化,但我没有看到原因无法完成的明显原因。 Does anyone know if this would not be permitted under the standard, or even if there are already implementations that do this optimization in practice? 有谁知道在标准下是否允许这样做,或者即使已经有实现在实践中进行这种优化的实现?

Yes, the standard allows coalescing allocations (C++14): 是的,该标准允许合并分配(C ++ 14):

5.3.4 New [expr.new] 5.3.4新[expr.new]

10 An implementation is allowed to omit a call to a replaceable global allocation function (18.6.1.1, 18.6.1.2). 10允许实现省略对可替换全局分配函数的调用(18.6.1.1,18.6.1.2)。 When it does so, the storage is instead provided by the implementation or provided by extending the allocation of another new-expression . 当它这样做时,存储由实现提供,或者通过扩展另一个新表达式的分配来提供。 The implementation may extend the allocation of a new-expression e1 to provide storage for a new-expression e2 if the following would be true were the allocation not extended: 实现可以扩展new-expression e1的分配,以便为新表达式 e2提供存储,如果以下情况属实,则分配未扩展:

  • the evaluation of e1 is sequenced before the evaluation of e2 , and 在评估e2之前,对e1的评估进行了测序
  • e2 is evaluated whenever e1 obtains storage, and 每当e1获得存储时评估e2 ,并且
  • both e1 and e2 invoke the same replaceable global allocation function, and e1e2调用相同的可替换全局分配函数,并且
  • if the allocation function invoked by e1 and e2 is throwing, any exceptions thrown in the evaluation of either e1 or e2 would be first caught in the same handler, and 如果由e1e2调用的分配函数抛出,则在e1e2的求值中抛出的任何异常都将首先在同一个处理程序中捕获,并且
  • the pointer values produced by e1 and e2 are operands to evaluated delete-expressions , and e1e2产生的指针值是评估delete-expressions的操作数,和
  • the evaluation of e2 is sequenced before the evaluation of the delete-expression whose operand is the pointer value produced by e1 . 在评估delete-expression之前对e2的评估进行排序,其操作数是e1产生的指针值。

C++11 did not allow coalescing or omitting such allocations. C ++ 11不允许合并或省略此类分配。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 C ++标准是否保证使用new完成动态分配? - Does the C++ standard guarantee that dynamic allocations are done with new? C++ 是否存在循环列表的标准实现? - Does a standard implementation of a Circular List exist for C++? C ++标准是否为编译器指定了STL实现细节? - Does the C++ standard specify STL implementation details for the compiler? C ++标准在哪里允许指向未定义类型的指针? - Where does the C++ standard allow pointers to undefined types? C++ 标准是否允许复制任意多态数据结构? - Does the C++ standard allow for copying an arbitrary polymorphic data structure? C ++标准是否允许这种浮点行为? - Does the C++ standard allow this floating-point behaviour? C ++标准是否允许使用typedef重命名构造函数? - Does the C++ standard allow using a typedef to rename a constructor? C++ 标准是否允许未初始化的 bool 使程序崩溃? - Does the C++ standard allow for an uninitialized bool to crash a program? 为什么 C++ 标准不允许函数模板偏特化? - Why does the C++ standard not allow function template partial specialization? C ++标准是否允许添加两个整数(基本类型int)来抛出C ++异常? - Does the C++ Standard allow the addition of two integers (fundamental type int) to throw a C++ exception?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM