I wrote this code to find optimal warping path of Dynamic time warping , it returns a struct .
static inline double min (double x, double y,double z )
{
if(x < y && x < z)
return x;
else if(y < x && y < z)
return y;
else if(z < x && z < y)
return z;
}
struct backtracking_result
{
vector<int> i;
vector<int> j;
};
backtracking_result backtracking(vector<vector<double> > mGamma)
{
backtracking_result br;
cout<<"test"<<mGamma.size()<<endl;
int i=mGamma.size();
cout<<"test"<<endl;
int j=mGamma.at(0).size();
cout<<"test"<<endl;
cout<<i<<j<<endl;
do
{
cout<<"test"<<endl;
if(i == 1)
j=j-1;
else if(j == 1)
i=i-1;
else
{
if(mGamma[i-1][j] == min(mGamma[i-1][j],mGamma[i][j-1],mGamma[i-1][j-1]))
i=i-1;
else if(mGamma[i][j-1] == min(mGamma[i-1][j],mGamma[i][j-1],mGamma[i-1][j-1]))
j=j-1;
else
{
i=i-1;
j=j-1;
}
}
br.i.push_back(i);
br.j.push_back(j);
}while((i > 1)&&(j > 1));
return br;
}
When i call this function , get this error from gdb debugger
Program received signal SIGSEGV, Segmentation fault.
0x000000000041eafb in VectorDTW::backtracking(std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >) ()
Where is the error ?
Stacktrace :
#0 0x000000000041eb17 in VectorDTW::backtracking(std::vector<std::vector<double, std::allocator<double> >, std::allocator<std::vector<double, std::allocator<double> > > >) ()
#1 0x0000000000420195 in VectorDTW::fastdynamic(std::vector<DollarRecognizer::Point2D, std::allocator<DollarRecognizer::Point2D> >&, std::vector<DollarRecognizer::Point2D, std::allocator<DollarRecognizer::Point2D> >&) ()
#2 0x000000000041d18b in main ()
The answers helped me figure out the problem , I added these two lines after calculating i & j at from the matrix ,
i=i-1;
j=j-1;
Now the program doesn’t try to access the last element . Thanks .
But i still cannot understand the reason for down-votes .
Since
jis the size of the internalvector<double>,mGamma[i-1][j]does not exist. You have the typical off-by-one error.