* "Thorsten Ottosen said:
| >
| > Consider
| >
| > std::list<T*> l;
| > l.push_back( new T );
| > // ... delete pointers later in a loop
| >
| > this is not exception-safe since a list has no knowlegde about ownership of the stored pointers. l.push_back
| > can throw bad_alloc if it cannot allocate a node; in that case the stored pointer is never freed.
|
| Well that depends on the context (never say never! ;-)).
|
| But in other words what you meant was exception unsafe was not the procedure
| I outlined for deleting the objects, but some specific imagined procedure for
| putting the pointers into the list in the first place.
depends on where you execute your procedure.
No.
A lot tends to think
{
list<T*> l; l.push_back( new T );
....
for( iterator i = l.begin(); i != l.end(); ++i ) delete &*i;
}
is ok when its clearly not.
Again it's your imagined procedure for insertion that is unsafe, namely
code that does not properly handle an exception from push_back, and that
has nothing to do with what you answered "not exception safe" to.
The deletion code I sketched or hinted at would be safe.
The code above is not an implementation of that sketch. It's meaningless
Undefined Behavior. Remove the "&" and add clearing of the list after
the loop and the deletion part will be safe under the common assumptions.
Even though you wrap list<T*> in a new class with the for loop in
the destructor, you're still not safe.
That depends very much on the programmer... ;-)
Btw., on style: don't use lowercase ell as a name, because it's so easy to
confuse with the digit one, and do use curly braces around loop bodies.