TC234 Memory Allocation

Tip / Sign in to post questions, reply, level up, and achieve exciting badges. Know more

cross mob
leonng
Level 3
Level 3
First like received First like given 10 questions asked

Hi there,

I'm trying to use the free() function, but it causes __asm__ volatile ("debug" : : : "memory"); .

There will not be error, when I put malloc() and free() into same function, like:

leonng_0-1675674152285.png

However, when it sperate to two functions, it will have error, like:

leonng_1-1675674245556.png

May I know why? Thank you!

Best Regards,

Leon

0 Likes
1 Solution
Di_W
Moderator
Moderator
Moderator
500 solutions authored 1000 replies posted 250 solutions authored

If you access the *ptr after free(), it may lead error.

View solution in original post

0 Likes
2 Replies
Di_W
Moderator
Moderator
Moderator
500 solutions authored 1000 replies posted 250 solutions authored
#include "Ifx_Types.h"
#include "IfxCpu.h"
#include "IfxScuWdt.h"
#include "SPI_CPU.h"

#include "Ifx_Fifo.h"
#include <stdlib.h>

#include "_Utilities/Ifx_Assert.h"
#include "Cpu/Std/IfxCpu.h"
#include "Stm/Std/IfxStm.h"

IFX_ALIGN(4) IfxCpu_syncEvent g_cpuSyncEvent = 0;

uint16 *receivePtr;

void readData_Long(uint16 addr, uint16 length){
    uint16 i;
    receivePtr=(uint16*)malloc(length *sizeof(uint16));

    for(i=0;i<length;i++)
        *(receivePtr+i)=i+1;

}
void get_readData_Long(uint16 data[],uint16 length){

     uint16 i;
     for(i=0;i<length;i++)
     {
         data[i]=*(receivePtr+i);
     }
     free(receivePtr);

}
void core0_main(void)
{
    uint16 data[11];

    IfxCpu_enableInterrupts();
    
    /* !!WATCHDOG0 AND SAFETY WATCHDOG ARE DISABLED HERE!!
     * Enable the watchdogs and service them periodically if it is required
     */
    IfxScuWdt_disableCpuWatchdog(IfxScuWdt_getCpuWatchdogPassword());
    IfxScuWdt_disableSafetyWatchdog(IfxScuWdt_getSafetyWatchdogPassword());
    
    /* Wait for CPU sync event */
    IfxCpu_emitEvent(&g_cpuSyncEvent);
    IfxCpu_waitEvent(&g_cpuSyncEvent, 1);
    


    readData_Long(0,10);
    get_readData_Long(data,10);


    while(1)
    {
    }
}

Please refer to above code, there is no error found.

0 Likes
Di_W
Moderator
Moderator
Moderator
500 solutions authored 1000 replies posted 250 solutions authored

If you access the *ptr after free(), it may lead error.

0 Likes