I created a vector which contains 100 Eigen::Matrix4f. But when I use transforms[i]
to access its elements, the programme shows "core dumped: segmentation fault". When I use push_back()
function to initialize it, it works well.
I checked the size of the vector. It is 100. I am confused about it.
It is in Ubuntu 16.04. I use CMake + GCC to compile it.
Segmentation fault C++ code
std::vector<Eigen::Matrix4f> transforms(100);
//transforms.size() is 100. I checked it.
for(int i = 0; i < transforms.size(); i++)
{
//I use transforms[i] to access the element in transforms.
transforms[i] = a_function_returns_a_valid_Matrix4f();
}
The following code works by using std::vector::push_back()
.
std::vector<Eigen::Matrix4f> transforms;
for(int i = 0; i < 100; i++)
{
//When I use push_back() to initialize transforms, it works.
transforms.push_back(a_function_returns_a_valid_Matrix4f());
}
It seems std::vector
allocated the memory correctly. I checked its size.
But failed to access its element by using transforms[i]
.