I want to initialize local (const) array:
void foo() {
const uint8_t arr[] = {1, 2, 3, 4, 5};
}
And for that initialization I pay 5 bytes of global RAM: those 5 constants are being copied to RAM at the start of the program and stay there forever.
The question is why? Couldn't the compiler just initialize the array directly from the FLASH when I call the function?
I can't use that right-hand temporary array anywhere else anyway, it must be destroyd right after ;
and yet it persists in RAM at all times after the startup.
Now I have to do something like
void foo() {
static const uint8_t init_arr[] PROGMEM = {1, 2, 3, 4, 5};
uint8_t arr[5];
copy_from_prog_function(arr, init_arr, 5);
}
And on top of that, now I can't even have my arr
to be const
!
Is there more elegant solution to this problem?