I have some code which includes functionality whereby an array has another array copied in to it, which array gets copied in is dependent on various conditions. I then process (with reads and writes) the array to which the copying was done.
Please see my pseudocode, with requirements explained within its comments:
uint8_t AArray[10]={0};//sizes are fixed, and all guaranteed to be smaller than ArrayMaxSize
uint8_t BArray[38]={0};
uint8_t CArray[25]={0};
#define ArrayMaxSize 40
uint8_t CopiedArray[ArrayMaxSize]={0};
uint8_t VariableLength=0;
uint8_t CopyDone=0;
//initially various things are done to put data in to AArray, BArray, CArray...
//details not shown, but each array accessed place by place AArray[i]= and so on
//then copying is done
if(Condition){
VariableLength=sizeof(AArray);
memmove(CopiedArray,AArray,sizeof(CopiedArray)); //copying sizeof the destinition array ensures we can't overwrite an array end, copied places at >=VariableLength but also <ArrayMaxSize may hold junk data stolen from other things in memory, but this is copied only, "So what. We only do later processing on array places <VariableLength"
CopyDone=1;
}else if(OtherCondition){
VariableLength=sizeof(BArray);
memmove(CopiedArray,BArray,sizeof(CopiedArray));
CopyDone=1;
}else if(YetAnotherCondition){
VariableLength=sizeof(CArray);
memmove(CopiedArray,CArray,sizeof(CopiedArray));
CopyDone=1;
}else{
CopyDone=0;
}
//then yet later on we do
if(CopyDone){
for(uint8_t i=0; i<VariableLength; i++){
//read from CopiedArray[i] for certain i values
//write to CopiedArray[i] for other i values
//do stuff with the variables read out
//guaranteed never to act on places where i is >= to the length of the array which was copied in to CopiedArray
}
//do some processing on CopiedArray as a whole after certain i values have been rewritten above
}
//after this point we no longer care what happens to CopiedArray as long as it doesn't memory leak
//and we don't care about preserving or not the changes made to some i places during the for loop
//we are ok with AArray, BArray and CArray at this point either being just as they were befoe the conditional copying was done
//or with AArray, BArray and CArray at this point being changed in those ith places where the for loop made edit to CopiedArray
Is there a way to do this with pointers and save on SRAM usage?
I am writing this for use on an ATTiny AVR chip, with some initial testing on an Atmega328p Uno, when it goes to the ATTiny i don't have much SRAM to work with.
If I could avoid the need for ArrayMaxSize (40 in the example, but could be other sizes, depending on how big the XArray arrays are, (and there might be DArray, EArray... in future versions)) and just have one or two bytes of pointer then that would be nice.
Is there a way to do this, or does one have to create a pointer array just as big as CopiedArray presently is?
Also I think usage of pointers rather than memmove/memcpy (I hear that memcpy is usually the right one to choose, but memmove is perfectly safe to use anywhere a memcpy might be used) would speed up the code's running from having as many clock cycles to copy the whole array used in the copying if loop, to having a pointer swap taking very few clock cycles regardless of array size?
Is there a way to make CopiedArray in to some sort of copy by reference to the entries in the actual arrays, and not have to make CopiedArray as big as ArrayMaxSize?
P.S. please ignore considerations of variable scope in this, local variables would only help me reduce global usage of SRAM, not peak usage, and the way my full code operates I can't forget about AArray, BArray, CArray... once CopiedArray is full, nor can i do vice-versa. All the arrays must be kept global, and it is peak SRAM usage I need to keep low, reducing global SRAM usage would just give a false sense of security.
Thanks