Skip to content

Maybe a bug #258

Description

@dekonunes

My english is not so well, but i'll try show what happening.

I'm creating a genetic algorithm and when create a string with 16 or more bits the lib have a bug, will explain better.

First i'm reading like this:

void IndividuoBinario::openJson() {        
        using json = nlohmann::json;
        ifstream texto("entrada.json");
        if (!texto)
            cout << "dont open" << endl;
        stringstream buffer;
        buffer << texto.rdbuf();
        this->entrada = json::parse(buffer.str());
        texto.close();
}

I have a class IndividuoBinario with this constructor:

IndividuoBinario::IndividuoBinario() {
    static mt19937 mt(time(NULL));
    static uniform_int_distribution<int> bit(0, 1);
    string aux;
    openJson();
    pair<int, int> auxGenes;
    for (int count = 0; count < this->entrada["qtdVariaveis"]; ++count) {
        auxGenes.first = this->entrada["variaveis"][count]["min"];
        auxGenes.second = this->entrada["variaveis"][count]["max"];
        this->genes.push_back(auxGenes);
    }
    for (int count = 0; count < this->genes.size(); ++count) {
        this->quantidadeBitsCadaGene.push_back(getNumeroBits(this->entrada["variaveis"][count]["max"],
                this->entrada["variaveis"][count]["min"],
                this->entrada["variaveis"][count]["precisao"]));
    }

    for (int var = 0; var < this->genes.size(); ++var) {
        for (int i = 0; i < this->quantidadeBitsCadaGene.at(var); i++) {
            aux = static_cast<ostringstream*>(&(ostringstream() << bit(mt)))->str();
            this->cromossomo = this->cromossomo + aux;
        }
    }
    this->probMutacao = this->entrada["chanceMutacao"];
}

Working fine, BUT when i make a pair this bug happen:

pair<IndividuoBinario, IndividuoBinario> newIndividuosCrossover; 

using printf to find the error (error is Segmentation fault) i found in this line (of method openJSON):

cout << "here 1" << endl; //show
ifstream texto("entrada.json");
cout << "here 2" << endl; // dont show (Segmentation fault)

when run:

.....
here1
here2
here1
here2
line before pair
here1
Segmentation Fault (core dumped)

but more weird its because this happen just when my string (this->cromossomo) is more then 16 chars and just when use pairs, if invoke normal class IndividuoBinario work fine, just using pair this happen and with more then 16 chars.

I used gdb and this happen

4134    malloc.c: Arquivo ou diretório não encontrado.

I dont know if this is because the lib, but maybe. I'm trying hard to solve this.

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions