MISRA C ++(规则18-4-1)和动态内存分配-是否允许std :: string? [英] MISRA C++ (rule 18-4-1) and Dynamic Memory Allocation - Is std::string permitted?

查看:132
本文介绍了MISRA C ++(规则18-4-1)和动态内存分配-是否允许std :: string?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

MISRA C ++规则18-4-1说:

不得使用动态堆内存分配.

请参阅: http://dist.sonarsource.com/reports/coverage/misra_c++_2008.html

根据此规则,MISRA C ++规则允许使用 std :: string ,因为随着字符串变大, std :: string 确实会分配内存.还有,像 std :: stringstream 这样的类是什么?

解决方案

否,不允许使用托管的 std :: string std :: stringstream .而且,如果您是在嵌入式系统上编程的,则极不可能使用这些结构.嵌入式系统很少需要完整的标准库(尤其是像 std :: stringstream 那样庞大而缓慢的库)及其附带的所有内容,包括RTTI,异常,动态内存分配等.

如果您正在使用某种类型的STL,那么它要么是手写的,要么专门针对嵌入式系统.而且很有可能,除非您正在编写操作系统内核,否则他们将使用静态内存分配器或其他某种策略而不是动态分配.

因此,如果您在嵌入式系统中询问我可以使用std :: stringstream",则说明您已经是一个糟糕的前提,不应为安全性至关重要的设备编写软件.


MISRA C++ rule 18-4-1 says:

Dynamic heap memory allocation shall not be used.

See: http://dist.sonarsource.com/reports/coverage/misra_c++_2008.html

In light of this rule, is std::string permitted under MISRA C++ rules, because std::string does allocate memory as a string gets bigger. Also, what of a class like std::stringstream?

解决方案

No, the hosted std::string and std::stringstream would not be allowed. And if you are programmed on an embedded system, it's highly unlikely you would be using those constructs. Embedded systems rarely have need for the full standard library (especially something as bulky and slow as std::stringstream) and everything that comes with it, including RTTI, exceptions, dynamic memory allocation, etc.

If you are using some type of STL, it's either going to be hand-written or targeted specifically towards embedded systems. And more likely than not, they'll use static memory allocators or some other strategy rather than dynamic allocation, unless you are writing an operating system kernel.

So, if you ask "Can I use std::stringstream" in an embedded system, you are already coming from a bad premise and should not be writing software for safety-critical devices.


这篇关于MISRA C ++(规则18-4-1)和动态内存分配-是否允许std :: string?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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