unordered_map 迭代器迭代到空指针,无视可能性
unordered_map iterator iterates to null pointer, defying possibility
我将一堆虚拟对象存储在 unordered_map 中,然后遍历以删除它们。不知何故,它出现了一个键值对,其中值是一个空指针,尽管我很确定这是不可能的。
我制作的每个对象都只是我随机构建的一个非常大的字符串的一部分。我确保将字符串的至少 5 个字符交给该对象的构造函数。
代码如下:
删除函数
bool delete_objects(){
Serial.println("deleting objects in 'store'");
for(auto iter = store.begin(); iter != store.end(); iter++)
{
Serial.println("\nDeleting the following element: \n");
Serial.print("Key: ");
Serial.println(iter->first);
delay(1000);
Serial.print("Value: ");
Serial.println(iter->second->ShowMeWhatYouGot());
delay(1000);
delete iter->second;
iter->second = nullptr;
store.erase(iter);
}
if(store.empty())
return true;
else
return false;
}
分配对象的块(在此之前调用delete_objects())
unsigned int index = 0;
unsigned int key = 0;
while(index < buff_string.length()){
int amount = (rand() % 51) + 5;
DummyClass* dc = new DummyClass(buff_string.substr(index, amount));
store[key] = dc;
index += amount;
key++;
Serial.println("Created new DummyClass object");
Serial.print("Free Memory: ");
Serial.println(System.freeMemory());
}
class定义
using namespace std;
class DummyClass {
private:
char* _container;
public:
DummyClass(){
}
DummyClass(string input){
_container = new char[input.length()];
strcpy(_container, input.c_str());
}
~DummyClass(){
delete _container;
_container = nullptr;
}
char* ShowMeWhatYouGot(){
return _container;
}
};
串口输出,当键为0
时,它是SOS(崩溃,对于粒子芯片)
Key: 194
Value: q->@B{s?tX/miN3K?[~2[mvTOSKPE34#5[@q8*d8BZwH`
Deleting the following element:
Key: 193
Value: 9Hb^7-J(#z3H#0uqg
Deleting the following element:
Key: 192
Value: V$xZ>C$u{MjxzxL?{KQqvQp*MN3~Ce&yZbmop1xlkJM)jGja~P{mY
Deleting the following element:
Key: 191
Value: yo*CVzE~2
Deleting the following element:
Key: 190
Value: [&PQakTV3{^Aq?(ffV/*24xaXej]~T1^SfWiM3ATpk#{CoQ
Deleting the following element:
Key: 0
Value
来自http://en.cppreference.com/w/cpp/container/unordered_map/erase
References and iterators to the erased elements are invalidated. Other iterators and references are not invalidated.
之后
store.erase(iter);
iter
无效。之后的表达式 ++iter
是未定义行为的原因。
解决问题的方法之一:
for(auto iter = store.begin(); iter != store.end(); /* iter++ */)
// Not needed
{
// code
// ...
iter = store.erase(iter);
}
另一种解决问题的方法:
for(auto iter = store.begin(); iter != store.end(); iter++)
{
// code
// ...
// Dont' call erase.
// store.erase(iter);
}
// Clear the map.
store.clear();
我将一堆虚拟对象存储在 unordered_map 中,然后遍历以删除它们。不知何故,它出现了一个键值对,其中值是一个空指针,尽管我很确定这是不可能的。
我制作的每个对象都只是我随机构建的一个非常大的字符串的一部分。我确保将字符串的至少 5 个字符交给该对象的构造函数。
代码如下:
删除函数
bool delete_objects(){
Serial.println("deleting objects in 'store'");
for(auto iter = store.begin(); iter != store.end(); iter++)
{
Serial.println("\nDeleting the following element: \n");
Serial.print("Key: ");
Serial.println(iter->first);
delay(1000);
Serial.print("Value: ");
Serial.println(iter->second->ShowMeWhatYouGot());
delay(1000);
delete iter->second;
iter->second = nullptr;
store.erase(iter);
}
if(store.empty())
return true;
else
return false;
}
分配对象的块(在此之前调用delete_objects())
unsigned int index = 0;
unsigned int key = 0;
while(index < buff_string.length()){
int amount = (rand() % 51) + 5;
DummyClass* dc = new DummyClass(buff_string.substr(index, amount));
store[key] = dc;
index += amount;
key++;
Serial.println("Created new DummyClass object");
Serial.print("Free Memory: ");
Serial.println(System.freeMemory());
}
class定义
using namespace std;
class DummyClass {
private:
char* _container;
public:
DummyClass(){
}
DummyClass(string input){
_container = new char[input.length()];
strcpy(_container, input.c_str());
}
~DummyClass(){
delete _container;
_container = nullptr;
}
char* ShowMeWhatYouGot(){
return _container;
}
};
串口输出,当键为0
时,它是SOS(崩溃,对于粒子芯片)Key: 194 Value: q->@B{s?tX/miN3K?[~2[mvTOSKPE34#5[@q8*d8BZwH`
Deleting the following element:
Key: 193 Value: 9Hb^7-J(#z3H#0uqg
Deleting the following element:
Key: 192 Value: V$xZ>C$u{MjxzxL?{KQqvQp*MN3~Ce&yZbmop1xlkJM)jGja~P{mY
Deleting the following element:
Key: 191 Value: yo*CVzE~2
Deleting the following element:
Key: 190 Value: [&PQakTV3{^Aq?(ffV/*24xaXej]~T1^SfWiM3ATpk#{CoQ
Deleting the following element:
Key: 0 Value
来自http://en.cppreference.com/w/cpp/container/unordered_map/erase
References and iterators to the erased elements are invalidated. Other iterators and references are not invalidated.
之后
store.erase(iter);
iter
无效。之后的表达式 ++iter
是未定义行为的原因。
解决问题的方法之一:
for(auto iter = store.begin(); iter != store.end(); /* iter++ */)
// Not needed
{
// code
// ...
iter = store.erase(iter);
}
另一种解决问题的方法:
for(auto iter = store.begin(); iter != store.end(); iter++)
{
// code
// ...
// Dont' call erase.
// store.erase(iter);
}
// Clear the map.
store.clear();