I want the fastest possible way to perform the following type of action:
//given the value of a variable
//copy a different source array in to a destination array
//then I make some changes to some elements in the destination array
//do some stuff with this array
//and later I want to be able to get back to the situation at the start where the Destination array is ready to accept new things copied in to it
I would be ok to copy by "reference" or by "value", but cannot guarantee that the arrays are the same length, I can guarantee the source one will be shorter or equal to the destination's length.
After copying I will want to modify some members in the destination array, I don't mind either way whether these modifications of members do or do not change the values of those members in the source array. I am ok either way with that.
The slow way is:
uint8_t DestArray[40]={0}
uint8_t DestArrayOriginal[40]={0}
uint8_t Source1Array[7]={0,1,2,3,4,5,6};
uint8_t Source2Array[7]={0,1,2,3,4,5,6,7,8};
uint8_t Mode=0;
uint8_t Var2=0;
for(uint8_t k=0; k<sizeof(DestArray); k++){
DestArrayOriginal[k]=DestArray[k];
}
//do some stuff which results in the Mode variable getting a value
//and which puts a new value in Var2
if(Mode==1){
uint8_t Length=sizeof(Source1Array);
for(uint8_t k=0; k<Length; k++){
DestArray[k]=Source1Array[k];
}
}else if(Mode==2){
LengthToSend=sizeof(Source2Array);
for(uint8_t k=0; k<Length; k++){
DestArray[k]=Source2Array[k];
}
}else if(Mode==3){//and so on, many different modes possible correspoding to many diferent source arrays which can be used
}
//and as I said after the copy I would be doing something like
DestArray[3]=Var2-4;
DestArray[12]=Var2;
//I don't care if Source1Array or Source2Array 's [3] element gets moified here or not
//obviously I don't want Source1Array or Source2Array 's [12] element modified though
//, as they don't have one so such a change would step on other variables in my code
//at some further later points I'd also like to be able to do
for(uint8_t k=0; k<sizeof(DestArrayOriginal); k++){
DestArray[k]=DestArrayOriginal[k];
}
but those for() loops are slow.
But I wondered if there was a pointer based way of doing it. If so it would be effectively just a few operations of the AVR's clock cycle to change a pointer value.
ifMode==1){
DestArray=Source1Array; //array syntax is aterall just a cover for pointers
}else if(Mode==2){
DestArray=Source2Array;
}
The thing is this looks like it would lose the reference to whatever DestArray was initialised as, a memory leak which would get worse every time different things were copied to DestArray.
And as Source2Array and Source1Array aren't necessarily the same length, and either of them is shorter than DestArray, real trouble could, I think, crop up if you wrote to the [12] place of DestArray once DestArray had become instead a pointer to Source1Array or Source2Array. And this could cause even more problems if one then makes that return of DestArray's contents to its originals, from a pointer perspective that could wipe over many bytes worth of other variables.
How can I quickly make such copies? How can I, very quickly, fill an array with the contents of a different array, selecting which different array dependng on a variable's value.
Thank you