Jump to content
  • Advertisement
Sign in to follow this  
Christoph

copy arrays

This topic is 4712 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi, can you tell me what is the fastest method to copy a part of an array to another array? For example, I have two pointers: int *index1; int *index2; index1 = new int[10]; index2 = new int[10]; Having filled these arrays, I want to copy, let's say, element 5 to 8 from index1 to index2. How do I best realize this? thx

Share this post


Link to post
Share on other sites
Advertisement
int i;
for(i = 5; i <= 8; ++i)
index2 = index1;



Premature optimization is the root of all evil. Unless you're absolutely 100% sure with profiler evidence that your array copy is too slow (and maybe not even then), you are not sure enough to mess around with this kind of optimization. On a 32-bit system, the fastest you'll get in general is moving blocks of data in chunks of 32 bits, which, if your data is in ints, you're already doing.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
Try:

memcpy(index2, index1 + (sizeof(int) * 5), sizeof(int) * (8 - 5));

It should be very fast and efficient that way. I don't know of a faster method but maybe there is one, let's see what other people say...

Be aware, that memcpy does not check for memory overflow on the target memory (index2 in this case). So you need to make sure that index2 offers enough space for the operation!

Share this post


Link to post
Share on other sites
Quote:
Original post by Kuladus

#include <cstring>
std::memcpy(&(index2+5),&(index1+5),3*sizeof(int));


Almost. You don't want to take the address of (index2+5). (index2+5) is already an address. Same with (index1+5).

Share this post


Link to post
Share on other sites
Well, you're using C++, so you should use the std::copy function:

#include <algorithm>

std::copy( index1+5, index1+8, index2 ); // copies index1[5],index1[6],index1[7]




A good implementation will implement that, for PoD types, as a memcpy, but will do the right thing if you change index1 and index2 to arrays of non-PoD types.

Note of course that you should also be using std::vector and not deaing with the memory yourself, or at the very least sticking the memory into a scoped_array<int> or shared_array<int> for RAII and exception-safety.

Fruny - added std prefix.

[Edited by - Fruny on June 30, 2005 10:34:37 AM]

Share this post


Link to post
Share on other sites
me22 got it right. There is little reason to use memcpy() anymore, std::copy() is infinitely superior.

Share this post


Link to post
Share on other sites
Quote:
Original post by Fruny
me22 got it right. There is little reason to use memcpy() anymore, std::copy() is infinitely superior.



Not quite infinite. It's 9 characters to type instead of 6. [smile]

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

Participate in the game development conversation and more when you create an account on GameDev.net!

Sign me up!