EDIT: I originally had two (unintentional) do loops, with shadowed index variables iCurrWin. This was the problem. Why it worked in one code set and the bottom isn’t clear, but it’s not something you’d ever intentionally do anyway so it’s not important. See below for a discussion on the merits of braces and how my usage is apparently nonstandard, though, if you like. And thanks for all the suggestions.
So, I ran into something today while writing a program that not only did I not understand, I still don’t. My code originally was this:
vector<Mat3> M1(iWindows,Mat3::ZERO),M2(iWindows,Mat3::ZERO);
vector<Mat3> M3(iWindows,Mat3::ZERO),M4(iWindows,Mat3::ZERO);
for (int iCurrWin=0; iCurrWin < iWindows; ++iCurrWin)
{
for (size_t iRow=0; iRow < 3; ++iRow)
for (size_t iCol=0; iCol < 3; ++iCol)
{
//define cInd1, cInd2, rInd1, rInd2
(M1[iCurrWin])(iRow,iCol)= t3_S[iCurrWin](iRow,iRow,iCol,iCol);
(M2[iCurrWin])(iRow,iCol)= t3_S[iCurrWin](iRow,iRow,cInd1,cInd2)
+t3_S[iCurrWin](iRow,iRow,cInd2,cInd1);
(M3[iCurrWin])(iRow,iCol)= t3_S[iCurrWin](rInd1,rInd2,iCol,iCol);
(M4[iCurrWin])(iRow,iCol)= t3_S[iCurrWin](rInd1,rInd2,cInd1,cInd2)
+t3_S[iCurrWin](rInd1,rInd2,cInd2,cInd1);
}
// define eToS
M1[iCurrWin]*=eToS;
M2[iCurrWin]*=eToS;
M3[iCurrWin]*=2.0*eToS;
M4[iCurrWin]*=2.0*eToS;
}
I have now changed it to this:
vector<Mat3> M1,M2,M3,M4;
for (int iCurrWin=0; iCurrWin < iWindows; ++iCurrWin)
{
Mat3 m3_LM1, m3_LM2, m3_LM3, m3_LM4;
for (size_t iRow=0; iRow < 3; ++iRow)
for (size_t iCol=0; iCol < 3; ++iCol)
{
//define cInd1, cInd2, rInd1, rInd2
m3_LM1(iRow,iCol) = t3_S[iCurrWin](iRow,iRow,iCol,iCol);
m3_LM2(iRow,iCol) = t3_S[iCurrWin](iRow,iRow,cInd1,cInd2)
+t3_S[iCurrWin](iRow,iRow,cInd2,cInd1);
m3_LM3(iRow,iCol) = t3_S[iCurrWin](rInd1,rInd2,iCol,iCol);
m3_LM4(iRow,iCol) = t3_S[iCurrWin](rInd1,rInd2,cInd1,cInd2)
+t3_S[iCurrWin](rInd1,rInd2,cInd2,cInd1);
}
//define eToS
M1.push_back(m3_LM1*eToS);
M2.push_back(m3_LM2*eToS);
M3.push_back(m3_LM3*2.0*eToS);
M4.push_back(m3_LM4*2.0*eToS);
}
There are no function calls here; the things that look like function calls are overloaded indexing operators to index into a matrix or a 4th order tensor. The other overloaded operators have been independently verified to work properly. Everything works fine if iWindows == 1.
The latter code works fine for arbitrary iWindows; the former does not. When I run through the loop in the former, initial assignment works fine if I dump the values directly to a file, but on inserting a check loop directly after the code to simply re-output the vectors, all but the last are screwed. This smells like I’m overwriting memory somehow, but I don’t see how.
Can anyone tell me what I did wrong?