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.
memmove() is similar to memcpy() as it also copies data from a source to destination.
memcpy() function in C/C++ The function memcpy() is used to copy a memory block from one location to another. One is source and another is destination pointed by the pointer. This is declared in “string. h” header file in C language.
How to implement your own memcpy implementation in C? Implementation of memcpy is not a big deal, you need to typecast the given source and destination address to char* (1 byte). After the typecasting copy the data from the source to destination one by one until n (given length).
The memcpy() function in C++ copies specified bytes of data from the source to the destination. It is defined in the cstring header file.
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.
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 e.g. 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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With