Skip to main content
Leimen
Associate II
October 14, 2020
Question

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

  • October 14, 2020
  • 1 reply
  • 1121 views

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?

This topic has been closed for replies.

1 reply

Tesla DeLorean
Guru
October 14, 2020

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 (See Profile) Up vote any posts that you find helpful, it shows what's working..