<分区>
我正在为 C++ 中的 STL vector 而苦苦挣扎。有一段时间(实际上是几个月),我只为 x64 编译我的项目,并且一切正常,当我想起我也需要它在 x86 机器上工作时(Win32 DirectX 应用程序)。 当然,编译器错误立即出现,16 位对齐的结构将不会对齐,例如传递给一个函数(那些是 XMVECTOR 和 XMMATRIX,以及 XMFLOATA 结构)。我成功地解决了这些问题,并让我的程序再次运行,直到我尝试通过 push_back() 和 emplace_back() 将这些结构加载到 std::vectors 中。为了清楚起见,我给出了一个简短的示例代码:
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
}
};
这是我的结构,我尝试将多个骨架加载到一个 vector 中,例如:
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() );
}
完整代码在这里:http://pastebin.com/6D1wF4Vh
我尝试将 __declspec_align(16) 设置为所有内容,尝试用 Armature* 填充 vector 并在 new 运算符中执行 aligned_malloc,但是程序每次退出时都会出现访问冲突读取位置 0xFFFFFFF 有时在 vector 中。 h 在 push_back 函数上,有时在初始化 XMVECTOR 结构等时。
我是不是漏掉了一些微不足道的东西?如果有人能指出显而易见的地方或提供任何帮助,我将不胜感激。