A call for common sense:
Assuming you want various arrays of different size depending on what value that is stored in flash, then you cannot use VLA nor dynamic memory.
Lets say that your size variable in flash is called X
. There is a worst-case scenario of the largest allowed value of X
, lets call that MAX_X
. Your code needs to work under this worst-case scenario - it can't be allowed to access an array out of bounds and run off into the woods when the size is MAX_X
.
This means that you need the following solution:
typedef struct
{
int array [MAX_X];
int size_used;
} some_type;
The actual array sized used is obtained by some_type my_type = { .size_used=X; };
It's nonsense to have the array as VLA because your code can't know how large X
is in advance. It must always set aside MAX_X
items. It's completely misguided to think there's some kind of memory being saved by allocating less bytes when the program isn't using them. Your program doesn't know how much memory that it is going to be used.
Now if anyone tells you to use malloc then you know they have likely never even seen a MCU let alone used one...