简体   繁体   中英

What is the memcpy equivalent in C++

What would be the equivalent of using memcpy to a buffer, or string in C++?

For example:

char message_buffer[32];
uint16_t n = 457u;
memcpy(message_buffer, &n, sizeof(n));
...

Something like:

std::string message_buffer;
uint16_t n = 457u;
std::copy(messagebuffer, n);

Is there no C++ equivalent? Do I just stick to using memcpy, but instead with std::string?

std::string message_buffer;
message_buffer.resize(32);
uint16_t n = 457u;
memcpy(&message_buffer[0], &n, sizeof(n));

I guess I should give more background on what I am trying to do - simply I am sending and receiving data using sockets. The data needs to be pre-appended with the 16 bit binary representation of that number (457). So the examples I gave are but the first step, afterwards I copy the information I would like to send to the buffer, knowing that the first 2 bytes (16bits) contain the "magic number" binary.

An std::string is for strings. If you want a buffer of bytes, you should use std::vector<char> (or its signed/unsigned counterparts) or std::array for small fixed length buffers instead.

Using std::copy is pretty much always the way to go, especially while in the "high level C++ realm" with all its classes etc.

However, I would say when you are dealing with low level constructs like byte buffers, the C++ function std::memcpy is the most appropriate choice. Just remember, std::memcpy is a "dumb" function that only copies bytes, but considering that we are trying to fill a byte buffer, that is what we want.

std::array<char, 32> buffer;
uint16_t n = 457u;
std::memcpy(buffer.data(), &n, sizeof(n));

Of course, if you want to store a more complex class with eg pointer members or non-trivial copy constructors (anything that is not "plain old data") in some byte buffer, you would need to serialize the object to get meaningful results, just as you would in C for structs with pointer members.

Analogously, you cannot simply use memcpy to get the data from the buffer back into some complex type. You would have do de-serialize the raw byte data in an appropriate way.

For the specific task you're doing, copying the byte representation of an object of some unrelated type into a buffer, memcpy is the appropriate function. This is specifically because it's one of the few legal ways to do this sort of thing independent of the types involved. Of course its use is limited as it's only valid with trivially copiable types.

When you're copying something where you don't want to destroy static type information, C++ provides other methods, such as the std::copy algorithm which operates on iterators rather than just void* as memcpy() does.

To encode the 16-bit number 457 into a byte array such that the first byte is 01010111 and the second byte is 00000100 in a platform-independent way, use:

unsigned char buffer[2];
const uint16_t n = 457u;
buffer[0] = n & 0xFF;
buffer[1] = (n >> 8) & 0xFF;

You can wrap this logic in a function if you need to do it often.

You cannot assume memcpy will have the same affect. See: https://en.wikipedia.org/wiki/Endianness

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM