Ошибка сегментации с векторами - PullRequest
0 голосов
/ 16 октября 2018

Программа, которую я делаю, не завершает цикл for и выдает ошибку ошибки сегментации до завершения цикла.Могу ли я спросить его, потому что цикл слишком мал?

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

Я попытался изменить имена переменных, чтобы они не сталкивались, но ошибки все те же.Я также ценю любые советы по улучшению моего кода.

1 Ответ

0 голосов
/ 16 октября 2018

В следующем коде:

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

У вас нет проверки границ значения x после его увеличения внутри цикла while, это может привести к доступу к элементам за пределамиpassList.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...