jeudi 14 mai 2015

C++ STL priority queue getting bad_alloc

I'm trying to use a STL priority queue, defined as:

template <typename T>
using min_queue = std::priority_queue<T, std::vector<T>, std::greater<T>>;

When running the following code:

Label kNN(int k, const Matrix &trainingSet, const std::vector<Label> &trainingLabels, Matrix &evSet, int i1, const DistanceF &f) {
    Timer timer("kNN Timer");
    min_queue<std::pair<double, Label>> distances;

    for (int i = 0; i < trainingSet.rows(); ++i) {
        distances.push(std::pair<double, Label>(f(trainingSet, i, evSet, i1), trainingLabels[i]));
    }

    int i = 0;
    int labels[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};

    while (!distances.empty() && i < k) {
        labels[(int)distances.top().second]++;
        distances.pop();
        ++i;
    }



    int maximum = 0;

    for (int j = 0; j < 10; ++j) {
        if (labels[j] > maximum) {
            maximum = j;
        }
    }

    return (double)maximum;
}

I get the following error:

malloc: *** error for object 0x230000000: pointer being freed was not allocated

I'm not sure how to approach this problem, any help will be appreciated :).

Thanks, Julian.

Aucun commentaire:

Enregistrer un commentaire