I know that there are lots of questions on this topic but my question is specifically: What happens if I allocate memory to say a buffer, and then I will never allocate memory in my code again. Do I then need to free that memory?
My application of the code is going to be for embedded design, so a microcontroller. The code would look something like this:
#include "ringbuffer.h"
const int BUFFER_SIZE = 5;
// create instance of ring buffer
ringbuffer buff1;
ringbuffer buff2;
int main(void)
{
// initialize ring buffer (this allocates memory to buffers)
ringbuffer_init(BUFFER_SIZE, &buff1);
ringbuffer_init(BUFFER_SIZE, &buff2);
while(true)
{
// receive data in buffer here
// do stuff with data
// run forever
}
// memory is never freed for the buffers
return 0;
}
The allocate memory to the buffer happens inside the ringbuffer_init()
function, and this function would be permitted to only run once in the start of main, before the loop.
My question is really. If I allocate this memory to the buffer, never free it, and I turn off and on my microcontroller, will it allocate the memory twice so that over time it builds up and crashes?
.c and .h files for the ringbuffer library on GitHub.