Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Juha Nieminen <nospam@thanks.invalid> |
|---|---|
| Newsgroups | comp.std.c++ |
| Subject | Re: Sequence container capacity after calling clear() |
| Date | 2013-03-24 23:36 -0700 |
| Organization | Netfront http://www.netfront.net/ |
| Message-ID | <kimj8v$10jd$1@adenine.netfront.net> (permalink) |
| References | <dZydnUi3X6YIOtHMnZ2dnUVZ8iCdnZ2d@giganews.com> |
In comp.lang.c++ Leigh Johnston <leigh@i42.co.uk> wrote: > Currently the behaviour is unspecified and I know of at least one > implementation that deallocates on vector<T>::clear(). One would think that such an implementation would get so many complaints that they would fix that. I don't think it's extremely rare for people to use std::vector as a buffer to do some data manipulation. If said code is called very frequently, then allocating a new array every single time would have a really big impact on performance, so it's better to reuse the one and same std::vector instance each time (by either making it 'static' if it's inside the function, a member function of the class, if this is a class, or a compilation-unit-local object.) If this is so, then usually you want to clear the vector before doing anything with it, so that it doesn't contain anything from the previous run (assuming that you don't know in advance how many elements there will be in the vector.) If the vector implementation works as usual, this is a fast operation because a 'clear()' will simply destroy the objects and set the size to zero, but not actually free the memory. Thus it will work as a static memory buffer. However, if 'clear()' frees the memory, that means that there will be a deallocation and an allocation every single time that the function is called, having a big impact in performance. The only way to make sure that this doesn't happen is to bypass std::vector and make your own class which manages a dynamic array. The value of std::vector as a useful tool has thus been diminished. --- news://freenews.netfront.net/ - complaints: news@netfront.net --- -- [ comp.std.c++ is moderated. To submit articles, try posting with your ] [ newsreader. If that fails, use mailto:std-cpp-submit@vandevoorde.com ] [ --- Please see the FAQ before posting. --- ] [ FAQ: http://www.comeaucomputing.com/csc/faq.html ]
Back to comp.std.c++ | Previous | Next — Previous in thread | Next in thread | Find similar
Sequence container capacity after calling clear() Leigh Johnston <leigh@i42.co.uk> - 2013-03-23 16:27 -0600
Re: Sequence container capacity after calling clear() Daniel Krügler <daniel.kruegler@googlemail.com> - 2013-03-23 22:05 -0700
Re: Sequence container capacity after calling clear() James Kanze <james.kanze@googlemail.com> - 2013-03-27 13:28 -0600
Re: Sequence container capacity after calling clear() Jason McKesson <jmckesson@googlemail.com> - 2013-03-23 22:05 -0700
Re: Sequence container capacity after calling clear() Juha Nieminen <nospam@thanks.invalid> - 2013-03-24 23:36 -0700
Re: Sequence container capacity after calling clear() Mathias Gaunard <loufoque@googlemail.com> - 2013-03-25 10:49 -0600
Re: Sequence container capacity after calling clear() Leigh Johnston <leigh@i42.co.uk> - 2013-03-26 08:55 -0600
csiph-web