Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C++ Converting memcpy to std::copy

Tags:

c++

copy

I wanted to replace my "memcpy" with "std::copy", but I just can't find the right way for passing the args.

My old memcpy code was

memcpy(&uFeatures.Features[0], &((char*)(m_pData))[iBytePos],iByteCount);

I tried various things with std::copy, but they all did not work.

Could anybody help, please?

like image 270
tmighty Avatar asked Nov 03 '22 22:11

tmighty


1 Answers

from your syntax it seems it will be smething like this. (assuming Features[0] is char*, if not you need to cast (check comments) )

std::copy(&uFeatures.Features[0], &uFeatures.Features[0]+iByteCount, &((char*)(m_pData))[iBytePos])

and as I see in your comments

I would like to let the compiler decide what it prefers (memcopy, memmove or anything else), that is why I want to use std::copy

std::copy will not be translated to memcpy by compiler. they are unrelated. copy will do something like this

while (first != last) {
    *d_first++ = *first++;
}
like image 103
Neel Basu Avatar answered Nov 12 '22 11:11

Neel Basu