samedi 5 novembre 2016

C++ Why does random generator destroy other memories?

My code kept giving me the error in the following picture

the line below "1 18"

Just to give a brief explanation,

the entries are elements in a global string vector 'featureName', and at my first iteration

"0 18"

you can see all elements printed fine.

but at the second iteration

"1 18" (18 represents size, so the size seems to be ok)

gibberish starts to come out and expresses malloc error.

It isn't display problem, as i was doing cout to find out why operation on the 'featureName' kept failing.

std::default_random_engine gen;
for (int i=0;i < numOfAugmentation;i++) {
    std::cout << i << " " << featureName.size() << std::endl;

    //
    for (string i : featureName) {
        std::cout << i << std::endl;
    }
    //


    double * newEntry = new double[numOfFeatures];
    newEntry[0] = (int)round(rand() / RAND_MAX);
    for (int j =1;j < featureName.size();j++) {
        if (augTable.find(featureName.at(j)) == augTable.end()) {
            std::cout << featureName.at(j)
                      << " not found at augmentation dataset"
                      << std::endl;
            return;
        }
        double * element = augTable[featureName.at(j)];
        int genderIndex = newEntry[0] == 0 ? 0 : 3;
        double center = element[genderIndex];
        double deviation = element[genderIndex+1]/divideSigma;
        std::normal_distribution<double> distri(center,deviation);
        newEntry[j] = distri(gen);
    }
    output.push_back("AUGMENTED");
    features.push_back(newEntry);

}

The code is above,

And as you can see, I'm not doing any operation to the 'featureName'!

I eventually tracked the problem and it was the two lines below

std::normal_distribution<double> distri(center,deviation);
newEntry[j] = distri(gen);

at the bottom of my code.

After i erased those two lines, for loop worked flawlessly till the end.

What is wrong with those two lines?

and How does that two line influence completely unrelated memory?




Aucun commentaire:

Enregistrer un commentaire