Copying Array into another array

Simply, I would like to copy an array A and past it in array B. I have an array_1={1,2,3,4,5}; and I have an array_2={6,7,8,9,0};. I am not able to use strcpy or memcpy in Arduino.

int array_1[5]={1,2,3,4,5}; //Array 1 with 5 elements
int array_2[5]={6,7,8,9,0}; //Array 2 with 5 elements

void setup(){
Serial.begin(9600);
copy(array_1, array_2, 5); //Call a function copy 
}

void loop(){
}

void copy(int arrayOriginal[], int arrayCopy[], int arraySize){ //Copy function
  for(int i=0; i<arraySize; i++){
  arrayOriginal[i]=array_1[i]=array_2[i];
  //tableauCopie[i]=tableauOriginal[tableau_2[i]];
  Serial.print(arrayOriginal[i]);
  delay(50);
}
}

I believe what you want is to utilize the first option pepe gave, which is the fastest way:

// Function to copy 'len' elements from 'src' to 'dst'
void copy(int* src, int* dst, int len) {
    memcpy(dst, src, sizeof(src[0])*len);
}

But that requires the string.h library to supply the code whereas the latter method does not.

The string.h library is very useful once understood though. http://www.nongnu.org/avr-libc/user-manual/group__avr__string.html

Pointers cost 2 bytes each!

byte i = 5; // good to 255 elements
while ( i-- ) *( dest + i ) = *( src + i ); // dest and src are your 2 array names

Correct, the second fastest way without using any libraries would be the pointer loops.

You can see the difference in speed using a simple sketch:

void copyA(int* src, int* dst, int len) {
    memcpy(dst, src, sizeof(src[0])*len);
}

void copyB(int* src, int* dst, int len) {
    for (int i = 0; i < len; i++) {
        *dst++ = *src++;
    }
}

void copyC(int* src, int* dst, int len) {
    for (int i = 0; i < len; i++) {
        dst[i] = src[i];
    }
}

void setup() {
    int start, finish, elements = 1000;
    int source[elements], destination[elements];
    
    for (int i = 0; i < elements; i++) {
        source[i] = i;
    }
    
    Serial.begin(9600);
    
    Serial.print("memcpy() time in µs: ");
    start = micros();
    copyA(source, destination, elements);
    finish = micros() - start;
    Serial.println(finish);
    
    Serial.print("pointers time in µs: ");
    start = micros();
    copyB(source, destination, elements);
    finish = micros() - start;
    Serial.println(finish);
    
    Serial.print("indexing time in µs: ");
    start = micros();
    copyC(source, destination, elements);
    finish = micros() - start;
    Serial.println(finish);
}

void loop() {
    // Nothing to do here
}

Keep in mind that, with the memory limitations of the Arduino, this is basically micro-optimization. The difference is a matter of a few microseconds, and it won’t make a difference regardless of which method you use, since you’ll sooner run out of memory than notice the speed difference here.

You might be adding overhead by making those into functions. All the parameters have to be pushed onto then popped off the stack unless the compiler optimizes even that out.

Yeah, depending on the compiler a function call is going to add overhead. Considering they were each called only once and have the same type contract, it should be nearly the same. You can remove the function calls from the equation and you'll still get practically the same differences in times for the operations.

Here is an article I wrote a while ago on the topic, http://arduino.land/FAQ/content/6/30/en/how-to-copy-an-array.html Most of its covered here, but has a few additional tips.

1 Like

I get the feeling that the memcpy function got more optimized, probably inlined w/o stack use.

GoForSmoke: I get the feeling that the memcpy function got more optimized, probably inlined w/o stack use.

Yeah, modern compilers will, GCC has builtin versions of many C standard functions like memset, memcpy, strcpy, strcmp,...

https://gcc.gnu.org/onlinedocs/gcc/Other-Builtins.html