Let's consider this:
case 0:
my_array[] = {DATA HERE};
exec_func(myarray);
This is not quite valid C++, but let's pretend you get the syntax right
to make it work. The problem is: doing this will not help you save
memory. Where do you think the compiler is going to store {DATA HERE}?
In memory, right. It will be stored as an anonymous array. Then the
initialization of my_array will make a second copy of the data in
memory, this time in a named array.
You can avoid this second copy by naming your arrays of constants, and
passing the correct one to your function:
// At global scope:
const int my_array_0[] = {...};
const int my_array_1[] = {...};
// Within the switch/case:
case 0:
exec_func(my_array_0);
You can even avoid the switch/case altogether by using an array of
pointers, but that is irrelevant to your current problem.
Your idea of initializing only the array you actually need can work if
you manage to do it procedurally, i.e. implementing some recipe with
instructions, rather than copying a set of constant:
case 0: {
int my_array[array_size];
for (int i = 0; i < array_size; i++) {
my_array[i] = some_expression_to_compute_this_array_item;
}
exec_func(myarray);
}
This is not always feasible though.
If you really have to store the constants in the program, and you are
using an AVR-based Arduino (like the Uno, Mega, Micro...), then you can
save RAM by storing the arrays of constants in flash memory, and copying
only the one you need to RAM:
// At global scope:
const int my_array_0[] PROGMEM = {...};
const int my_array_1[] PROGMEM = {...};
// Within the switch/case:
case 0: {
int my_array[array_size]; // array in RAM
memcpy_P(my_array, my_array_0, sizeof my_array);
exec_func(my_array);
}
Check the documentations of PROGMEM and memcpy_P() for
the details.
If you go this route, you may consider modifying the function
exec_func() so that it expects its parameter to be a pointer to flash
instead of a pointer to RAM. Then you will completely avoid the copy in
RAM.