I am wondering if it is safe to use memcpy on a struct that contain arrays and methods(just some getters and setters since indexing of arrays is unusual and i have to map it somehow). I know that its is safe for PODs and I am unsure if my struct would be considered POD or not?
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.
memcpy() function is an inbuilt function in C++ STL, which is defined in <cstring> header file. memcpy() function is used to copy blocks of memory. This function is used to copy the number of values from one memory location to another. The result of the function is a binary copy of the data.
Description. In generated code, the Memory Copy block copies variables or data to and from processor memory as you have configured with block parameters. Your model can contain as many of these blocks as you require to manipulate memory on your processor.
You can use memcpy
if the struct
is TriviallyCopyable.
You can check whether your struct
is trivially copyable by using std::is_trivially_copyable
.
Also, as pointed out by @JohanLundberg in a comment, the destination address must be 0
modulo std::alignment_of<T>
. You can read more about alignment requirements of objects at http://en.cppreference.com/w/cpp/language/object#Alignment.
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