[英]Heap use after free err
在 Leetcode 出現免費錯誤后獲得堆使用,似乎不明白根本原因。你們能幫我嗎?
大多數情況下,所有內容都在堆棧上聲明。 我唯一的懷疑是我在堆棧 multiset temp 上創建的淺拷貝,但您不能釋放堆棧上沒有在堆上創建的任何內容?
class Solution {
public:
void earn_points(multiset<int> points,int currpoint, int& max){
set<int> unique;
if(points.size() == 0){
cout <<"finalscore="<< currpoint << " "<<endl;
if(currpoint > max){
max = currpoint;
}
}
multiset<int> temp = points;
for(auto it=points.begin(); it != points.end(); ++it){
int num = *it;
if(unique.find(num) != unique.end()){
continue;
}
unique.insert(num);
int delete_num1 = num + 1;
int delete_num2 = num - 1;
points.erase(it);
if(points.find(delete_num1) != points.end())
points.erase(delete_num1);
if(points.find(delete_num2) != points.end())
points.erase(delete_num2);
cout << num <<" ";
for(auto i : points){
cout << i <<" ";
}
cout << endl;
earn_points(points,currpoint + num,max);
points = temp;
}
}
int deleteAndEarn(vector<int>& nums) {
multiset<int> points(nums.begin(),nums.end());
int max = INT32_MIN;
earn_points(points,0,max);
return max;
}
};
您的問題很可能在這里:
points.erase(it);
if(points.find(delete_num1) != points.end())
points.erase(delete_num1);
if(points.find(delete_num2) != points.end())
points.erase(delete_num2);
當您從多重集中擦除內容時,它會使迭代器無效,因此當您點擊迭代器引用您在 for... 循環中擦除的內容時,您正在引用不再存在的內容。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.