简体   繁体   English

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

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

I have read a bunch of threads dealing with this issue, and most of them seem to point to for-loop indexing being off. 我已经阅读了许多处理此问题的线程,其中大多数似乎都指出for循环索引已关闭。 I double checked and found no obvious errors, what am I missing here: 我仔细检查并没有发现明显的错误,我在这里缺少什么:

 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; 
}

Thanks in advance 提前致谢

Here is the problem: 这是问题所在:

sprintf(FarID,  "%02d", (index + nbRW));

The FarID array is two character, but you write three characters to the array. FarID数组是两个字符,但是您将三个字符写入该数组。 Remember that every string also contains a special character that terminates the string. 请记住,每个字符串还包含一个终止字符串的特殊字符。

You do the same on the line just above that, when writing three characters to the two-character array NearID . 将三个字符写入两个字符的数组NearID时,可以在上方的一行中执行相同的NearID

Writing beyond the bounds of an array leads to undefined behavior , which means that almost anything can happen. 超出数组范围的写入会导致未定义的行为 ,这意味着几乎所有事情都可能发生。

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

相关问题 运行时检查失败 #2 - 变量“IDNumber”周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'IDNumber' was corrupted 运行时检查失败#2-变量&#39;indices&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'indices' was corrupted 运行时检查失败#2-变量&#39;result&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'result' was corrupted 运行时检查失败#2-变量&#39;numberchoices&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'numberchoices' was corrupted 运行时检查失败#2-变量&#39;ex&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'ex' was corrupted 运行时检查失败-变量周围的堆栈已损坏 - Run-Time Check Failure - Stack around variable was corrupted 运行时检查失败#2-变量&#39;s&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 's' was corrupted 运行时检查失败#2 - 变量&#39;B&#39;周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'B' was corrupted 运行时检查失败#2-变量周围的堆栈-已损坏 - Run-Time Check Failure #2 - Stack around the variable — was corrupted 运行时检查失败 #2 - 变量“newRow”周围的堆栈已损坏 - Run-Time Check Failure #2 - Stack around the variable 'newRow' was corrupted
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM