samedi 25 avril 2020

push_back and exception safety

I understand that push_back can throw a bad_alloc exception and if there is no try catch block it is not true that the destructors are called. if any push_back throws and it's not inside a try catch block I'll have a memory leak? This means that if I don't want to have a leak I have to wrap every push_back inside a try catch?Is this behaviour too rare and expensive for a vector not to catch the exception, release the already allocated memory and rethrow the exception?

Aucun commentaire:

Enregistrer un commentaire