[英]std::vector.resize not allocating memory sometimes?
It seems like std::vector.resize isn't actually allocating memory sometimes. 似乎std :: vector.resize有时实际上并未分配内存。 If I run this piece of code:
如果我运行这段代码:
std::vector<InstanceData> instanceData;
instanceData.resize(meshCount);
float randX = 0.0f;
float randY = 0.0f;
float randZ = 0.0f;
float maxX = 1000.0f;
float maxY = 1000.0f;
float maxZ = 1000.0f;
for(int i = 0; i < meshCount; i++)
{
randX = (rand() % 100) * 0.01f;
randY = (rand() % 100) * 0.01f;
randZ = (rand() % 100) * 0.01f;
instanceData[i].worldMatrix = DirectX::XMMatrixTranspose(DirectX::XMMatrixTranslation(randX * maxX, randY * maxY, randZ * maxZ));
instanceData[i].color = DirectX::XMFLOAT4(randX, randY, randZ, 1.0f);
}
The program sometimes crashes when I build for release but seemingly not when I build for debug. 当我为发布而构建时,该程序有时会崩溃,但是当我为调试而构建时,似乎不会崩溃。 I use Visual Studio 2013 to compile.
我使用Visual Studio 2013进行编译。
If I change the resize(meshCount)
to reserve(meshCount)
(or just remove it) and resort to using a temporary InstanceData which I then just fill with data and push into the vector it seemingly doesn't crash. 如果我将
resize(meshCount)
更改为reserve(meshCount)
(或只是将其删除)并诉诸于使用临时InstanceData,那么我只需填充数据并将其推入向量中,它似乎就不会崩溃。
If I attach the debugger to the release version it gives me this error message: 如果我将调试器附加到发行版,它将显示以下错误消息:
Unhandled exception at 0x00BB5BCB in path.exe: 0xC0000005: Access violation reading location 0x00000000. path.exe中0x00BB5BCB处未处理的异常:0xC0000005:访问冲突读取位置0x00000000。
Any idea what's going on? 知道发生了什么吗? Why can't I use
resize
? 为什么不能使用
resize
?
EDIT: Here's InstanceData 编辑:这是InstanceData
struct InstanceData
{
DirectX::XMMATRIX worldMatrix;
DirectX::XMFLOAT4 color;
};
解决方案是使用XMFLOAT4X4代替XMMATRIX
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.