Using pointers, malloc in Kinetis Microcontroller

cancel
Showing results for 
Show  only  | Search instead for 
Did you mean: 

Using pointers, malloc in Kinetis Microcontroller

Jump to solution
1,198 Views
annamol
Contributor IV

Hi,

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 K53 in IAR. The code works for sometime and then throws the error "******default_isr entered on vector 3 *****". 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 malloc and then freed memory using free(pointer). 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.?  I am attaching the code i wrote .

void main (void)

{

        int *raw;

        raw = malloc(1000* sizeof(int));

        int i=0;

        while(1)

        {        

          int count=0;

          for(int k=0;k<1000;k++)  {

              *(raw++) = 2*count++;

           }

        printf("\TEST");

        for(int i=1000;i>0;i--)   {

           printf("%d\n",*(raw -i));

        }

       free(raw);

        }

}

Labels (1)
0 Kudos
1 Solution
807 Views
davidsherman
Senior Contributor I

I see a big problem, you've got a while(1) loop, and the first for() loop increments raw.  The second for loop does not decrement the pointer, it only gets an offset from it.  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.

int *raw;

raw = malloc(1000* sizeof(int));

        int i=0;

          if(raw == NULL)

          {

               /*do something if malloc was not successful*/

          }

        while(1)

        {

          int count=0;

          for(int k=0;k<1000;k++)  {

              *(raw++) = 2*count++;

           }

        printf("\TEST");

        for(int i=1000;i>0;i--)   {

           printf("%d\n",*(--raw));

        }

       free(raw);

        }

View solution in original post

0 Kudos
5 Replies
807 Views
annamol
Contributor IV

Hi,

Sorry I got it wrong. Thanks guys

0 Kudos
807 Views
jeremyzhou
NXP Employee
NXP Employee

Hi Annamol,

I think David Sherman had pointed out the root cause of the issue.

To free the allocated memory by using the free function, the parameter must be as same as the return value of the malloc function.

And David Sherman had already clarified it in details and I think it can help you out.
Have a great day,
Ping

-----------------------------------------------------------------------------------------------------------------------
Note: If this post answers your question, please click the Correct Answer button. Thank you!
-----------------------------------------------------------------------------------------------------------------------

808 Views
davidsherman
Senior Contributor I

I see a big problem, you've got a while(1) loop, and the first for() loop increments raw.  The second for loop does not decrement the pointer, it only gets an offset from it.  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.

int *raw;

raw = malloc(1000* sizeof(int));

        int i=0;

          if(raw == NULL)

          {

               /*do something if malloc was not successful*/

          }

        while(1)

        {

          int count=0;

          for(int k=0;k<1000;k++)  {

              *(raw++) = 2*count++;

           }

        printf("\TEST");

        for(int i=1000;i>0;i--)   {

           printf("%d\n",*(--raw));

        }

       free(raw);

        }

0 Kudos
807 Views
annamol
Contributor IV

Hi,

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??

0 Kudos
807 Views
davidsherman
Senior Contributor I

Your malloc is fine, it only runs once in main.  I repeat, your pointer is incrementing (raw++) in the FIRST for() loop, but it never gets decremented in the SECOND for() loop.  When your while() loop runs again, it increments beyond your allocated memory and eventually will overwrite the stack memory which causes the crash.