3

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?

Praetorian
  • 106,671
  • 19
  • 240
  • 328
Xofo
  • 1,256
  • 4
  • 18
  • 33
  • 1
    I do not have access to the document, but are there not other rules relating to the use of the standard library and exceptions that would preclude the use of iostream in any case? You can do anything you deem necessary under MISRA with an appropriate _deviation declaration procedure_; what you should be asking yourself is whether this is an appropriate deviation? Unless there are contractual requirements to conform to MISRA, you need to think for yourself and understand the consequences in your specific application. – Clifford Jan 04 '18 at 09:43
  • Thank you for your comments! – Xofo Jan 04 '18 at 12:20
  • 1
    add `std::vector` to the list – Richard Critten Jan 30 '18 at 18:07
  • I had asked this question because the AUTOSAR specification did not as strict towards C++ as MISRA is. The AUTOSAR specification can be read here - https://www.autosar.org/fileadmin/user_upload/standards/adaptive/17-03/AUTOSAR_RS_CPP14Guidelines.pdf – Xofo Dec 01 '18 at 04:19

1 Answers1

5

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.


Xofo
  • 1,256
  • 4
  • 18
  • 33
user9168101
  • 251
  • 2
  • 3
  • 4
    Please note that MISRA is not exclusively used by safety-critical or realtime-demanding systems. It is nowadays de facto standard for all kinds of embedded systems. While it makes no sense to use heap allocation and standard library features in a microcontroller realtime system, it may be acceptable to use them in some higher-level, PC-like embedded applications. Applications where nobody cares about deterministic behavior or real-time performance, but just want to use MISRA as a way to weed out bugs. – Lundin Jan 08 '18 at 09:42