<?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: Realloc problems in 8-bit Microcontrollers</title>
    <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Realloc-problems/m-p/187615#M14191</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I did check the ANSI C standard and I found that&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;"&lt;/P&gt;&lt;H5&gt;Notes&lt;/H5&gt;&lt;P&gt;If &lt;STRONG&gt;ptr&lt;/STRONG&gt; does not match a pointer earlier returned by &lt;STRONG&gt;calloc&lt;/STRONG&gt;, &lt;STRONG&gt;malloc&lt;/STRONG&gt;, or &lt;STRONG&gt;realloc&lt;/STRONG&gt;, or if the space has been deallocated by a call to &lt;STRONG&gt;free&lt;/STRONG&gt; or &lt;STRONG&gt;realloc&lt;/STRONG&gt;, the behaviour is undefined"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;May be you should not free the memory block first.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;CrasyCat&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 04 Jun 2009 16:16:35 GMT</pubDate>
    <dc:creator>CrasyCat</dc:creator>
    <dc:date>2009-06-04T16:16:35Z</dc:date>
    <item>
      <title>Realloc problems</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Realloc-problems/m-p/187614#M14190</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Dear All,&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I am developing with a flexis QE64 with Codewarrior v5.9.0 build 2404 and I experienced some problems with realloc function.&lt;/P&gt;&lt;P&gt;I am using the pre defined function in ALLOC.C (path \CodeWarrior for Microcontrollers V6.1\lib\hc08c\src) and there are some conditions where the reallocation fails.&lt;/P&gt;&lt;P&gt;Either using mutilink P&amp;amp;E or full chip simulation, the following code fails:&lt;/P&gt;&lt;P&gt;In the following piece of code the block where I request a reallocation is 15 bytes and when I call the function&amp;nbsp;realloc((void*) ptr13, 22); the new block is only 5 bytes.&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;void main(void) {&lt;/P&gt;&lt;P&gt;unsigned char *ptr1, *ptr2, *ptr3, *ptr4, *ptr5, *ptr6, *ptr7, *ptr8, *ptr9, *ptr10, *ptr11, *ptr12, *ptr13, *ptr14;&lt;/P&gt;&lt;P&gt;unsigned char i;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;EnableInterrupts; /* enable interrupts */&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;/* include your code here */&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;// Build an Heap&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;ptr1 = malloc(4 * 5 - 5);&lt;/P&gt;&lt;P&gt;ptr2 = malloc(7 * 5 - 5);&lt;/P&gt;&lt;P&gt;ptr3 = malloc(4 * 5 - 5);&lt;/P&gt;&lt;P&gt;ptr4 = malloc(4 * 5 - 5);&lt;/P&gt;&lt;P&gt;ptr5 = malloc(3 * 5 - 5);&lt;/P&gt;&lt;P&gt;ptr6 = malloc(4 * 5 - 5);&lt;/P&gt;&lt;P&gt;ptr7 = malloc(5 * 5 - 5);&lt;/P&gt;&lt;P&gt;ptr8 = malloc(7 * 5 - 5);&lt;/P&gt;&lt;P&gt;ptr9 = malloc(7 * 5 - 5);&lt;/P&gt;&lt;P&gt;ptr10 = malloc(3 * 5 - 5);&lt;/P&gt;&lt;P&gt;ptr11 = malloc(9 * 5 - 5);&lt;/P&gt;&lt;P&gt;ptr12 = malloc(14 * 5 - 5);&lt;/P&gt;&lt;P&gt;ptr13 = malloc(5 * 5 - 5);&lt;/P&gt;&lt;P&gt;ptr14 = malloc(7 * 5 - 5);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;free(ptr4);&lt;/P&gt;&lt;P&gt;ptr4 = malloc(3 * 5 - 5);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;free(ptr13);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;ptr13 = malloc(4 * 5 - 5);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;free(ptr12);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;// fill the space&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;for(i = 0; i &amp;lt; 15; i++){&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; &lt;SPAN class="Apple-tab-span"&gt;&lt;/SPAN&gt;*(ptr13 + i) = i + 1;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;}&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;realloc((void*) ptr13, 22);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;for(;&lt;A href="http://freescale.i.lithium.com/i/smilies/16x16_smiley-wink.gif"&gt;&lt;IMG alt=":smileywink:" class="emoticon emoticon-smileywink" src="http://freescale.i.lithium.com/i/smilies/16x16_smiley-wink.gif" title="Smiley Wink" /&gt;&lt;/A&gt; {&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp; &amp;nbsp;__RESET_WATCHDOG(); /* feeds the dog */&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;} /* loop forever */&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Can someone help me?&lt;/P&gt;&lt;P&gt;Thanks in advance.&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 04 Jun 2009 14:38:08 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Realloc-problems/m-p/187614#M14190</guid>
      <dc:creator>mimais</dc:creator>
      <dc:date>2009-06-04T14:38:08Z</dc:date>
    </item>
    <item>
      <title>Re: Realloc problems</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Realloc-problems/m-p/187615#M14191</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hello&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I did check the ANSI C standard and I found that&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;"&lt;/P&gt;&lt;H5&gt;Notes&lt;/H5&gt;&lt;P&gt;If &lt;STRONG&gt;ptr&lt;/STRONG&gt; does not match a pointer earlier returned by &lt;STRONG&gt;calloc&lt;/STRONG&gt;, &lt;STRONG&gt;malloc&lt;/STRONG&gt;, or &lt;STRONG&gt;realloc&lt;/STRONG&gt;, or if the space has been deallocated by a call to &lt;STRONG&gt;free&lt;/STRONG&gt; or &lt;STRONG&gt;realloc&lt;/STRONG&gt;, the behaviour is undefined"&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;May be you should not free the memory block first.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;CrasyCat&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 04 Jun 2009 16:16:35 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Realloc-problems/m-p/187615#M14191</guid>
      <dc:creator>CrasyCat</dc:creator>
      <dc:date>2009-06-04T16:16:35Z</dc:date>
    </item>
    <item>
      <title>Re: Realloc problems</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Realloc-problems/m-p/187616#M14192</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;Does realloc return NULL or 5 or something else?&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 04 Jun 2009 16:44:08 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Realloc-problems/m-p/187616#M14192</guid>
      <dc:creator>Lundin</dc:creator>
      <dc:date>2009-06-04T16:44:08Z</dc:date>
    </item>
    <item>
      <title>Re: Realloc problems</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Realloc-problems/m-p/187617#M14193</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;Huh? That isn't the case here. That ptr13 was previously freed doesn't matter, malloc() is called for it after that:&lt;BR /&gt;&lt;BR /&gt;ptr13 = malloc(4 * 5 - 5);&lt;BR /&gt;&lt;BR /&gt;...&lt;BR /&gt;&lt;BR /&gt;realloc((void*) ptr13, 22);&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;BR /&gt;Btw ANSI C has been obsolete since 1990, the standard is ISO C.&lt;DIV class="message-edit-history"&gt;&lt;SPAN class="edit-author"&gt;Message Edited by Lundin on&lt;/SPAN&gt; &lt;SPAN class="local-date"&gt;2009-06-04&lt;/SPAN&gt; &lt;SPAN class="local-time"&gt;11:52 AM&lt;/SPAN&gt;&lt;/DIV&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 04 Jun 2009 16:51:25 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Realloc-problems/m-p/187617#M14193</guid>
      <dc:creator>Lundin</dc:creator>
      <dc:date>2009-06-04T16:51:25Z</dc:date>
    </item>
    <item>
      <title>Re: Realloc problems</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Realloc-problems/m-p/187618#M14194</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;For Lundin:&lt;/P&gt;&lt;P&gt;The realloc function returns the pointer of the new re-allocated block. From the return point of view the reallocation succeeded but the size of the reallocated block is only 5 and not 22 (or 25) as requested.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;For CrasyCat:&lt;/P&gt;&lt;P&gt;The realloc has been called with a valid block pointer, the area has not been freed!&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Thanks.&amp;nbsp;&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 04 Jun 2009 20:20:34 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Realloc-problems/m-p/187618#M14194</guid>
      <dc:creator>mimais</dc:creator>
      <dc:date>2009-06-04T20:20:34Z</dc:date>
    </item>
    <item>
      <title>Re: Realloc problems</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Realloc-problems/m-p/187619#M14195</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;If it returns a pointer and you get something else than 1 2 3... 15 followed by 7 rubbish values, then something fishy is indeed going on. However, how do you know only 5 bytes were allocated? With the sample code you posted only, it is impossible to tell how much of the data that is valid.&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 04 Jun 2009 20:47:08 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Realloc-problems/m-p/187619#M14195</guid>
      <dc:creator>Lundin</dc:creator>
      <dc:date>2009-06-04T20:47:08Z</dc:date>
    </item>
    <item>
      <title>Re: Realloc problems</title>
      <link>https://community.nxp.com/t5/8-bit-Microcontrollers/Realloc-problems/m-p/187620#M14196</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Using the full chip simulation tool in Codewarrior I saw, at the end of reallocation, that at the returned address (the realloc returned address) there was only 1 2 3 4 5 and then another heap header block started.&lt;/P&gt;&lt;P&gt;Attached you can find two pictures with the heap status before and after the realloc call.&lt;/P&gt;&lt;P&gt;Note that ptr13 = 0x026E (the block where the realloc has been called) and dest = 0x0228 (the returned block).&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;If you want I can give to you the all project.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 04 Jun 2009 21:21:36 GMT</pubDate>
      <guid>https://community.nxp.com/t5/8-bit-Microcontrollers/Realloc-problems/m-p/187620#M14196</guid>
      <dc:creator>mimais</dc:creator>
      <dc:date>2009-06-04T21:21:36Z</dc:date>
    </item>
  </channel>
</rss>

