I'm writing a simple bare-metal application on an embedded processor. As part of this application it must allocate some memory on the heap of approx 256kB, using malloc. Note: Originally this was statically allocated in main but at a certain size limit we had to move to dynamic allocation (gcc sliently failed).
The issue is - when the program runs the first time everything works. However if the processor gets reset during execution the pc returns to the start but the program memory still has the heap as it was at the previous execution. So the next time the program runs malloc fails (as malloc's allocation table from the previous run still exists and it thinks the available heap space is already allocated).
So the question is - how do I ensure malloc's table is cleared at the start of main every time I run? Is there a way I can use the location of the heap to zero out some memory (to blank the table) or alternatively get access to the list of pointers malloc has previously returned so I could free them?
The heap should be setup in the pre main init code. In a bare metal system there is no system call to allocate heap memory to your program. You might try getting the debugger to start at the first loaded instruction instead of main and see where it sets up the heap. It sounds like your system requires the program be reloaded on a reset to initialize the heap. What system are you using?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With