Topic: deallocate memory for clear() or erase(begin(), end())?


Author: spam-this@bogus.com ("JT")
Date: Wed, 20 Jul 2005 15:24:28 GMT
Raw View
1) What does the Standard say an implementation should do about deallocating
memory when container.clear() and container.erase(begin(), end()) are used?
I having trouble finding any specifics on this in the Standard, is it left
up to the implementation?

2) Also, it appears that container.clear() is defined as erase(begin(),
end()), in Tables 67 (Sequence Containers) and Table 69 (Associative
Containers).  This implies to me that an implementation should perform the
same memory deallocation scheme for clear() as it would for erase(begin(),
end()).  Would you agree?

Thanks,
JT



---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: caj@cs.york.ac.uk (chris jefferson)
Date: Wed, 20 Jul 2005 22:48:28 GMT
Raw View
JT wrote:
> 1) What does the Standard say an implementation should do about deallocating
> memory when container.clear() and container.erase(begin(), end()) are used?
> I having trouble finding any specifics on this in the Standard, is it left
> up to the implementation?
>
> 2) Also, it appears that container.clear() is defined as erase(begin(),
> end()), in Tables 67 (Sequence Containers) and Table 69 (Associative
> Containers).  This implies to me that an implementation should perform the
> same memory deallocation scheme for clear() as it would for erase(begin(),
> end()).  Would you agree?
>

Let me answer your questions backwards :)

I agree with 2, that clear() is defined purely as erase(begin(), end()).

And then in 1, I would expect that erase(begin(),end()) wouldn't do
anything that much different to erase(i,j), for any other i,j. As the
capacity of (for example) vector is observable, as the effects of
vector::erase don't say that it can change the capacity, then it can't.

Chris

---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]





Author: sjhoweATdialDOTpipexDOTcom@eu.uu.net ("Stephen Howe")
Date: Wed, 20 Jul 2005 23:47:07 GMT
Raw View
> 1) What does the Standard say an implementation should do about
> deallocating
> memory when container.clear() and container.erase(begin(), end()) are
> used?

It does not. It is unclear on this point. I wish it was clear (no pun
intended).

> I having trouble finding any specifics on this in the Standard, is it left
> up to the implementation?

Yup

> 2) Also, it appears that container.clear() is defined as erase(begin(),
> end()), in Tables 67 (Sequence Containers) and Table 69 (Associative
> Containers).  This implies to me that an implementation should perform the
> same memory deallocation scheme for clear() as it would for erase(begin(),
> end()).  Would you agree?

No. All that matters is there is no elements left.
How that is implemented is another question.
For example, many vendors for set/map clear() recursively delete the
elements without bothering to preserve nodal pointer invariants and then
adjust the booking to indicate it is empty. That is faster than erase() for
two iterators as erase may be called for just some of the elements.

Stephen Howe


---
[ comp.std.c++ is moderated.  To submit articles, try just posting with ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu    ]
[              --- Please see the FAQ before posting. ---               ]
[ FAQ: http://www.jamesd.demon.co.uk/csc/faq.html                       ]