cancel
Showing results for 
Search instead for 
Did you mean: 

Use C++ standard Library in a stm32 firmware

Vergnani_ElEn
Associate II

Hi,

I'm currently developing firmware with an event-based architecture. My colleague and I thought about using the standard library, but this approach is risky because the standard library requires a lot of heap memory.

My question is: when is it possible or recommended to use standard library functions such as std::vector, std::map, and std::queue? And when is it mandatory to avoid using them?

What advice would you give me for implementing a good event-driven architecture?

3 REPLIES 3
Andrew Neil
Super User

@Vergnani_ElEn wrote:

What advice would you give me for implementing a good event-driven architecture?


It is certainly possible to do that without the C++ standard library.

In fact, without C++ at all.

State Machines, callbacks, etc can easily be done in plain ol' C ...

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.
TDK
Super User

In general, dynamic memory allocation is frowned upon in embedded electronics applications. If you are designing safety critical software, the rules will generally disallow this. For example, see MISRA recommendations.

If your software crashing is not a big deal, just an inconvenience, then go for it. It's up to you what guidelines you want to follow in your code. There is no one size fits all rule.

Let the requirements of the application drive the design and implementation choices.

If you feel a post has answered your question, please click "Accept as Solution".
Andrew Neil
Super User

@Vergnani_ElEn wrote:

this approach is risky because the standard library requires a lot of heap memory.


The risk is not so much in the use of heap memory as in the re-use - ie, if you keep allocating & deallocating things.

If you only allocate - never deallocate - then there's not a problem.

If you are careful about de-allocation and garbage collections, then you may be able to avoid the problem.

The trouble with the C++ standard Library is knowing what it's actually doing behind the scenes...

A complex system that works is invariably found to have evolved from a simple system that worked.
A complex system designed from scratch never works and cannot be patched up to make it work.