Following How do I create a module in MISRAC:2012 that follows Dir 4.12 and 4.8? discussion. I fail to see how memory pools are MISRA C Dir 4.12 compliant.
If we read the directive 4.12 it is said that dynamic memory usage shall not be used to avoid undefined behavior as specified in rule 21.3 that enumerates the following failure modes, amongst other, as examples:
- Accessing allocate memory before storing a value into it.
- A pointer to freed memory is used in any way.
Later on, directive 4.12 says that "If a decision is made to use dynamic memory" (and I read here "deviation") "care shall be taken that ensures software behaves in a predictable manner".
In the mentioned previous thread, @Lundin answer completly ignores the undefined behavior risk and just focuses on the deterministic behavior of a memory pool implementation compared to a malloc/free implementation, which it seems to me is the deviation path, no the rule itself. The rule, as it is written, it is said to prevent bad application usage of dynamically allocated/freed memory by the application leading to undefined behavior.
Finally, even if the stack is dynamic, the protocol to access it is totally controlled by the compiler, so I would say it cannot be compared to memory pools or any othe dynamic memory allocation mechanism in terms of reliability.
It looks to me that MISRA C rationale on Dir 4.12 warns about the risk of the dynamic memory protocol itself to lend to undefined behavior when accessing memory. Confusingly enough, then it says that if you decide to use dynamic memory you should be aware of the deterministic behavior of allocations, which is another orthogonal matter.
MISRA C mixes, for example, "accessing to a freed pointer" (the problem said to be prevented) with deterministic behavior of allocation/deallocation (the other problem said to prevent if you use dynamic memory).
My interpretation is that if you want your software to be realiable you should control both. So maybe the most appropiate would be to:
- Evaluate if dynamic memory is really needed
- If so, deviate the rule and stablish controls to prevent both: application access errors and undeterministic allocation/deallocation behavior.