1

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.

4

2 回答 2

2

Microsoft seem to recommend that you use XMFLOAT4 and XMFLOAT4X4 to store the values, and use XMVECTOR and XMMATRIX for calculations.

From the DirectXMath help : (http://msdn.microsoft.com/en-us/library/windows/desktop/ee418725(v=vs.85).aspx#basic_usage)

"However, often it is easier and more compact to avoid using XMVECTOR or XMMATRIX directly in a class or structure. Instead, make use of the XMFLOAT3, XMFLOAT4, XMFLOAT4X3, XMFLOAT4X4, and so on, as members of your structure. Further, you can use the Vector Loading and Vector Storage functions to move the data efficiently into XMVECTOR or XMMATRIX local variables, perform computations, and store the results. There are also streaming functions (XMVector3TransformStream, XMVector4TransformStream, and so on) that efficiently operate directly on arrays of these data types."

You can store the XMVECTOR and XMMATRIX types but specially if you store them in vectors and so on it can be very hard to get the alignment requirements right as the default allocators don't meet the requirements.

于 2013-09-05T07:39:06.050 回答
0

while(!file.eof()) doesn't do what you think it does. eof() only returns true after trying to read past the end of file. Do you check the error state of file before calling push_back(...)? If not, the last element will be initialized with undefined values. And depending on your software, may, or may not, cause runtime errors.

于 2013-09-04T20:19:26.303 回答