[Solved] Copying to std::chrono::milliseconds with memcpy() gives error -Werror=class-memaccess [closed]

The safer way to implement this that doesn’t rely on knowing the internal layout of std::chrono::duration would be to copy to an integer then pass the integer to the duration: std::array<unsigned char, 6> myArray = {123, 123, 112, 0, 15}; int64_t milliseconds = 0; memcpy(&milliseconds, &myArray, 5); std::chrono::milliseconds dest{milliseconds}; 3 solved Copying to std::chrono::milliseconds with … Read more

[Solved] Trouble copying the string using memcpy

The command strcat(a[0],”\0″); is working on strings which are already terminated by \0. Otherwise it doesn’t know where to append the second string. In your case a[0] is not terminated, so the function will induce undefined behavior. You can do the following instead: a[0][n] = ‘\0’; (the same is for the rest of a elements) … Read more

[Solved] Copy structure to a different structure [closed]

If instead of copying all bytes in A, you only copy the number of bytes that B expects, you will achieve your desired result: memcpy(&v2, &v1, sizeof(v2)); // remember that the first argument is the destination However, this is not good coding style. With this minimal code example, it is hard to tell, but you … Read more