<?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 Using pointers, malloc in Kinetis Microcontroller in Kinetis Microcontrollers</title>
    <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Using-pointers-malloc-in-Kinetis-Microcontroller/m-p/515795#M32610</link>
    <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;There is an application for which I have to store 7K samples and store them for later processing. Due to RAM constraints we have in K53 MCU, I thought about going ahead with pointers for data storage. As the first step, I wrote a simple code for &lt;STRONG&gt;K53&lt;/STRONG&gt; in &lt;STRONG&gt;IAR&lt;/STRONG&gt;. The code works for sometime and then throws the error "&lt;STRONG&gt;******default_isr entered on vector 3 *****&lt;/STRONG&gt;". But even then it works at times and then again throws this statement. Is there anything wrong in my usage as I can't find out where I am exactly going wrong. I allotted memory using&lt;STRONG&gt; malloc&lt;/STRONG&gt; and then freed memory using &lt;STRONG&gt;free(pointer)&lt;/STRONG&gt;. After using free(), the control goes somewhere and when I pause the debug session, it gives me a pop up saying target cannot be halted, do you want to stop debug session? is there a specific way to use malloc and free?If there is some example project using pointers as a replacement for array, it would be great.?&amp;nbsp; I am attaching the code i wrote .&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;void main (void)&lt;/P&gt;&lt;P&gt;{&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int *raw;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; raw = malloc(1000* sizeof(int));&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int i=0;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; while(1)&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; &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int count=0;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for(int k=0;k&amp;lt;1000;k++)&amp;nbsp; {&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; *(raw++) = 2*count++; &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; } &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; printf("\TEST");&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for(int i=1000;i&amp;gt;0;i--)&amp;nbsp;&amp;nbsp; {&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; printf("%d\n",*(raw -i));&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; free(raw);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
    <pubDate>Tue, 07 Jun 2016 13:34:02 GMT</pubDate>
    <dc:creator>annamol</dc:creator>
    <dc:date>2016-06-07T13:34:02Z</dc:date>
    <item>
      <title>Using pointers, malloc in Kinetis Microcontroller</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Using-pointers-malloc-in-Kinetis-Microcontroller/m-p/515795#M32610</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;There is an application for which I have to store 7K samples and store them for later processing. Due to RAM constraints we have in K53 MCU, I thought about going ahead with pointers for data storage. As the first step, I wrote a simple code for &lt;STRONG&gt;K53&lt;/STRONG&gt; in &lt;STRONG&gt;IAR&lt;/STRONG&gt;. The code works for sometime and then throws the error "&lt;STRONG&gt;******default_isr entered on vector 3 *****&lt;/STRONG&gt;". But even then it works at times and then again throws this statement. Is there anything wrong in my usage as I can't find out where I am exactly going wrong. I allotted memory using&lt;STRONG&gt; malloc&lt;/STRONG&gt; and then freed memory using &lt;STRONG&gt;free(pointer)&lt;/STRONG&gt;. After using free(), the control goes somewhere and when I pause the debug session, it gives me a pop up saying target cannot be halted, do you want to stop debug session? is there a specific way to use malloc and free?If there is some example project using pointers as a replacement for array, it would be great.?&amp;nbsp; I am attaching the code i wrote .&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;void main (void)&lt;/P&gt;&lt;P&gt;{&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int *raw;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; raw = malloc(1000* sizeof(int));&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int i=0;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; while(1)&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; &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int count=0;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for(int k=0;k&amp;lt;1000;k++)&amp;nbsp; {&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; *(raw++) = 2*count++; &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; } &lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; printf("\TEST");&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for(int i=1000;i&amp;gt;0;i--)&amp;nbsp;&amp;nbsp; {&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; printf("%d\n",*(raw -i));&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; free(raw);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;&lt;P&gt;}&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Tue, 07 Jun 2016 13:34:02 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Using-pointers-malloc-in-Kinetis-Microcontroller/m-p/515795#M32610</guid>
      <dc:creator>annamol</dc:creator>
      <dc:date>2016-06-07T13:34:02Z</dc:date>
    </item>
    <item>
      <title>Re: Using pointers, malloc in Kinetis Microcontroller</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Using-pointers-malloc-in-Kinetis-Microcontroller/m-p/515796#M32611</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;I see a big problem, you've got a while(1) loop, and the first for() loop increments raw.&amp;nbsp; The second for loop does not decrement the pointer, it only gets an offset from it.&amp;nbsp; Then, the second time through the while loop, raw is going to continue incrementing in the first for loop and eventually overwrite other things, like the stack.&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;int *raw;&lt;/P&gt;&lt;P&gt;raw = malloc(1000* sizeof(int));&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int i=0;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; if(raw == NULL)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&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; /*do something if malloc was not successful*/&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; while(1)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; {&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int count=0;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for(int k=0;k&amp;lt;1000;k++)&amp;nbsp; {&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; *(raw++) = 2*count++;&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; }&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; printf("\TEST");&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; for(int i=1000;i&amp;gt;0;i--)&amp;nbsp;&amp;nbsp; {&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; printf("%d\n",*(--raw));&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; free(raw);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Wed, 08 Jun 2016 14:50:54 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Using-pointers-malloc-in-Kinetis-Microcontroller/m-p/515796#M32611</guid>
      <dc:creator>davidsherman</dc:creator>
      <dc:date>2016-06-08T14:50:54Z</dc:date>
    </item>
    <item>
      <title>Re: Using pointers, malloc in Kinetis Microcontroller</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Using-pointers-malloc-in-Kinetis-Microcontroller/m-p/515797#M32612</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;I am sorry. Are you saying that my raw++ can overwrite default register allocations and that is causing default_isr and giving me the popup?? I had tried without while(1) loop, this was part of testing to see if code is proper or not. Excluding while(1), is the usage proper? It works fine in the first iteration. The second time it hits malloc(), it throws default)isr(). Is the usage wrong?? &lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Jun 2016 06:35:05 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Using-pointers-malloc-in-Kinetis-Microcontroller/m-p/515797#M32612</guid>
      <dc:creator>annamol</dc:creator>
      <dc:date>2016-06-10T06:35:05Z</dc:date>
    </item>
    <item>
      <title>Re: Using pointers, malloc in Kinetis Microcontroller</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Using-pointers-malloc-in-Kinetis-Microcontroller/m-p/515798#M32613</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Your malloc is fine, it only runs once in main.&amp;nbsp; I repeat, your pointer is incrementing (raw++) in the FIRST for() loop, but it never gets decremented in the SECOND for() loop.&amp;nbsp; When your while() loop runs again, it increments beyond your allocated memory and eventually will overwrite the stack memory which causes the crash.&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Fri, 10 Jun 2016 17:18:42 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Using-pointers-malloc-in-Kinetis-Microcontroller/m-p/515798#M32613</guid>
      <dc:creator>davidsherman</dc:creator>
      <dc:date>2016-06-10T17:18:42Z</dc:date>
    </item>
    <item>
      <title>Re: Using pointers, malloc in Kinetis Microcontroller</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Using-pointers-malloc-in-Kinetis-Microcontroller/m-p/515799#M32614</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi Annamol,&lt;/P&gt;&lt;P&gt;I think David Sherman had pointed out the root cause of the issue.&lt;/P&gt;&lt;P&gt;To free the allocated memory by using the free function, the parameter must be as same as the return value of the malloc function.&lt;/P&gt;&lt;P&gt;And David Sherman had already clarified it in details and I think it can help you out.&lt;BR /&gt;Have a great day,&lt;BR /&gt;Ping&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;-----------------------------------------------------------------------------------------------------------------------&lt;BR /&gt;Note: If this post answers your question, please click the Correct Answer button. Thank you!&lt;BR /&gt;-----------------------------------------------------------------------------------------------------------------------&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Sun, 12 Jun 2016 03:16:23 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Using-pointers-malloc-in-Kinetis-Microcontroller/m-p/515799#M32614</guid>
      <dc:creator>jeremyzhou</dc:creator>
      <dc:date>2016-06-12T03:16:23Z</dc:date>
    </item>
    <item>
      <title>Re: Using pointers, malloc in Kinetis Microcontroller</title>
      <link>https://community.nxp.com/t5/Kinetis-Microcontrollers/Using-pointers-malloc-in-Kinetis-Microcontroller/m-p/515800#M32615</link>
      <description>&lt;HTML&gt;&lt;HEAD&gt;&lt;/HEAD&gt;&lt;BODY&gt;&lt;P&gt;Hi,&lt;/P&gt;&lt;P&gt;&lt;/P&gt;&lt;P&gt;Sorry I got it wrong. Thanks guys&lt;/P&gt;&lt;/BODY&gt;&lt;/HTML&gt;</description>
      <pubDate>Mon, 13 Jun 2016 10:50:06 GMT</pubDate>
      <guid>https://community.nxp.com/t5/Kinetis-Microcontrollers/Using-pointers-malloc-in-Kinetis-Microcontroller/m-p/515800#M32615</guid>
      <dc:creator>annamol</dc:creator>
      <dc:date>2016-06-13T10:50:06Z</dc:date>
    </item>
  </channel>
</rss>

