MISRA Discussion Forums
About Rule 18-4-1 "Dynamic heap memory allocation shall not be used." - Printable Version

+- MISRA Discussion Forums (https://forum.misra.org.uk)
+-- Forum: MISRA C++ (https://forum.misra.org.uk/forumdisplay.php?fid=18)
+--- Forum: MISRA C++:2008 rules (https://forum.misra.org.uk/forumdisplay.php?fid=19)
+---- Forum: 6.18 Language support library (C++) (https://forum.misra.org.uk/forumdisplay.php?fid=146)
+---- Thread: About Rule 18-4-1 "Dynamic heap memory allocation shall not be used." (/showthread.php?tid=1482)



About Rule 18-4-1 "Dynamic heap memory allocation shall not be used." - Sebastien.Ashby@nxp - 25-03-2019

Hi,

I have a question about Rule 18-4-1 "Dynamic heap memory allocation shall not be used.".

C++ already has a form of memory allocation for its automatic variables.
That is they are allocated on the stack.
I take for granted that this form of memory allocation is allowed by MISRA.

Would managing and allocating the heap space in a stack fashion address most concerns associated with rule 18-4-1?

For example, the call stack is also subject to out-of-storage runtime failure, i.e. stack overflow.
The call stack being used to store a host of values associated with function calls and its depth may be based on runtime values.
The only way to secure against this being to find the worst case your system allows and set the stack size accordingly.

If the heap space allocator was not subject to:
  • Memory leaks
    Non-deterministic behavior
    • Fragmentation
      Order of allocation/deallocation

And the memory exhaustion case was handled in the same fashion as the call stack.
Meaning testing your system to find the worst case and ensuring that at least that amount is available.

Would this type of dynamic heap memory allocation be compliant with the intent of rule 18-4-1?


Best regards.

Sebastien Ashby


Re: About Rule 18-4-1 "Dynamic heap memory allocation shall not be used." - misra cpp - 10-04-2019

We would agree that creating large amounts of data on the stack has many of the same problems as creating it on the heap.

The rules were written when most target applications had well defined and static memory requirements, making dynamic allocation unnecessary.

However, this approach is being re-examined