copy arrays

Started by
35 comments, last by s_p_oneil 18 years, 9 months ago
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
Advertisement
#include <cstring>std::memcpy(index2+5,index1+5,3*sizeof(int));
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.

Wielder of the Sacred Wands
[Work - ArenaNet] [Epoch Language] [Scribblings]

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!
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).
Quote:Original post by Dave Hunt
Almost.


[Editted.]
Oop - well it's past midnight... [smile]
Quote:Original post by Kuladus
#include <cstring>std::memcpy(index2+5,index1+5,3*sizeof(int));


do note that this will only work reliably for POD types.
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats
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]
me22 got it right. There is little reason to use memcpy() anymore, std::copy() is infinitely superior.
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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]

This topic is closed to new replies.

Advertisement