<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: dynamic allocation question on S08 in 8-bit Microcontrollers</title>
    <link>https://community.nxp.com/t5/8-bit-Microcontrollers/dynamic-allocation-question-on-S08/m-p/235266#M19475</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE&gt;
&lt;P&gt;could one way to do be to allocate a local array in function 1 and then transmitt the pointer and the length of the array to function 2 ?&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;&lt;P&gt;What for? You still have the same amount of RAM for everything. Does you MCU have 2k of RAM? OK, then you have 2k for static variables + stack + heap(if using malloc). Bigger stack usage means less space left for static vars and heap. Bigger heap usage means less space for stack and static variables. So what's the point?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 24 Jan 2013 13:02:01 GMT</pubDate>
    <dc:creator>kef</dc:creator>
    <dc:date>2013-01-24T13:02:01Z</dc:date>
    <item>
      <title>dynamic allocation question on S08</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/dynamic-allocation-question-on-S08/m-p/235261#M19470</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi there,&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I'm writting high level layers routines (HIL and services) which are supposed to run on various platforms (SO8, Coldfire, Kinetis, ... whatever in fact)&lt;/P&gt;&lt;P&gt;In a queue management routine, I need a dynamic allocation and my code is using the malloc and free functions.&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;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 ?)&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I know that dynamic allocation is not well accepted in embedded, and clearly forbidden by some standards like AUTOSAR in the automotive field.&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;My questions are :&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;OL&gt;&lt;LI&gt; 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 ?&lt;/LI&gt;&lt;LI&gt;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 ?&lt;/LI&gt;&lt;LI&gt;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.&lt;/LI&gt;&lt;/OL&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Any clue appreciated. Thanks&lt;/P&gt;&lt;P style="min-height: 8pt; padding: 0px;"&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Stephane&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 23 Jan 2013 22:01:29 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/dynamic-allocation-question-on-S08/m-p/235261#M19470</guid>
      <dc:creator>Stephman</dc:creator>
      <dc:date>2013-01-23T22:01:29Z</dc:date>
    </item>
    <item>
      <title>Re: dynamic allocation question on S08</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/dynamic-allocation-question-on-S08/m-p/235262#M19471</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt; &lt;SPAN style="font-family: 'Courier New'; font-size: 10pt;"&gt;If you don’t have free() calls, then it is obvious that you don’t need malloc() at all. If you have free() calls, then what do you know about memory fragmentation? Say you have 1k heap&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; font-size: 10pt;"&gt;. You malloc() 0.3k, then again 0.3k, then free the first 0.3k. Now, when you try to allocate 0.5k – it fails, though you have 0.7k of free space left. So you need to have few times more free space than the biggest piece of &lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; font-size: 10pt;"&gt;RAM&lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; font-size: 10pt;"&gt; you may need to allocate! It doesn't sound good.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Courier New'; font-size: 10pt;"&gt;Malloc() makes sense on desktop OSes. Smart OS may have mechanisms to share dynamic memory among apps. But even using OSes, not very smart developers tend to allocate memory &lt;STRONG&gt;once&lt;/STRONG&gt;. So again, why malloc?&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt; &lt;SPAN style="font-family: 'Courier New'; font-size: 10pt;"&gt;Say you have two objects, which are not allocated simultaneously. What about&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt; &lt;SPAN style="font-family: 'Courier New'; font-size: 10pt;"&gt;union{&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &lt;SPAN style="font-family: 'Courier New'; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp; char data1[50];&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &lt;SPAN style="font-family: 'Courier New'; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp; double data2[70];&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &lt;SPAN style="font-family: 'Courier New'; font-size: 10pt;"&gt;} dynamic_memory1;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Courier New'; font-size: 10pt;"&gt;Just don't access union field, which is not yet virtually allocated.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Courier New'; font-size: 10pt;"&gt;Too simple? Say you have (data1 or data2) and (data3 or data4) allocated simultaneously:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt; &lt;SPAN style="font-family: 'Courier New'; font-size: 10pt;"&gt;struct {&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &lt;SPAN style="font-family: 'Courier New'; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp; union{&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &lt;SPAN style="font-family: 'Courier New'; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; char data1[50];&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &lt;SPAN style="font-family: 'Courier New'; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; double data2[70];&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &lt;SPAN style="font-family: 'Courier New'; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp; } dm1;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &lt;SPAN style="font-family: 'Courier New'; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp; union{&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &lt;SPAN style="font-family: 'Courier New'; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; char data3[50];&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &lt;SPAN style="font-family: 'Courier New'; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; double data4[70];&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &lt;SPAN style="font-family: 'Courier New'; font-size: 10pt;"&gt;&amp;nbsp;&amp;nbsp; } dm2;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &lt;SPAN style="font-family: 'Courier New'; font-size: 10pt;"&gt;} dynamic_memory;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt; &lt;SPAN style="font-family: 'Courier New'; font-size: 10pt;"&gt;Using defines you may simplify field accesses like:&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Courier New'; font-size: 10pt;"&gt;#define data1 dynamic_memory.dm1.data1&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt; &lt;SPAN style="font-family: 'Courier New'; font-size: 10pt;"&gt;You say you need malloc for custom queue size. You don’t. You need to match queue size with the app. App won't work properly with too small queue, but it is OK too have too big queue. &lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; font-size: 10pt;"&gt;If you have free space left, then you can increase size of the queue to make it using all available memory. BTW on s08, queue with constant size should operate much faster (if coded properly). &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&amp;nbsp; &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt; &lt;SPAN style="font-family: 'Courier New'; font-size: 10pt;"&gt;1. &lt;/SPAN&gt;&lt;SPAN style="font-family: 'Courier New'; font-size: 10pt;"&gt;Dynamic memory leaks when developer forgets to free() unused stuff or there's a bug, which makes some free() omitted. But there’s also memory fragmentation, and I agree: using malloc() in embedded is poor design, unless your embedded is using something like Linux or WinCE with more than one app running simultaneously.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Courier New'; font-size: 10pt;"&gt;2. In embedded, where you are boss for all available CPU resources, malloc is not elegant.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt; &lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Courier New'; font-size: 10pt;"&gt;3. You can DIY your own malloc. It is not hard at all, but doesn't make a lot of sense for S08 and other small MCUs.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="font-family: 'Courier New'; font-size: 10pt;"&gt;Using older CodeWarrior, you could change LIBDEF_HEAPSIZE in libdef.h, add alloc.c and heap.c to your project and change linker priority to make these files having higher priority than *.lib file. Linker should ignore copy from library and use what you added to project. Instead of editing libdef.h in CW files, you can make a copy of libdef.h and edit Access Paths to make compiler search your project folder first.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="mce_paste_marker"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 24 Jan 2013 09:14:49 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/dynamic-allocation-question-on-S08/m-p/235262#M19471</guid>
      <dc:creator>kef</dc:creator>
      <dc:date>2013-01-24T09:14:49Z</dc:date>
    </item>
    <item>
      <title>Re: dynamic allocation question on S08</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/dynamic-allocation-question-on-S08/m-p/235263#M19472</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Edward,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Thanks for your long, nice and valuable answer.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;For embedded OS, many of them are using malloc &amp;amp; free like FreeRTOS and very likerly MQX in their queue management. This is why I find surprising that malloc&amp;amp;free&amp;nbsp; are not recommended in embedded designs as it is used by default in such OS which are dedicated for microcontrollers (by the way does it mean we can't use the queue routines of these OS if they have to be implemented in automotive or any other safe requrement system ?)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I don't understand this statement that on desktop OS we can use dynamic allocations but not in embedded. Leaking memory is bad whatever the platform is, right ? desktop PC has just a more powerful processor with larger memory attached but is it a reason to authorize memory leaks just because we have enough memory to keep it stable ? I admit that I may be completely wrong in my approach but this is not clear for me.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I understand the memory leak process but can't find in my applications cases where it could happen as I may be one these 'not very smart developpers' who can use dynamic allocation only once during MCU initialization to create SPI or SCI buffers for instance. I do agree that it could be replaced by hard coded tabs but I always thought it was less elegant, especially at HIL levels.&lt;/P&gt;&lt;P&gt;I had also used the dynamic allocation to create large buffers of different sizes not used at the same time. The RAM available wasn't enabling to code these buffers as global variable so I've used there the dynamic allocation. Of course the blocs were released with free() before allocating the other.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The second main benefit of dynamic allocation was for me the ability to have a unique function taking care about creating queues of customized length. I don't know how to do other than doing what I was doing previously, i.e. hard coding all the memory blocs where the function needs it. &lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Your suggestion of using unions for allocating same memory to differents objects is nice and would have probably saved me for this last example but I don't find a way to code that properly enabling the user at Application level to define the size of the buffer he needs without hardcoding it.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Any advice ?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Many thanks&lt;/P&gt;&lt;P&gt;Stephane&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="mce_paste_marker"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 24 Jan 2013 10:59:41 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/dynamic-allocation-question-on-S08/m-p/235263#M19472</guid>
      <dc:creator>Stephman</dc:creator>
      <dc:date>2013-01-24T10:59:41Z</dc:date>
    </item>
    <item>
      <title>Re: dynamic allocation question on S08</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/dynamic-allocation-question-on-S08/m-p/235264#M19473</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;could one way to do be to allocate a local array in function 1 and then transmitt the pointer and the length of the array to function 2 ? &lt;/P&gt;&lt;P&gt;Data would be then in stac. We would comme back to stack management issues which is I feel worst than malloc management as there is no stack overflow control whereas there is in malloc a 'is there any free space' control.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Stephane&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 24 Jan 2013 11:37:42 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/dynamic-allocation-question-on-S08/m-p/235264#M19473</guid>
      <dc:creator>Stephman</dc:creator>
      <dc:date>2013-01-24T11:37:42Z</dc:date>
    </item>
    <item>
      <title>Re: dynamic allocation question on S08</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/dynamic-allocation-question-on-S08/m-p/235265#M19474</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;SPAN lang="EN" style="color: #575757; font-family: Helvetica; font-size: 8pt;"&gt;Of course malloc works in embedded, but using malloc()&lt;BR /&gt;makes no sense at least for me. In fact dynamic memory (at least on small MCUs&lt;BR /&gt;with no memory mapping and access controllers) is some big static array (heap) plus&lt;BR /&gt;some access routines, which return pointers to that array on request and track&lt;BR /&gt;declared memory usage for each malloc() request. So what are benefits of using&lt;BR /&gt;malloc??? I see none for all my embedded apps, malloc is just an overhead and&lt;BR /&gt;risk of memory fragmentation. &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN lang="EN" style="color: #575757; font-family: Helvetica; font-size: 8pt;"&gt;FreeRTOS? MQX? Well, the most of embedded apps don’t need&lt;BR /&gt;any RTOS. Each app has to follow some structure, very similar to cooperative&lt;BR /&gt;RTOS with main loop for all the tasks. Preemptive RTOS allows more, even wait&lt;BR /&gt;forever loops for event, which may never happen. But since preemptive RTOS has&lt;BR /&gt;overhead requirement for &lt;/SPAN&gt;&lt;SPAN lang="EN" style="color: #575757; font-family: Helvetica; font-size: 8pt;"&gt;RAM&lt;/SPAN&gt;&lt;SPAN lang="EN" style="color: #575757; font-family: Helvetica; font-size: 8pt;"&gt;, I never use it.&lt;BR /&gt;When it is really required, it is more effective to allow interrupt nesting or&lt;BR /&gt;do simple two&amp;nbsp; tasks switching in&lt;BR /&gt;periodic interrupt. Each commercial RTOS has a lot of routines, which save&lt;BR /&gt;developers work at the cost of more CPU cycles, &lt;/SPAN&gt;&lt;SPAN lang="EN" style="color: #575757; font-family: Helvetica; font-size: 8pt;"&gt;RAM&lt;/SPAN&gt;&lt;SPAN lang="EN" style="color: #575757; font-family: Helvetica; font-size: 8pt;"&gt; and code sice overhead.&lt;BR /&gt;The same for malloc. You can use it if you see benefit. &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;UL style="list-style-type: disc;"&gt;&lt;LI&gt;&lt;SPAN lang="EN" style="color: #575757; font-family: Helvetica; font-size: 8pt;"&gt;Leaking memory is bad whatever&lt;BR /&gt;the platform is, right ? &lt;/SPAN&gt;&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN lang="EN" style="color: #575757; font-family: Helvetica; font-size: 8pt;"&gt;Leaking memory is caused by bugs in the code. Mamory&lt;BR /&gt;fragmentation has no vaccines, except allocating always the same size or not&lt;BR /&gt;freeing anything. &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN lang="EN" style="color: #575757; font-family: Helvetica; font-size: 8pt;"&gt;Do you need user control of queue size? Just allocate&lt;BR /&gt;array, big enough for max queue size setting. Make in/out pointers/indexes&lt;BR /&gt;resetting when they cross queue size border. What else you are going to use&lt;BR /&gt;free heap for? What happens when you have max queue size settings? Do other&lt;BR /&gt;thing fit remaining space? If so, then what the purpose of malloc()? Just neat and&lt;BR /&gt;nice code with great malloc() call? Is it OK to have few kB of code space&lt;BR /&gt;occupied with this nice malloc()? In case you need to allow user choose bigger&lt;BR /&gt;buffer and smaller another buffer, or vice versa, then what’s the problem of&lt;BR /&gt;having single array for both buffers and moving top and bottom of each queue up&lt;BR /&gt;and down? &lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN lang="EN" style="color: #575757; font-family: Helvetica; font-size: 8pt;"&gt;It’s up to you to use malloc or not.&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="mce_paste_marker"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 24 Jan 2013 12:57:10 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/dynamic-allocation-question-on-S08/m-p/235265#M19474</guid>
      <dc:creator>kef</dc:creator>
      <dc:date>2013-01-24T12:57:10Z</dc:date>
    </item>
    <item>
      <title>Re: dynamic allocation question on S08</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/dynamic-allocation-question-on-S08/m-p/235266#M19475</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE&gt;
&lt;P&gt;could one way to do be to allocate a local array in function 1 and then transmitt the pointer and the length of the array to function 2 ?&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;&lt;P&gt;What for? You still have the same amount of RAM for everything. Does you MCU have 2k of RAM? OK, then you have 2k for static variables + stack + heap(if using malloc). Bigger stack usage means less space left for static vars and heap. Bigger heap usage means less space for stack and static variables. So what's the point?&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 24 Jan 2013 13:02:01 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/dynamic-allocation-question-on-S08/m-p/235266#M19475</guid>
      <dc:creator>kef</dc:creator>
      <dc:date>2013-01-24T13:02:01Z</dc:date>
    </item>
    <item>
      <title>Re: dynamic allocation question on S08</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/dynamic-allocation-question-on-S08/m-p/235267#M19476</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I definitely agree on the fragmentation problem that I hear for the first time.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;So yes, I'd like to replace malloc with a static array queue management but I don't know how to implement static arrays queues management with different sizes. &lt;/P&gt;&lt;P&gt;Before trying implementing queues with malloc, I had coded a circular buffer routine which works fine. In fact this is this circular buffer routine that I've upgraded to add the malloc because I don't know how to to create several different circular buffers with different sizes, by using this unique circular buffer routine. I don't want to duplicate the circular buffer routine each time I need a buffer. As long as the buffers were with the same size, no problem, I could create many buffers. But how to manage then different sizes ?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Here ishow I've implemented my circular buffer routine before implementing the malloc :&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="c++" __jive_macro_name="code" class="jive_text_macro jive_macro_code _jivemacro_uid_135903578028323" jivemacro_uid="_135903578028323" modifiedtitle="true"&gt;
&lt;P&gt;#define CIRCULAR_BUFFER_SIZE 100&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;/* buffer structure */&lt;/P&gt;
&lt;P&gt;typedef struct{&lt;/P&gt;
&lt;P&gt;&amp;nbsp; U8 u8tab[CIRCULAR_BUFFER_SIZE];&lt;/P&gt;
&lt;P&gt;&amp;nbsp; U8 *u8p_in, *u8p_out;&lt;/P&gt;
&lt;P&gt;&amp;nbsp; U32 u32Count;&lt;/P&gt;
&lt;P&gt;}stBUFFER;&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;/* Circular buffer prototypes */&lt;/P&gt;
&lt;P&gt;void CircularBuffer_Reset(stBUFFER *stBuffer);&lt;/P&gt;
&lt;P&gt;char CircularBuffer_Push(stBUFFER *stBuffer, U8 u8byte);&lt;/P&gt;
&lt;P&gt;U8 CircularBuffer_Pull(stBUFFER *stBuffer);&lt;/P&gt;
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The circular buffer routines are just playing with the pointers. The limitation is that my buffer size is the same for any buffer.... So what would be the method to make it flexible ?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Then to manage several buffer sizes I've added the following function which allocate the memory&lt;/P&gt;&lt;P&gt;the typdef became :&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="c++" __jive_macro_name="code" class="jive_text_macro jive_macro_code _jivemacro_uid_13590364715007862" jivemacro_uid="_13590364715007862"&gt;
&lt;P&gt;typedef struct&lt;/P&gt;
&lt;P&gt;{&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; U8 *u8p_in, *u8p_out;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; volatile U32 u32Count;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; U32 u32Size;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; U8 *u8tab;&lt;/P&gt;
&lt;P&gt;}stBUFFER;&lt;/P&gt;
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;and I've added the allocation routine :&lt;/P&gt;&lt;P&gt;&lt;SPAN class="mce_paste_marker"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;PRE __default_attr="c++" __jive_macro_name="code" class="jive_text_macro jive_macro_code _jivemacro_uid_13590364647804899" jivemacro_uid="_13590364647804899"&gt;
&lt;P&gt;/*--------------------------------------------------------------------------------&lt;/P&gt;
&lt;P&gt;Description&amp;nbsp;&amp;nbsp;&amp;nbsp; : Initialize a circular buffer&lt;/P&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;Call&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; : CircularBuffer_Init (stBUFFER *stBuffer, U32 u32TableSize) &lt;/P&gt;
&lt;P&gt;Input(s)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; : *stBuffer = Pointer to the buffer to initialize&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; u32TableSize = size to allocate&lt;/P&gt;
&lt;P&gt;Output(s)&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; : none&lt;/P&gt;
&lt;P&gt;Return&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; : none&lt;/P&gt;
&lt;P&gt;--------------------------------------------------------------------------------*/&lt;/P&gt;
&lt;P&gt;void CircularBuffer_Init (stBUFFER *stBuffer, U32 u32TableSize) &lt;/P&gt;
&lt;P&gt;{&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; stBuffer-&amp;gt;u8tab = malloc (u32TableSize * sizeof(char));&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; stBuffer-&amp;gt;u32Size = u32TableSize;&lt;/P&gt;
&lt;P&gt;}&lt;/P&gt;
&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;The full code is attached (Buffer rouines renamed in queues routines)&lt;/P&gt;&lt;P&gt;any advice on the method I could use ?&lt;/P&gt;&lt;P&gt;Thanks&lt;/P&gt;&lt;P&gt;Stephane&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="mce_paste_marker"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 24 Jan 2013 14:19:02 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/dynamic-allocation-question-on-S08/m-p/235267#M19476</guid>
      <dc:creator>Stephman</dc:creator>
      <dc:date>2013-01-24T14:19:02Z</dc:date>
    </item>
    <item>
      <title>Re: dynamic allocation question on S08</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/dynamic-allocation-question-on-S08/m-p/235268#M19477</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;In case free() is not required, small malloc() can be implemented this way&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;PRE __default_attr="c++" __jive_macro_name="code" class="jive_text_macro jive_macro_code _jivemacro_uid_13590567280317679" jivemacro_uid="_13590567280317679" modifiedtitle="true"&gt;
&lt;P&gt;#define MYHEAP_SIZE 1000
 
char myheap[MYHEAP_SIZE];
char *myfreeheap = myheap;
 &lt;/P&gt;
&lt;P&gt;#define malloc(size) ( (myfreeheap + (size)) &amp;lt; (myheap + MYHEAP_SIZE) ) ? myfreeheap : NULL; myfreeheap +=(size)&lt;/P&gt;
&lt;SPAN class="mce_paste_marker"&gt;&lt;/SPAN&gt;
&lt;P&gt;&lt;/P&gt;

&lt;/PRE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="mce_paste_marker"&gt;To free all resources just reset myfreeheap to &amp;amp;myheap[0]. &lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 24 Jan 2013 19:42:23 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/dynamic-allocation-question-on-S08/m-p/235268#M19477</guid>
      <dc:creator>kef</dc:creator>
      <dc:date>2013-01-24T19:42:23Z</dc:date>
    </item>
    <item>
      <title>Re: dynamic allocation question on S08</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/dynamic-allocation-question-on-S08/m-p/235269#M19478</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I'm not familiar with c++ so I may not understand the #define line&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="mce_paste_marker"&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;P&gt;but it still call malloc ? what's the benefit of that ?&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN class="mce_paste_marker"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 24 Jan 2013 20:08:05 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/dynamic-allocation-question-on-S08/m-p/235269#M19478</guid>
      <dc:creator>Stephman</dc:creator>
      <dc:date>2013-01-24T20:08:05Z</dc:date>
    </item>
    <item>
      <title>Re: dynamic allocation question on S08</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/dynamic-allocation-question-on-S08/m-p/235270#M19479</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;&lt;BR /&gt;Well, it is not C++, just C. You may convert this define to function, but in case stdlib.h is included, define will silently suppress malloc declaration. Never mind, you decide what to do with it.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;No benefit of malloc, but since you want buffer allocation at runtime, it is still malloc like stuff. Also above snippet answers how one may code it's own malloc. Still, IMO buffers should be allocated statically at design-n-compile time. #defines and enums are fine to specify buffer sizes.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 25 Jan 2013 05:34:52 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/dynamic-allocation-question-on-S08/m-p/235270#M19479</guid>
      <dc:creator>kef</dc:creator>
      <dc:date>2013-01-25T05:34:52Z</dc:date>
    </item>
    <item>
      <title>Re: dynamic allocation question on S08</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/dynamic-allocation-question-on-S08/m-p/235271#M19480</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;wow. I do not understand at all this syntax. But I'm going to investigate.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;By the way, spending hours on the web I've found the way to define multiple queues in static arrays. The method seems to be linked lists. This apparently works, but it sounds not so easy to make it robust.&lt;/P&gt;&lt;P&gt;Not sure about that, but&amp;nbsp; at the end I understand that the only benefit is that memory blocs are contiguous avoiding then framgmentation, but I feel that there is still the problem of memory leak if you don't free queues anyway...&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This could be then the answer for very small RAM sizes but this isn't this method considered as dynamic allocation anyway ??&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Stephane&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 25 Jan 2013 08:19:02 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/dynamic-allocation-question-on-S08/m-p/235271#M19480</guid>
      <dc:creator>Stephman</dc:creator>
      <dc:date>2013-01-25T08:19:02Z</dc:date>
    </item>
    <item>
      <title>Re: dynamic allocation question on S08</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/dynamic-allocation-question-on-S08/m-p/235272#M19481</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Queue either works, or doesn't work and you have some data missing or part of queue memory blocked and not used. Just fix it and you won't see any leaks.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 25 Jan 2013 09:10:22 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/dynamic-allocation-question-on-S08/m-p/235272#M19481</guid>
      <dc:creator>kef</dc:creator>
      <dc:date>2013-01-25T09:10:22Z</dc:date>
    </item>
    <item>
      <title>Re: dynamic allocation question on S08</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/dynamic-allocation-question-on-S08/m-p/235273#M19482</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Well I guess things starts to become clear in my mind and &lt;SPAN class="mce_paste_marker"&gt;&lt;/SPAN&gt;I got the syntax you have written. I had never used the C ternary operator before so I was confused.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;SPAN style="text-decoration: underline;"&gt;&lt;STRONG&gt;So to summarize&lt;/STRONG&gt;&lt;/SPAN&gt; (may help some other people)&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;-&amp;gt; Dynamic allocation using malloc/free functions has the disadvantage of :&lt;/P&gt;&lt;UL&gt;&lt;LI&gt;having fragmented memory which requires larger RAM memory to work,&lt;/LI&gt;&lt;LI&gt;having possible memory leaks because of blocs not properly relreased&lt;/LI&gt;&lt;LI&gt;be forbidden by some standards for safety embedded systems&lt;/LI&gt;&lt;/UL&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;-&amp;gt; If memory allocation is only used once (at MCU initialization for instance) and never released with free(), then one way is to define a static array and implement memory blocs within this array which will never be released. This enables having customized size blocs, no fragmentation, no memory leaks,... One implementation is the last code you have shown, but may need to be improved to make it more robust.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;-&amp;gt; if memory allocation is used on the fly byè the application then one can implement a linked list system within the static array. The main disadvantage is that on low RAM systems, fragmentation may happen very quickly and the system may crash because it's impossible anymore to allocate a large enough memory bloc.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;This can be bypassed by preallocating memory blocks. For instance 10, 50, 100, 200, 500, 1000 bytes....&lt;/P&gt;&lt;P&gt;The user could then call a dedicated function like sendqueue10(...), sendqueue500 (...), and so on.&lt;/P&gt;&lt;P&gt;The functions just need to check that there are still free blocks available to assign. The main disadvantage is that RAM usage is not optimized at all as blocks sizes are not flexible and some RAM may be wasted but it looks like it bypass all the other disadvantages of dynamic allocation.&lt;/P&gt;&lt;P&gt;Or an other solution could be to have defragmentation routine but this would take time to do that.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm going to implement these solutions in my OS and see how it runs and if it fits my requirements.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Many thanks for all you very valuable help Edward. You gave me enough information to let me undertstand more about memory allocations.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Regards&lt;/P&gt;&lt;P&gt;Stephane&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;One way to go could be as well to preallocate static queues of different sizes. Then the user could use the queue of a given length. (for instance, 50, 100, 200, 500, 1000 bytes) This is not optimal for RAM usage but would work.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I'm going to code this static array with linked list to manage my queues.&lt;/P&gt;&lt;P&gt;&lt;SPAN class="mce_paste_marker"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 25 Jan 2013 12:43:54 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/dynamic-allocation-question-on-S08/m-p/235273#M19482</guid>
      <dc:creator>Stephman</dc:creator>
      <dc:date>2013-01-25T12:43:54Z</dc:date>
    </item>
    <item>
      <title>Re: dynamic allocation question on S08</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/dynamic-allocation-question-on-S08/m-p/235274#M19483</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;BLOCKQUOTE&gt;
&lt;P&gt;&lt;/P&gt;
&lt;P&gt;Or an other solution could be to have defragmentation routine but this would take time to do that.&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &lt;SPAN class="mce_paste_marker"&gt;&lt;BR /&gt;&lt;/SPAN&gt;&lt;/P&gt;
&lt;/BLOCKQUOTE&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;IMO it is malloc implementaion dependent, wheter you can or can't defragment dynamic memory. It is quite stright forward for implementations, in which malloc always allocates block at least/highest possible address in the heap. Then you always keep free space for largest alllocated block, you start from the block with least/highest address, allocate new block of the same size, copy block to new location, free and reallocate old block (expecting to remove free block island), copy old block back and free new copy block, etc..&lt;/P&gt;&lt;P&gt;But how would you defragment dynamic memory if new block is allocated in circular or even random direction? You newer know how it is made until you look at source code of malloc.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 25 Jan 2013 13:32:58 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/dynamic-allocation-question-on-S08/m-p/235274#M19483</guid>
      <dc:creator>kef</dc:creator>
      <dc:date>2013-01-25T13:32:58Z</dc:date>
    </item>
  </channel>
</rss>

