Does deleting an unordered_map element also release the malloc memory assigned to that unordered_map element. I was under the impression it does not. Please see the difference in two scenarios.
struct sdata{
unsigned pid;
unsigned long start;
unsigned long end;
};
int main(){
std::unordered_map<unsigned,struct sdata*> ht;
ht[0] = (struct sdata*)malloc(sizeof(struct sdata));
ht[1] = (struct sdata*)malloc(sizeof(struct sdata));
ht[2] = (struct sdata*)malloc(sizeof(struct sdata));
for (int i=0; i< 3; i++){
ht[i]->start = i;
ht[i]->end = i+1;
}
struct sdata *temp = ht[1];
delete ht[1];
//temp->start = 100;
std::cout<<"start: "<<temp->start<<std::endl;
std::cout<<"end: "<<temp->end<<std::endl;
return 0;
}
After delete ht[1], temp->start printed a garbage value, and obvious removing the delete values printed are correct.
And surprisingly, after delete, if I access the temp->start by assigning 100 to it (commented line in code), the value printed by temp->start is correct.
Can anyone please help me to understand this behavior of unordered_map Thanks in advance,