It is generally not a good idea to have dynamically sized arrays in the small memory of an Arduino - it can lead to memory corruption. Better to make a fixed array that is big enough for the largest amount of data you will need.
Thank you for answer. I'm using Arduino Due but my full code will contain about 128 pcs such a dynamic array.
Each of them contain 1 - max 4 elements. I'm not sure how mutch memory need my dynamic arry.
I cannot resize this format:
void (voidsArray[1])(uint8_t);
If I use fixed void (voidsArray[4])(uint8_t); it take 12824=1024 1kb but you are right due has 96Kb
:o
If you cannot use a fixed sized array because it uses too much memory then I don't see how you can work round it by using a dynamically sized array. What is going to happen when you resize it and run out of memory ?
my full code will contain about 128 pcs such a dynamic array.
Are you saying that your code will have 128 different arrays ?
how many different functions do you have really ? why would you store the full function pointer each time ? why not keep an array of functions and only store the index of the callback... if you have 16 functions, you could store the index only on 4 bits for example. So to store 128 arrays of up to 4 pointers would require only 256 bytes
Why not have a base class that's a linked list object and inherent that for each of the functions? Then the functions could link themselves into a list. No more worry about function pointers and all that entails. Also, why do they need to be in a list? What's the big plan here?
BTW,
Your array is not storing "void pointers". It's an array of function pointers. The signature of said functions is that they take a uint8_t argument and return no value. The reason you're doing what you're doing the way you're doing it is still rather unclear.
My projekt a bit complex
I use interrupted MCP23017 io expanders for inputs with Arduino due.
Max 8 ic can use on one wire line it is max 8x16=128 inputs
Other wire (wire1) also able to have 8 ic so I can have max total 256 inputs
(Each channel interrupted! I dont have speed problem!)
Each input has a possibility to register dynamically calback functions from different classes
For ex:
input 1 has only one registred callback function for a Dimmer class
input 2 has two callbacks for two Dimmer classes because one input handle two Dimmers
input 3 has has 6 callbacks for Shutter classes
or mix them with different event handlers like click, doble click, hold, double hold...
and so on...
This looks like working solution for dinamic growing function pointer array:
So you configure your system for a max number of input (128 or 256) and you want to attach different behaviors to each input with a Max of 4 things to trigger per input.
So you’ll use up to 4x256=1024 entries to store the behaviors.
Depending on how many bytes you’ll require 0.5, 1, 2 for the behaviors is left up to you - there are ideas listed previously - but point is that it should easily fit in your 96k
So why do you want to go through the Alloc /realloc game? At the end of the day if you use your system to its full capacity you’ll need the memory anyway so better make sure it’s there from the start. It will also make access code much easier - everything can be indexed in an array of struct for example - and if you use indexing tables for the functions as proposed above you can also store the array of struct in EEPROM easily if that is a need to save configurations