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


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


Relevant Pages

  • [-mm PATCH 10/10] Memory controller add documentation
    ... Differentiate between RSS and Page Cache - Paul Menage ... The infrastructure allows easy addition of other types of memory to control ... +c. Provides *zero overhead* for non memory controller users ... a container on hitting a limit, ...
    (Linux-Kernel)
  • Re: [ckrm-tech] [patch00/05]: Containers(V2)- Introduction
    ... I finally looked into your memory controller patches. ... Current cpuset feature in Linux kernel ... We use the term container to indicate a structure against which we track ... changes in kernel are minimized so as this support can ...
    (Linux-Kernel)
  • Re: Block based virtual harddisk library
    ... > to the virtual disk is block based. ... > actual file (container) on disk. ... > and trie structures contain extra data for each entry, memory can ... So on each new block allocation in the container ...
    (comp.programming)
  • Re: BC: resource beancounters (v2)
    ... The task of limiting a container to 4.5GB of memory bottles down to the ... This is a fall-back method in UBC, for the case "privvmpages" limits ... in my experiments such modified scanner was unable to regulate ...
    (Linux-Kernel)
  • Re: [ckrm-tech] RFC: Memory Controller
    ... I have a port of Rohit's memory controller to run over my generic containers. ... container which touches the page or should it be charged equally among ... If you charge the first user, then it will appear that the first job ... RSS control ...
    (Linux-Kernel)