cancel
Showing results for 
Search instead for 
Did you mean: 

use malloc, calloc and free memory occurred Hard fault interrupt. why?

Leimen
Associate II

Target: STM32G473RBTX

IDE: STM32CubeIDE v1.4.2

bool read(STRUCT *devInfo)

{

uint8_t readbuf[len];

STRUCT *tmp = (STRUCT *)calloc(1, sizeof( STRUCT ));

if(EEPRomReadNByte(addr, len, readbuf))

{

tmp = (STRUCT *)&readbuf; //implicit convert

memcpy(devInfo, tmp, sizeof( STRUCT ));//make sure

}

free(tmp); //<- HardFault_Handler occurred

}

1.why happened to call free memory will occur Hard fault interrupt?

2.implicit convert some time is work or not, why?

1 REPLY 1

You are trying to free memory that isn't part of the heap.

And the chaining structure contains random junk, which is undefined and unpredictable.​

Tips, Buy me a coffee, or three.. PayPal Venmo
Up vote any posts that you find helpful, it shows what's working..