In my project I need three arrays to store data inside.
I define a maximal number of elements as a macro variable. However, at a certain point in the setup function, the program could determine, that the arrays would actually store a smaller number of elements. In other words, it is possible (and even likely) that there is more memory reserved for the arrays than actually necessary.
As I think that memory might be a critical factor on a micro controller like the Arduino, I would like to set the excess memory free.
Is there a way to dynamically resize and shrink the arrays at runtime?
Something like this (pseudocode)
#define maxSize 50
int theArray[maxSize];
int actualSize = getActualArraySize();
// sets actualSize to a value <= 50, let's assume actualSize = 20
shrinkArray(theArray, actualSize);
// shrinkArray doesn't exist :) I need something like that.
theArray
as a pointer instead of array:int *theArray;
and then later allocate the required amount of memory:theArray = malloc(requredArrayLength * sizeof(int));
- Would that solve your problem?