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);
}
}
Solved! Go to Solution.
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);
}
Hi,
Sorry I got it wrong. Thanks guys
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!
-----------------------------------------------------------------------------------------------------------------------
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);
}
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??
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.