4

What is the most efficient way to copy an array of 1000 ints from one array to another in objective C?

This will be running on an iphone within some drawing code so its important to be as efficient as possible.

Thanks.

2 Answers 2

3

if the concern is efficiency, I'm assuming this is a C array of ints. If so, you can use memcpy().

For example:

int copyOfArr[4], arr[4] = {0,1,2,3};

memcpy(copyOfArr, arr, sizeof(arr));

Hope that helps.

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

2 Comments

I'm actually having a hard time finding good documentation on how to use memcpy() for copying arrays of ints. Any pointers?
THanks that does help - how would you copy subsets of the array (i.e. from StartIndex to endIndex)?
2

If you are talking about a normal C array, then memcpy is the most efficient way. If you are talking about NSArray, then send a "copy" message.

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.