繁体   English   中英

运行时检查失败#2-变量'NearID'周围的堆栈已损坏

[英]Run-Time Check Failure #2 - Stack around the variable 'NearID' was corrupted

我已经阅读了许多处理此问题的线程,其中大多数似乎都指出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时,可以在上方的一行中执行相同的NearID

超出数组范围的写入会导致未定义的行为 ,这意味着几乎所有事情都可能发生。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM