Home > Software design >  Reference to a vector element become invalid after n iterations
Reference to a vector element become invalid after n iterations

Time:07-10

Program stop occur in this line

guess = secret;

From that, I guess that reference is broken, because if I change reference to simple value

const string secret = word_list[idx_word];

the program finishes correctly. So, my question is why this happen. The word_list is not changed/resided in loop.

Erorr occur on 392 iteration.

#include <QCoreApplication>
#include <QFile>
#include <QDir>
#include <QVector>
#include <cmath>
#include <algorithm>
#include <iostream>
#include <map>
#include <set>
#include <thread>
#include <mutex>
#include <iomanip>
#include <string>

using namespace std;

bool debug = true;

const int COUNT = 5;
string MASK_FULL_MATCH(COUNT, 'o');

const string getMask(const string& word, const string& answer) {
    if (word.size() != COUNT || answer.size() != COUNT) {
        cout << word.size() << " " << answer.size() << endl;
    }

    char mask[5];
    bool visited[5];
    for (int i = 0; i < COUNT; i  ) {
        mask[i] = 'x';
        visited[i] = false;
    }

    // find correct letters
    for(int i = 0; i < COUNT; i  ){
        if (word[i] == answer[i]){
            mask[i] = 'o';
            visited[i] = true;
        }
    }

    // find present letters
    for (int i = 0; i < COUNT; i  ){
        if (mask[i] != 'o'){
            for (int j = 0; j < COUNT; j  ) {
                if (answer[j] == word[i] && !visited[j]) {
                    mask[i] = '-';
                    visited[j] = true;
                    break;
                }
            }
        }
    }

    return string(mask, COUNT);
}

int main(int argc, char *argv[])
{
    QString pathToFile = QString("C:/Users/Ivan/Desktop/w_assets/w")   QString::number(COUNT)   QString("_entropy.txt");

    QFile file(pathToFile);
    if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
        return -2;

    QTextStream in(&file);
    QVector<string> word_list;
    while (!in.atEnd()) {
      QString line = in.readLine();
      word_list.append(line.split(QChar(' '))[0].toStdString());
    }
    file.close();

    for (int idx_word = 0; idx_word < word_list.size(); idx_word  ) {
        const string &secret = word_list[idx_word];
        cout << secret << '\t';
    }

    int total = 0;
    for (int idx_word = 0; idx_word < word_list.size(); idx_word  ) {
        const string &secret = word_list[idx_word];
        cout << "NEW SECRET " << secret << endl;
        QVector<string> possible_answers = word_list;
        for (int row = 0; row < 6; row  ) {
            string guess;
            if (row == 0) {
                guess = word_list[0];
            }
            else {
                cout << "before broken secret\n";
                guess = secret;
                cout << "after broken secret\n";
                cout << "row " << row << "; GUESS " << guess << endl;
            }

            debug = true;
            string mask = getMask(guess, secret);
            debug = false;
            cout << "MASK: " << mask << endl;
            if (mask == MASK_FULL_MATCH) {
                break;
            }

            QVector<string> new_possible_answers;
            for (const auto& pa : possible_answers) {
                if (getMask(guess, pa) == mask) {
                    new_possible_answers.append(pa);
                }
            }

            possible_answers = new_possible_answers;
            cout << "NEW POSSIBLE WORDS SIZE " << possible_answers.size() << endl;
        }

    }

    return 0;
}

CodePudding user response:

word_list[0]; - this is a non-const operation in a QVector (see documentation, there is even a note about the possible detach) and since the reference count of your word_list is two due to the copy to possible_answers some lines above, the container has to do a detach and therefore your reference goes out of scope. If you work with references on Qt containers you have to make sure to either have a reference count of 1 or only use const-access to the container (e.g. by creating a const ref to the container -> const auto &const_word_list = word_list; guess = const_word_list [0])

  •  Tags:  
  • c qt
  • Related