简体   繁体   English

C ++标准库中是否有任何函数或类可以保证不执行动态内存分配?

[英]Are there any functions or classes in C++ standard library which are guaranteed to not perform dynamic memory allocation?

There are contexts in which we want our C++ code to not perform dynamic memory allocation ('on the heap'), specifically in some embedded development use cases. 在某些情况下,我们希望我们的C ++代码不执行动态内存分配('在堆上'),特别是在某些嵌入式开发用例中。

There are standard library classes which can be implemented without dynamic memory allocation: optional, array, tuple, variant to name a few. 有一些标准的库类可以在没有动态内存分配的情况下实现:可选,数组,元组,变体等等。 The same is true for standard library free functions. 标准库免费功能也是如此。

Are there any such classes or functions which are guaranteed by the standard to not allocate memory dynamically? 是否有标准保证不会动态分配内存的类或函数? The only functions I could find with such a guarantee are the placement new() functions. 我能找到这种保证的唯一功能是放置new()函数。

There are very few cases if any where the C++ standard makes any direct guarantee about not using dynamic memory. 如果C ++标准对不使用动态内存有任何直接保证,那么极少数情况下。

On systems where dynamic memory allocation is signal-unsafe, you can be certain that all functions listed as signal-safe are non-allocating. 在动态内存分配信号不安全的系统上,您可以确定列为信号安全的所有功能都是非分配的。 The standard mentions 标准提到

_Exit
abort
forward
initializer_list functions
memcpy
memmove
move
move_if_noexcept
numeric_limits members
quick_exit
signal
type traits
plain lock-free atomic operations

If you can assume conformance to another standard, POSIX, then it lists more functions that are async-signal-safe . 如果您可以假设符合另一个标准POSIX,那么它会列出更多异步信号安全的函数。 Some of these functions listed by POSIX are provided by C++ (and C) standards as well (such as strcat ), and therefore those standard C++ functions will be signal safe on all POSIX systems. POSIX列出的这些函数中的一些也由C ++(和C)标准(例如strcat )提供,因此这些标准C ++函数在所有POSIX系统上都是信号安全的。

There are a few functions in [new.delete.placement], which are non-allocating by definition. [new.delete.placement]中有一些函数,它们根据定义是非分配的。


Another question separate from guarantees is, whether a reasonable implementation of a function or a type would not allocate. 与保证分开的另一个问题是,函数或类型的合理实现是否不会分配。 Many, many things such as std::tuple and std::array (with non-allocating type arguments naturally) fall into this category. 许多很多东西,比如std::tuplestd::array (当然都有非分配类型的参数)属于这一类。

It would be reasonable that functions which are declared noexcept , and do not have any failure path (like setting error code, returning error indicating value, or terminating the process) shouldn't allocate, since allocation may throw. 声明为noexcept且没有任何失败路径(如设置错误代码,返回错误指示值或终止进程)的函数不应该分配是合理的,因为分配可能会抛出。

Conversely, there are functions that in a reasonable implementation do allocate dynamic memory. 相反,有一些功能在合理的实现中确实分配动态内存。 Obviously those that involve allocators, as well as those listed in the SO post that you linked. 显然那些涉及分配器的那些,以及你链接的SO帖子中列出的那些。 One non-obvious one that often bites people writing signal handlers is missing from the list: It is not at all reasonable to expect printf or any of its related functions to not allocate. 列表中遗漏了一个经常咬人写信号处理程序的非显而易见的事情:期望printf或其任何相关函数不分配是完全没有道理的。

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM