AnsweredAssumed Answered

dynamic allocation question on S08

Question asked by REY Stephane on Jan 23, 2013
Latest reply on Jan 25, 2013 by kef

Hi there,


I'm writting high level layers routines (HIL and services) which are supposed to run on various platforms (SO8, Coldfire, Kinetis, ... whatever in fact)

In a queue management routine, I need a dynamic allocation and my code is using the malloc and free functions.


On Codewarrior 10.2 for S08, the default heap size is 2000 bytes which is above the RAM available on the CPU where I'm testing my code. I don't want to rebuild the library as this becomes not really portable. My code should compile on any compiler at the end (this is the aim of high level layers isn't it ?)


I know that dynamic allocation is not well accepted in embedded, and clearly forbidden by some standards like AUTOSAR in the automotive field.


My questions are :


  1. Why dynamic allocations and its possible memory 'leaks' are different from an 'usual' stack overflow due to recursive functions pushing in the stack. I mean that stack overflows is a design issue and the designer shall ensure this is not happening in its code. So why dynamic allocation would be worst ? I've seen older post on the forum saying that using dynamic allocation especially on 8-bits microcontrollers is 'poor design'. Why ? Again, for me this is like a stack management, isn't it ? Why is it considered as different ?
  2. If malloc is "forbidden or not recommended", this is also the case for any proprietary dynamic allocation routine. So it means more or less "don't use dynamic allocation, ... this is bad (and you may go to hell !). Ok, but then ? How can we manage my memory allocations ? Shall I put all my variables as global and then run out of memory after three functions ? I found that memory allocation is an elegant way to save processor ressources so how can we do without using dynamic allocation ?
  3. Just in case I feel brave and decide to not follow the recommendations to not implemente dynamic allocation in my embedded applications, where could I find an example of small proprietary routine, in order to replace malloc, that could work on any processor, S08 included, without having to rebuild any library.


Any clue appreciated. Thanks