Re: STL Question: Safe to use elements after erasing them from the collection?
From: Ivan Vecerina (please_use_web_form_at_ivan.vecerina.com)
Date: 12/05/03
- Next message: Thomas Matthews: "Re: overloading ! operator for factorial"
- Previous message: Brad Marts: "Re: How to avoid ambiguous constructors?"
- In reply to: Generic Usenet Account: "STL Question: Safe to use elements after erasing them from the collection?"
- Next in thread: Gary Labowitz: "Re: STL Question: Safe to use elements after erasing them from the collection?"
- Reply: Gary Labowitz: "Re: STL Question: Safe to use elements after erasing them from the collection?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Fri, 5 Dec 2003 17:53:42 +0100
"Generic Usenet Account" <usenet@sta.samsung.com> wrote in message
news:90e5135.0312050833.7c1ea34@posting.google.com...
| To settle the dispute regarding what happens when an "erase" method is
| invoked on an STL container (i.e. whether the element is merely
| removed from the container or whether it also gets deleted in the
| process), I looked up the STL code.
| Erase certainly does not delete the memory associated with the element.
Most containers (excluding std::vector) may release the memory associated
with items that have been removed using the erase member function.
However, many implementations will keep a cache of previously
allocated nodes, to speed-up later addition of new elements.
| However, it appears that the
| destructor on the element is invoked. I wonder why it has to be this
| way. In my opinion, this renders the element extremely risky to use
| after it has been "erased" from the collection.
According to the standard, the destructor of the erased elements
*must* be called. It is not uncommon for code to rely on the
deterministic destruction of the items being erased.
Attempting to access items that have been erased leads to undefined
behavior. Even if they were not destroyed immediately, hopefully
the memory they occupy would be recycled at some point. So it
wouldn't be safe to access the erased items either way.
| void destroy_node(link_type p) {
| destroy(&p->data);
| put_node(p);
| }
I would guess that 'put_node()' is used by this implementation
to store the node into a list of nodes that can be recycled.
| stl_construct.h
| ...
| template <class T>
| inline void destroy(T* pointer) {
| pointer->~T(); // KPB Note:- we are explicitly
| // invoking the destructor,
| // but we are not performing
| // the delete operation
| }
The allocated memory includes the item itself and the
prev/next links of the list nodes. It may also be part
of an array of nodes that have been allocated as a chunk,
to optimize performance.
The memory storage itself will typically be freed or
recycled at a later point in time...
I hope this helps,
Ivan
-- http://ivan.vecerina.com/contact/?subject=NG_POST <- e-mail contact form Brainbench MVP for C++ <> http://www.brainbench.com
- Next message: Thomas Matthews: "Re: overloading ! operator for factorial"
- Previous message: Brad Marts: "Re: How to avoid ambiguous constructors?"
- In reply to: Generic Usenet Account: "STL Question: Safe to use elements after erasing them from the collection?"
- Next in thread: Gary Labowitz: "Re: STL Question: Safe to use elements after erasing them from the collection?"
- Reply: Gary Labowitz: "Re: STL Question: Safe to use elements after erasing them from the collection?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|