Currently working on my DirectX game and using memset(0) (or ZeroMemory macro in VS if you wish) in constant buffers constructors to initialize all values with zeros and it works just fine. Problem occurs when I accidentally tried to initialize some other stuct that contains a vector this way. According to compiler (VS2010/VS2012) this causes "vector iterators incompatible", std::vector::end to be more precise. I can understand that memset might invalidate vectors iterators, but why "end" iterator is not working properly after I push back elements to the vector. Shouldn't it reposition vectors end iterator to right position (after last element)? Are all kind of std::some_container::end iterators affected by this aswell?
#include <vector>
class MyClass
{
public:
MyClass() {
memset(this, 0, sizeof(*this));
}
~MyClass() {}
std::vector<int>& GetData() { return m_data; }
float m_range;
private:
std::vector<int> m_data;
};
int main()
{
MyClass myClass;
myClass.GetData().push_back(1);
myClass.GetData().push_back(2);
for (auto it = myClass.GetData().begin(); it != myClass.GetData().end(); it++)
{
//stuff
}
}