Free () needed after malloc on mcu (even if array / pointer is always used)?

Ok, I'm pretty new to C, so I may not have figured it all out yet, so here I go:

We need to program stm32f0 with c without any hal libraries. For the exercise, we needed to change the usart example code from sequential to interrupt (for the receiving part). Now it was not too difficult. Another purpose is to add a fifo buffer. (for the actual assignment, we needed to use a fixed buffer size of about 100 characters). I would like to implement a dynamic buffer size. A little googling gave me an answer to using a pointer and using malloc to allocate memory for the required characters. So I did this:

volatile char *fifo_char_buffer;
void USART_init(int bufsize){
    fifo_char_buffer = (char *)malloc(bufsize * sizeof *fifo_char_buffer);
    //setting bunch of registers for usart1
}
//functions for writing/reading fifobuffer 

Everything works fine and dandy on the microcontroller. But I began to read a few more about malloc, and I found out that it is more or less necessary to do free after malloc.

Now my question is: since this buffer in its current state will never be cleared immediately and will be used continuously until you literally turn off the device, does this pointer really need to be freed? For my possibly limited knowledge, every time you restart the MCU, it just does all the highlighting again, right?

+4
source share
2 answers

, free, exit , , , .

, ( , ).

( ) malloc, "running".

free .

, , , init ..

+3

, . . malloc , , , .

0

Source: https://habr.com/ru/post/1685830/


All Articles