jeudi 19 mai 2022

How These 2 Almost Same code are different?

This Code is giving error Heap-Use-After-Free

for (auto it = mp.begin();it != mp.end();it++)
{
    if(it->second+ttl<=currentTime)
    {
        mp.erase(it);
    }
}

But this code

for (auto it = mp.begin();it != mp.end();)
{
    if(it->second+ttl<=currentTime)
    {
        mp.erase(it++);
    }
    else
    {
        ++it;
    }
}

Does Not give ans error. Can you please explain why? Both look basically the same to me. Please Help

Aucun commentaire:

Enregistrer un commentaire