<?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>LPCXpresso IDE中的主题 Re: realloc problem?</title>
    <link>https://community.nxp.com/t5/LPCXpresso-IDE/realloc-problem/m-p/561284#M16263</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;STRONG&gt;Content originally posted in LPCWare by ToBeFrank on Sun Jun 16 11:58:06 MST 2013&lt;/STRONG&gt;&lt;BR /&gt;&lt;HR /&gt;&lt;SPAN style="color: #0000ff;"&gt;&lt;STRONG&gt;Quote: Lumos&lt;/STRONG&gt;&lt;BR /&gt;Hello,&lt;BR /&gt;I am using&amp;nbsp; LPCXpresso v5.2.2 [Build 2108].&lt;BR /&gt;I am trying to use realloc function from stdlib.h (Redlib - semihost).&lt;BR /&gt;&lt;BR /&gt;For test I am calling this part of code in the loop:&lt;BR /&gt;&lt;BR /&gt;void* ptr1 = NULL;&lt;BR /&gt;void* ptr2 = NULL;&lt;BR /&gt;ptr1 = realloc(NULL, 300);&lt;BR /&gt;ptr2 = realloc(ptr1, 301);&lt;BR /&gt;//free(ptr1);&lt;BR /&gt;free(ptr2);&lt;BR /&gt;&lt;BR /&gt;When deallocation of ptr1 is commented-out, code crashes very soon.&lt;BR /&gt;But, in case free(ptr1); is commented, code is running without any crash.&lt;BR /&gt;From description of realloc function, old space should be deallocated automatically, but from this test it looks different. Manual deallocation is needed.&lt;/SPAN&gt;&lt;HR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Trying to free ptr1 is incorrect. As you you noticed, realloc deallocates the old space if a new memory location was needed. Why would you think you need to free ptr1 then? Your code crashes when free(ptr1) is uncommented because you're freeing a memory object twice.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Thu, 16 Jun 2016 02:13:20 GMT</pubDate>
    <dc:creator>lpcware</dc:creator>
    <dc:date>2016-06-16T02:13:20Z</dc:date>
    <item>
      <title>realloc problem?</title>
      <link>https://community.nxp.com/t5/LPCXpresso-IDE/realloc-problem/m-p/561281#M16260</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;STRONG&gt;Content originally posted in LPCWare by Lumos on Fri Jun 14 15:11:09 MST 2013&lt;/STRONG&gt;&lt;BR /&gt;&lt;SPAN&gt;Hello,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I am using&amp;nbsp; LPCXpresso v5.2.2 [Build 2108].&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I am trying to use realloc function from stdlib.h (Redlib - semihost).&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;For test I am calling this part of code in the loop:&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;void* ptr1 = NULL;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;void* ptr2 = NULL;&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;ptr1 = realloc(NULL, 300);&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;ptr2 = realloc(ptr1, 301);&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;//free(ptr1);&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;free(ptr2);&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;When deallocation of ptr1 is commented-out, code crashes very soon.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;But, in case free(ptr1); is commented, code is running without any crash.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;From description of realloc function, old space should be deallocated automatically, but from this test it looks different. Manual deallocation is needed. &lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Can someone explain whats going on here?&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;Thank you&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 16 Jun 2016 02:13:18 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPCXpresso-IDE/realloc-problem/m-p/561281#M16260</guid>
      <dc:creator>lpcware</dc:creator>
      <dc:date>2016-06-16T02:13:18Z</dc:date>
    </item>
    <item>
      <title>Re: realloc problem?</title>
      <link>https://community.nxp.com/t5/LPCXpresso-IDE/realloc-problem/m-p/561282#M16261</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;STRONG&gt;Content originally posted in LPCWare by cfb on Fri Jun 14 18:02:12 MST 2013&lt;/STRONG&gt;&lt;BR /&gt;&lt;SPAN&gt;It looks like a misuse of void pointers to me. Try using typed pointers and cast the return result from realloc.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 16 Jun 2016 02:13:18 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPCXpresso-IDE/realloc-problem/m-p/561282#M16261</guid>
      <dc:creator>lpcware</dc:creator>
      <dc:date>2016-06-16T02:13:18Z</dc:date>
    </item>
    <item>
      <title>Re: realloc problem?</title>
      <link>https://community.nxp.com/t5/LPCXpresso-IDE/realloc-problem/m-p/561283#M16262</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;STRONG&gt;Content originally posted in LPCWare by Lumos on Sat Jun 15 02:19:04 MST 2013&lt;/STRONG&gt;&lt;BR /&gt;&lt;HR /&gt;&lt;SPAN style="color: #0000ff;"&gt;&lt;STRONG&gt;Quote: cfb&lt;/STRONG&gt;&lt;BR /&gt;It looks like a misuse of void pointers to me. Try using typed pointers and cast the return result from realloc.&lt;/SPAN&gt;&lt;HR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Typing of pointers does not have any effect.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;But I tried to set Newlib (semihost) library type, without changing of code itself, ant it is working without problems.&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;So this leads me to the conclusion: realloc implementation in the Redlib (semihost) library type is not fully complaint with specification.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Please correct me if I am wrong.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 16 Jun 2016 02:13:19 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPCXpresso-IDE/realloc-problem/m-p/561283#M16262</guid>
      <dc:creator>lpcware</dc:creator>
      <dc:date>2016-06-16T02:13:19Z</dc:date>
    </item>
    <item>
      <title>Re: realloc problem?</title>
      <link>https://community.nxp.com/t5/LPCXpresso-IDE/realloc-problem/m-p/561284#M16263</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;STRONG&gt;Content originally posted in LPCWare by ToBeFrank on Sun Jun 16 11:58:06 MST 2013&lt;/STRONG&gt;&lt;BR /&gt;&lt;HR /&gt;&lt;SPAN style="color: #0000ff;"&gt;&lt;STRONG&gt;Quote: Lumos&lt;/STRONG&gt;&lt;BR /&gt;Hello,&lt;BR /&gt;I am using&amp;nbsp; LPCXpresso v5.2.2 [Build 2108].&lt;BR /&gt;I am trying to use realloc function from stdlib.h (Redlib - semihost).&lt;BR /&gt;&lt;BR /&gt;For test I am calling this part of code in the loop:&lt;BR /&gt;&lt;BR /&gt;void* ptr1 = NULL;&lt;BR /&gt;void* ptr2 = NULL;&lt;BR /&gt;ptr1 = realloc(NULL, 300);&lt;BR /&gt;ptr2 = realloc(ptr1, 301);&lt;BR /&gt;//free(ptr1);&lt;BR /&gt;free(ptr2);&lt;BR /&gt;&lt;BR /&gt;When deallocation of ptr1 is commented-out, code crashes very soon.&lt;BR /&gt;But, in case free(ptr1); is commented, code is running without any crash.&lt;BR /&gt;From description of realloc function, old space should be deallocated automatically, but from this test it looks different. Manual deallocation is needed.&lt;/SPAN&gt;&lt;HR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Trying to free ptr1 is incorrect. As you you noticed, realloc deallocates the old space if a new memory location was needed. Why would you think you need to free ptr1 then? Your code crashes when free(ptr1) is uncommented because you're freeing a memory object twice.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 16 Jun 2016 02:13:20 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPCXpresso-IDE/realloc-problem/m-p/561284#M16263</guid>
      <dc:creator>lpcware</dc:creator>
      <dc:date>2016-06-16T02:13:20Z</dc:date>
    </item>
    <item>
      <title>Re: realloc problem?</title>
      <link>https://community.nxp.com/t5/LPCXpresso-IDE/realloc-problem/m-p/561285#M16264</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;STRONG&gt;Content originally posted in LPCWare by TheFallGuy on Sun Jun 16 14:15:40 MST 2013&lt;/STRONG&gt;&lt;BR /&gt;&lt;SPAN&gt;The definition for realloc states&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;" If the area pointed to was moved, a free(ptr) is done."&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Thus, your free of ptr1 is NEVER correct.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 16 Jun 2016 02:13:20 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPCXpresso-IDE/realloc-problem/m-p/561285#M16264</guid>
      <dc:creator>lpcware</dc:creator>
      <dc:date>2016-06-16T02:13:20Z</dc:date>
    </item>
    <item>
      <title>Re: realloc problem?</title>
      <link>https://community.nxp.com/t5/LPCXpresso-IDE/realloc-problem/m-p/561286#M16265</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;STRONG&gt;Content originally posted in LPCWare by CodeRedSupport on Mon Jun 17 03:15:25 MST 2013&lt;/STRONG&gt;&lt;BR /&gt;&lt;HR /&gt;&lt;SPAN style="color: #0000ff;"&gt;&lt;STRONG&gt;Quote: Lumos&lt;/STRONG&gt;&lt;BR /&gt;Hello,&lt;BR /&gt;I am using&amp;nbsp; LPCXpresso v5.2.2 [Build 2108].&lt;BR /&gt;I am trying to use realloc function from stdlib.h (Redlib - semihost).&lt;BR /&gt;&lt;BR /&gt;For test I am calling this part of code in the loop:&lt;BR /&gt;&lt;BR /&gt;void* ptr1 = NULL;&lt;BR /&gt;void* ptr2 = NULL;&lt;BR /&gt;ptr1 = realloc(NULL, 300);&lt;BR /&gt;ptr2 = realloc(ptr1, 301);&lt;BR /&gt;//free(ptr1);&lt;BR /&gt;free(ptr2);&lt;BR /&gt;&lt;BR /&gt;When deallocation of ptr1 is commented-out, code crashes very soon.&lt;BR /&gt;But, in case free(ptr1); is commented, code is running without any crash.&lt;BR /&gt;From description of realloc function, old space should be deallocated automatically, but from this test it looks different. Manual deallocation is needed. &lt;BR /&gt;&lt;/SPAN&gt;&lt;HR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;It looks like you are correct - Redlib's realloc is not releasing memory correctly. We'll investigate for fixing in a future LPCXpresso release.&lt;/SPAN&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Regards,&lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;CodeRedSupport&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 16 Jun 2016 02:13:21 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPCXpresso-IDE/realloc-problem/m-p/561286#M16265</guid>
      <dc:creator>lpcware</dc:creator>
      <dc:date>2016-06-16T02:13:21Z</dc:date>
    </item>
    <item>
      <title>Re: realloc problem?</title>
      <link>https://community.nxp.com/t5/LPCXpresso-IDE/realloc-problem/m-p/561287#M16266</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;STRONG&gt;Content originally posted in LPCWare by Lumos on Mon Jun 17 12:03:04 MST 2013&lt;/STRONG&gt;&lt;BR /&gt;&lt;HR /&gt;&lt;SPAN style="color: #0000ff;"&gt;&lt;STRONG&gt;Quote: CodeRedSupport&lt;/STRONG&gt;&lt;BR /&gt;It looks like you are correct - Redlib's realloc is not releasing memory correctly. We'll investigate for fixing in a future LPCXpresso release.&lt;/SPAN&gt;&lt;HR /&gt;&lt;BR /&gt;&lt;BR /&gt;&lt;SPAN&gt;Yes, thank you for confirmation. &lt;/SPAN&gt;&lt;BR /&gt;&lt;SPAN&gt;I am going to use own realloc function until fix will be available.&lt;/SPAN&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Thu, 16 Jun 2016 02:13:21 GMT</pubDate>
      <guid>https://community.nxp.com/t5/LPCXpresso-IDE/realloc-problem/m-p/561287#M16266</guid>
      <dc:creator>lpcware</dc:creator>
      <dc:date>2016-06-16T02:13:21Z</dc:date>
    </item>
  </channel>
</rss>

