Vote count:
0
I'm not at ease with the way memory's managed in the following example :
void X::foo(void) {
// test1 :
for (auto i = this->vec.begin(); i != this->vec.end(); ++i) {
if ( ... ) {
return;
}
}
// test2
...
// test3
...
}
Even if there's no memory allocation for iterators, I would like to be sure that there's no problem quitting such abruptly the loop. Is it safer to break the loop, check the result of the test and then quit the function without passing throuh test2 and test3 ?
asked 1 min ago
1 Answer
Vote count:
0
This is safe as long as you return any possibly allocated resources back to the system. If you are in c++ and utilizing structures who properly implement the ideas of RAII, i.e., shared_ptr etc. Then there will be no problem.
Return essentially "immediately" leaves the function call. However, before it does it cleans up any temporary objects created first.
answered 10 secs ago
Aucun commentaire:
Enregistrer un commentaire