0

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,

Eric Postpischil
  • 195,579
  • 13
  • 168
  • 312
Arun Kp
  • 392
  • 2
  • 13
  • Why are you dynamically allocating with `malloc` _at all_ in C++? – Chris Jul 09 '22 at 02:09
  • 4
    malloc goes with free, new with delete, new[] with delete[]. It is not valid to mix & match between them. – Avi Berger Jul 09 '22 at 02:09
  • Because this is C++ and not C, you also do not need to specify `struct sdata` but can rather just use `sdata`. – Chris Jul 09 '22 at 02:11
  • Do not tag C for C++ questions. – Eric Postpischil Jul 09 '22 at 02:11
  • 1
    If you release ownership of memory & then try to access it - that's undefined behavior. Don't do it. Anything can happen. The big scary new owner of the memory could show up and attempt to shoot you in the foot. – Avi Berger Jul 09 '22 at 02:12
  • @Chris to be honest , I am C guy working mainly on linux kernel changes for my thesis work, I switched to c++ to use a simulator tool, this code piece is what i did there, I am still in the hand of C, sorry about that – Arun Kp Jul 09 '22 at 02:15
  • And to your actual question, NO. unordered_map has no way of knowing how the memory was obtained or whose responsibility its management is. Not its job. For that look into smart pointers. – Avi Berger Jul 09 '22 at 02:16
  • @EricPostpischil : sorry about that, I am being a C person, I thought C experts also could help me out here, – Arun Kp Jul 09 '22 at 02:16
  • @AviBerger Thanks for the comment, I will look into smart pointers – Arun Kp Jul 09 '22 at 02:18
  • Best to treat C and C++ as they are: two distinct programming languages. – Chris Jul 09 '22 at 02:18
  • Anything and everything could happen as a result of this. Outcomes of undefined behaviour include you get what you expect, the program aborts, or maybe the computer spontaneously generates a Rise of Skywalker that doesn't suck. The possibilities are limitless. – user4581301 Jul 09 '22 at 03:25

0 Answers0