I am struggling with STL vectors in C++. I was compiling my project for only x64 for a time (actually for months), and everything was working fine, when I remembered that I need this to work on x86 machines also (Win32 DirectX application). Of course, the compiler errors came up instantly, that 16-bit aligned structures will not be aligned if for example passed to a function (those are the XMVECTOR and XMMATRIX, and XMFLOATA structures). I resolved those with success, and got my program running again, until I tried to load those structures into std::vectors by push_back() and emplace_back(). I give a short example code to be clear:
struct Armature
{
string name;
int index;
//{...} There are much more
XMVECTOR translation; //Four 32 bit floating point components aligned on a 16 byte
// boundary and mapped to hardware vector registers
XMVECTOR rotation;
XMVECTOR scale;
XMMATRIX world; //it is like 4 XMVECTORs (4*4 matrix)
Armature(){
//I initialize everything here
}
};
That is my structure, and I try to load several Armatures into a vector, like:
std::vector<Armature> armatures;
while(ThereAreMoreArmaturesToRead()){ //roughly like this, I just parse a file and load up the information from there to fill out my Armature structure
armatures.push_back( Armature() );
}
Full code here: http://pastebin.com/6D1wF4Vh
I tried setting __declspec_align(16) to everything, tried filling out the vector with Armature* and do aligned_malloc in the new operator, but the program exits every time with an Access violation reading location 0xFFFFFFF sometimes in the vector.h on the push_back function, sometimes while initializing an XMVECTOR struct, etc.
Am I missing something trivial here? Then I would be very grateful if someone could point out the obvious, or for any help.