0
\$\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;
}
\$\endgroup\$
  • \$\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\$ – glampert Mar 6 '15 at 17:54
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;
    }
}
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.