2
#include<stdio.h>

void int_copy(int* ptrA,int* ptrB,int nbr){
 //int* temp = ptrB;
 while(nbr != 0){
  *ptrB++ = *ptrA++;
  nbr--;
 }
 *ptrB = -1;
 //ptrB = temp;
}

int main(){
 int stringa[40] = {100,101,102,103,104,105,106,107,108,109,110,-1};
 int stringb[40] = {0};
 int *ptr;
 int *ptr1;
 int len = 0;

 ptr = stringa;
 ptr1 = stringb;

 while(*ptr != -1){
  *ptr++;len++;
 }

 printf("\n len : %d \n",len);

 int_copy(stringa,stringb,len);

 while(*ptr1 != -1){
  printf("%d\t",*ptr1);
  *ptr1++;
 }

 return 0;
}

I was trying out an example program to copy an array of integers to another integer array. Is there another way to do it in a more efficient way.

EDITED :

void int_copy(int* ptrA,int* ptrB,int nbr){
 memcpy(ptrA,ptrB,(sizeof(int)*nbr));
}
Angus
  • 12,133
  • 29
  • 96
  • 151

2 Answers2

5

Don't use a sentinel (-1), store the length.

Then you can use memcpy - hint: copy sizeof(int)*len bytes.

Karoly Horvath
  • 94,607
  • 11
  • 117
  • 176
  • actually, he passes `nbr` as argument to copying function, so he **can** use memcpy right now instead of looping – quetzalcoatl Jul 11 '13 at 07:33
  • memcpy is slow it copy byte by byte but coping int by int need less loop iterations. – Grijesh Chauhan Jul 11 '13 at 07:34
  • 1
    @GrijeshChauhan: not on my compiler/processor. In my version, it first copies in blocks of fourbytes (register size), then optionally copies a tail of 2 and 1 byte. I bet that if I had x64 then it would start with copying blocks of 8 bytes. HMM.. or maybe I remembe rwrong and it was memmove.. ah well. platform specific IMHO. – quetzalcoatl Jul 11 '13 at 07:35
  • 1
    @quetzalcoatl: well, actually I was more worried about the sentinel, as you have to scan the array twice. – Karoly Horvath Jul 11 '13 at 07:38
  • guys.. it's all just initial setup. he could even hardcode it. If he asks about how to make the copying better, then it's not _that_ important _how_ does he get the array and the number of elements.. once he has input and output buffers and knows the number, he can use memcpy - and that's the answer.. – quetzalcoatl Jul 11 '13 at 07:42
  • @Karoly : I tried as you said, but its not getting copied properly. I can give len to the size of memcpy rather than giving sizeof(int) * len. Please see my edit – Angus Jul 11 '13 at 10:16
  • @quetzalcoatl Well After reading [this answer](http://stackoverflow.com/a/19187600/1673391) looks you are correct, memcpy is smarter than I could have guess :) + to the answer. – Grijesh Chauhan Feb 18 '14 at 18:09
0

I was trying out an example program to copy an array of integers to another integer array. Is there another way to do it in a more efficient way.

Since this is a sample program, I'm not sure how your real program looks like. But as for allocating an array: you can either do it statically (as in your example) or dynamically using e.g. malloc(). In either way, the size of the array is known (either fixed in your source code or indirectly through the size parameter to the malloc call).

So yes, since you should know the size of the array, you can use memcpy() which is more efficient since its implementation is optimized for the architecture it runs on.

BTW: an exception could be a library that allocates the memory for you and does not provide back the length of the array. But I never came across such a situation...

grasbueschel
  • 879
  • 2
  • 8
  • 24