Skip to main content

Timeline for Conditional assignment of array

Current License: CC BY-SA 4.0

13 events
when toggle format what by license comment
Oct 30, 2022 at 21:19 vote accept user1584421
Oct 29, 2022 at 9:11 comment added Edgar Bonet @user1584421: I amended the answer to cope with arrays of different sizes. Note that there are now multiple variables that share the name my_array, but they are different variables, each local to a {} block in the switch/case.
Oct 29, 2022 at 9:09 history edited Edgar Bonet CC BY-SA 4.0
+ handle arrays of different sizes.
Oct 28, 2022 at 21:50 comment added user1584421 I am facing another problem. While i am able to assign an array from PROGMEM and pass it to a function, the program would have to move from case 0 to other cases. Thus, the base array would have to be reinitialized. Considering i have not used new, can i somehow or reassign the array? The arrays in PROGMEM are not of the same size.
Oct 28, 2022 at 19:53 comment added user1584421 I tried the pointer to array to flash before but it didnt work. Of course, maybe i did something wrong. However, i managed to get it to work with the code snippet you provided just before the edit. With the extra work of having to deduce the number of elements in the array in the flash. int ArrayElements = sizeof(my_array_0) / sizeof(int); And then i would create the empty array, just like your example, passing the arrayelements as its size. Yes, maybe its not as fast and memory efficient, but its three lines of code. And only one array in memory. I can get away with it.
Oct 28, 2022 at 19:49 vote accept user1584421
Oct 28, 2022 at 21:50
Oct 28, 2022 at 19:48 comment added user1584421 LOL! You just edited it in the same time i managed to get it to work! I will give it a read and mark it as accepted it later! Thank you so much for your answer!
Oct 28, 2022 at 19:46 history edited Edgar Bonet CC BY-SA 4.0
+ passing a pointer to flash to a function.
Oct 28, 2022 at 14:35 comment added user1584421 @chrisl No they arrays would be constants. So i don't even need my_array[]. Just pointers for the const arrays in Flash memory. Thanks for the tip!
Oct 28, 2022 at 14:28 comment added chrisl @user1584421 That depends on if you want to change the data. exec_func() can be written to read the data from flash, but you cannot change it there. If you need to do transformations to the raw array data, then you still need to copy it to RAM like Edgar did in his last code snippet.
Oct 28, 2022 at 14:06 comment added user1584421 Thank you very much for this detailed answer! The problem is: doing this will not help you save memory. Where do you think the compiler is going to store {DATA HERE}? Well, my logic behind this thought, was that I have seen much larger codebases in arduino. I thought that the CODE part was different than the variable assignment. I am reading about PROGMEM right now. One more question. If i pass a pointer to flash location, then i don't need the my_array[] at all? I just give a pointer to the my_array_0[] (for example)?
Oct 28, 2022 at 9:01 history edited Edgar Bonet CC BY-SA 4.0
+ zero coy approach.
Oct 28, 2022 at 8:53 history answered Edgar Bonet CC BY-SA 4.0