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));
}
4
  • 1
    @Dayalrai: that's not copying Commented Jul 11, 2013 at 7:39
  • @KarolyHorvath ohh yes you are right.I was not thinking from that point of view.Deleting my misguiding comment. Commented Jul 11, 2013 at 7:41
  • 1
    In function int_copy temp is not initialized, so you may get an error on the last line. Commented Jul 11, 2013 at 7:42
  • actually, the last line is a deletium. assigning to local 'ptr' effectively does nothing at the end of this function. first line can be then removed, too. Commented Jul 11, 2013 at 7:44

2 Answers 2

5

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

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

Sign up to request clarification or add additional context in comments.

7 Comments

actually, he passes nbr as argument to copying function, so he can use memcpy right now instead of looping
memcpy is slow it copy byte by byte but coping int by int need less loop iterations.
@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: well, actually I was more worried about the sentinel, as you have to scan the array twice.
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..
|
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...

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.