1
\$\begingroup\$

Is there a better way than this to copy the values from source to destination when the blocks of memory you want to copy to and from are different sizes? Also considering you have no control over making sure the sizes match to begin with.

while (count--) {
 memcpy(dst, src, dst_stride);
 src += src_stride;
 dst += dst_stride;
}
Heslacher
50.9k5 gold badges83 silver badges177 bronze badges
asked Mar 6, 2015 at 10:10
\$\endgroup\$
1
  • \$\begingroup\$ This seems more of a StackOverflow kind of question. Perhaps migrating it to that site would result in more feedback than in here... \$\endgroup\$ Commented Mar 6, 2015 at 17:54

1 Answer 1

1
\$\begingroup\$

If it is possible that the strides match then you can add a if check to just copy it all in one go:

if(src_stride == dst_stride){
 memcpy(dst, src, count * dst_stride);
} else {
 while (count--) {
 memcpy(dst, src, dst_stride);
 src += src_stride;
 dst += dst_stride;
 }
}
answered Mar 6, 2015 at 10:39
\$\endgroup\$

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

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.