I.e. can I do this:

Matrix myMat;
std::memcpy(&myMat, &dxMat, sizeof(XMMATRIX));
std::memcpy(&dxMat, &myMat, sizeof(XMMATRIX));

Matrix is my own class which contains 16 contiguous float variables (only). Also can I do the same with my vector class (4 floats) and XMVECTOR?

In other words, is XMMATRIX a POD?

  • \$\begingroup\$ memcpy should technically work. You just need to make sure that your data structure matches the data structure you're copying from exact, or you may run into some odd results. \$\endgroup\$ – user39686 Jan 6 '14 at 20:08

XMMATRIX is a Plain Old data structure, it is defined like this:

struct XMMATRIX {
  XMVECTOR r[4];

You can use memcopy with POD. XMMATRIX is not different, it is only designed so it is always 16byte aligned so it can be used with SSE registers.

But keep in mind that using memcpy is error prone and could introduce hard to find bugs. I suggest you use a constructor in your Matrix class that takes XMMATRIX, and copy it internally. This way you take advantage of: 1. Type safety, instead of the void* that is passed to memcpy. 2. Your code won't have memcpy all over the place. So less bugs, and even if you used memcpy inside the constructor and have a bug you will only solve it once. According to the DRY principle.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.