我已经阅读了许多处理此问题的线程,其中大多数似乎都指出for循环索引已关闭。我仔细检查并没有发现明显的错误,我在这里缺少什么:
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
时,您需要在上面的一行中执行相同的操作。超出数组范围的写入会导致未定义的行为,这意味着几乎所有事情都可能发生。
关于c++ - 运行时检查失败#2-变量'NearID'周围的堆栈已损坏,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20199950/