I've read that it is bad practice to use dynamic memory allocations on a microcontroller and I have a pretty good grasp of why.
I am developing a custom PCB around an ATMega2560 and in my situation, I need to create 24 objects for one kind of sensor and 8 more for another.
With a small number of sensors, you might create objects like this:
Ezo_board PH = Ezo_board(99, "PH"); //create a PH circuit object, who's address is 99 and name is "PH"
Ezo_board EC = Ezo_board(100, "EC"); //create an EC circuit object who's address is 100 and name is "EC"
But since I have so many and I want to loop through them I created a pointer array
Ezo_board *EC[maxEC];
and in setup function I wrote:
for (uint8_t i = 0; i < maxEC; i++)
{
EC[i] = new Ezo_board(i+11) //passing the I2C address which start at 11 and increment
I never delete the Ezo_board objects in the code. Is this a safe way to use dynamic memory allocation or is there a better way to do this while still maintaining ability to loop through the sensors in subsequent code?
Ezo_board boards[] = {{99,"PH"}, {100,"EC"}, };, or if it has ambiguous constructors you'll need ` = {Ezo_board(99, "PH"), Ezo_board(100, "EC"), }`for (auto & sensor : sensors) { ...and you don't have to know the size