1
我試圖使用STL優先級隊列,定義爲:當運行下面的代碼C++ STL的優先級隊列中獲取bad_alloc的
template <typename T>
using min_queue = std::priority_queue<T, std::vector<T>, std::greater<T>>;
:
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;
}
我收到以下錯誤:
malloc: *** error for object 0x230000000: pointer being freed was not allocated
您能否提供一個編譯並導致此錯誤的最小示例? – werewindle
我不能,代碼庫相當長(除非你想克隆庫) –
看,錯誤可以在你的代碼中的任何地方。它提供的代碼片段幾乎沒有機會。在那裏沒有手動內存分配/釋放,所以這段代碼不應該導致雙重釋放錯誤。 – werewindle