2025-10-31 6:58 AM
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?
2025-10-31 7:18 AM
@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 ...
2025-10-31 8:49 AM - edited 2025-10-31 8:50 AM
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.
2025-10-31 10:00 AM
@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...