I met a problem when doing my project. I abstract the problem to the following code.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
#include <iostream>
#include <vector>
#include <boost/numeric/ublas/matrix.hpp>
#include <boost/numeric/ublas/io.hpp>
#include <boost/numeric/ublas/matrix_proxy.hpp>
usingnamespace std;
namespace ublas = boost::numeric::ublas;
int main()
{
vector<ublas::matrix<double> > m;
m.push_back(ublas::matrix<double>());
ublas::matrix<double> & n = m.back();
m.push_back(ublas::matrix<double>());
n = ublas::scalar_matrix<double>(3,3);
for(vector<ublas::matrix<double> >::iterator it = m.begin() ; it != m.end() ; it++)
cout<<*it<<endl;
}
The code can be compiled correctly. But running the executable will cause a segment fault. If line 15 of the code is removed, the executable works fine. What may be the problem. Thanks
ublas::matrix<double> & n = m.back();
m.push_back(ublas::matrix<double>());
// the reference 'n' is no longer a valid reference
// (the sequence might have been relocated in memory)