繁体   English   中英

带向量的分段错误

[英]Segmentation fault w/ vectors

我正在做的程序没有完成 for 循环,它在循环完成之前向我抛出 Segmentation fault 错误。 请问是不是因为循环太小了?

for(vector<string>::iterator it = passList.begin(); it!= passList.end();it++)
    {
        for (size_t x = 0; x < passList.size()-1; ++x)
        {
            while(passList[x] == used)
            {
                x++;
            }
                firstPW = passList[x];
                cout << firstPW << endl;
                //passList.erase(remove(passList.begin(), passList.end(), firstPW), passList.end());
                replace (passList.begin(), passList.end(), firstPW, used);
                chainHead.push_back(firstPW);
                strmd5 = MD5Hash(firstPW);
                //chainTail.push_back(strmd5);
                //cout << strmd5 << endl;

                strmd5.erase(remove_if(strmd5.begin(), strmd5.end(), isnonnum), strmd5.end());
                cout << strmd5 << endl;

                numOfDigits = strmd5.length();
                //cout << numOfDigits << endl;

                digitsToMinus = numOfDigits - to_string(count).length();
                strmd5.erase(strmd5.length()-digitsToMinus);
                cout << strmd5 << endl;

                intmd5 = stoi(strmd5);
                //cout << intmd5 << endl;
                intmd5 = intmd5 + 1;
                index = intmd5 % count;
                cout << index << endl;

                chainHead2.push_back(chainHead[0]);
        }

        for(int i=0; i<3; i++)
        {
            firstPW = passList2[index];
            //cout << firstPW << endl;
            //passList.erase(remove(passList.begin(), passList.end(), firstPW), passList.end());
            replace (passList.begin(), passList.end(), firstPW, used);
            //chainHead.push_back(firstPW);
            strmd5 = MD5Hash(firstPW);
            chainTail.push_back(strmd5);
            //cout << strmd5 << endl;

            strmd5.erase(remove_if(strmd5.begin(), strmd5.end(), isnonnum), strmd5.end());
            //cout << strmd5 << endl;

            numOfDigits = strmd5.length();
            //cout << numOfDigits << endl;

            digitsToMinus = numOfDigits - to_string(count).length();
            strmd5.erase(strmd5.length()-digitsToMinus);
            //cout << strmd5 << endl;

            intmd5 = stoi(strmd5);
            //cout << intmd5 << endl;
            //intmd5 = intmd5 + 1;
            index = intmd5 % count;
            //cout << index << endl;
            if(i = 3)
            {
                temp = chainTail.back();
                chainTail2.push_back(temp);
            } 
        }
        chainHead.clear();
        chainTail.clear();
        /*for(int a=0; a<chainHead2.size(); a++)
        {
            cout << chainHead2[a] << endl;
        }
        for(int b=0; b<chainTail2.size(); b++)
        {
            cout << chainTail2[b] << endl;
        }*/
        PWchain = chainHead2[0];
        HASHchain = chainTail2[0];
        chainHead2.clear();
        chainTail2.clear();

        Rainbowtxt.open("Rainbow.txt",ios::app);

        Rainbowtxt << PWchain << " " << HASHchain << endl;
        Rainbowtxt.close();
    }

我尝试更改变量名称,以免它们发生冲突,但错误仍然相同。 我也感谢任何改进我的代码的建议。

在以下代码中:

for (size_t x = 0; x < passList.size()-1; ++x)
{
    while(passList[x] == used)
    {
        x++;
    }

在 while 循环内增加x的值后,您没有对x的值进行边界检查,这可能会导致访问passList边界之外的项目。

暂无
暂无

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

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