Я прочитал кучу потоков, посвященных этой проблеме, и большинство из них, похоже, указывают на то, что индексирование for-loop отключено. Я дважды проверил и не обнаружил очевидных ошибок, чего мне здесь не хватает:
void VsuCNCtrl::resetRWID(bool state)
{
VsuCNComposite * thisCNAirport;
VsuCNComposite * thisRW;
VsuCNList RWList;
RWCString rwName;
RWCString ICAO;
int index, i, nbRW;
char FarID[2];
char NearID[2];
# ifdef DEBUGRWID
cout << "resetRWID: " << endl;
# endif
if (theTree().entries() < 1)
return;
VsuCNTreeNode * topNode = theTree().getTopTreeNode();
if (!topNode)
{
cout << "VsuCNCtrl(resetRWID) Warning: empty Database." << endl;
return;
}
thisCNAirport = (VsuCNComposite *)topNode->getCN();
thisCNAirport->getRelatives("VsuCNAirport","VsuCNRunway", RWList);
nbRW = RWList.entries();
if (state == true)
{
for (i = 0; i < nbRW; i++)
{
thisRW = RWList[i];
RWCString rwName = thisRW->getName();
int index = VsuCNRunwayId::getRunwayId(rwName);
sprintf(NearID, "%02d", index);
sprintf(FarID, "%02d", (index + nbRW));
updateUICNData(rwName, RWCString("RwNearId"), RWCString(NearID));
updateUICNData(rwName, RWCString("RwFarId"), RWCString(FarID));
}
}
# ifdef DEBUGRWID
cout << "resetRWID: nbRW " << nbRW << endl;
# endif
VsuCNRunwayId::resetRunwayId();
for (i = 0; i < nbRW; i++)
{
thisRW = RWList[i];
VsuCNRunwayId::addRunway(thisRW->getName());
}
for (i = 0; i < nbRW; i++)
{
thisRW = RWList[i];
rwName = thisRW->getName();
int nid = thisRW->get("RwNearId");
int fid = thisRW->get("RwFarId");
index = VsuCNRunwayId::getRunwayId(rwName);
if ( nid == 0 && fid == 0)
{
sprintf(NearID, "%02d", index);
sprintf(FarID, "%02d", (index + nbRW));
}
else
{
if (nid != index)
sprintf(NearID, "%02d", nid);
else
sprintf(NearID, "%02d", index);
if (fid != (index + nbRW))
sprintf(FarID, "%02d", fid);
else
sprintf(FarID, "%02d", (index + nbRW));
}
updateUICNData(rwName, RWCString("RwNearId"), RWCString(NearID));
updateUICNData(rwName, RWCString("RwFarId"), RWCString(FarID));
# ifdef DEBUGRWID
cout << "rwName : " << rwName << endl;
cout << "RwNearId : " << RWCString(NearID) << endl;
cout << "RwFarId : " << RWCString(FarID) << endl;
# endif
}
return;
}
заранее спасибо
Вот проблема:
sprintf(FarID, "%02d", (index + nbRW));
Массив FarID
имеет два символа, но вы пишете три символа в массив. Помните, что каждая строка также содержит специальный символ, который завершает строку.
Вы делаете то же самое на линии чуть выше, когда пишете три символа в двухсимвольный массив NearID
.
Запись за пределы массива приводит к неопределенному поведению, а это означает, что почти все может случиться.