Topic: Is clear() allowed to deallocate?
Author: stefan--nospam--@slapeta.com (Stefan Slapeta)
Date: Thu, 9 Dec 2004 21:04:16 GMT Raw View
The notes for clear() in 23.1.1/Table 67 read as follows:
erase(begin(), end())
post: size() == 0
Does this allow deallocation? The Dinkumware implementation (VC 7.1)
does so (capacity() == 0 after clear()) and IMO that's a really bad
practice.
Stefan
---
[ 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: pjp@dinkumware.com ("P.J. Plauger")
Date: Fri, 10 Dec 2004 05:24:55 GMT Raw View
"Stefan Slapeta" <stefan--nospam--@slapeta.com> wrote in message
news:cp99pv$cjq$1@paperboy.Austria.EU.net...
> The notes for clear() in 23.1.1/Table 67 read as follows:
>
> erase(begin(), end())
> post: size() == 0
>
> Does this allow deallocation? The Dinkumware implementation (VC 7.1) does
> so (capacity() == 0 after clear()) and IMO that's a really bad practice.
The C++ Standard wasn't always so clear (as it were) on this topic.
Now that it is, we no longer reduce capacity. (See, e.g. the V8.0
beta.)
P.J. Plauger
Dinkumware, Ltd.
http://www.dinkumware.com
---
[ 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: "msalters" <Michiel.Salters@logicacmg.com>
Date: Fri, 10 Dec 2004 12:32:45 CST Raw View
Stefan Slapeta wrote:
> The notes for clear() in 23.1.1/Table 67 read as follows:
>
> erase(begin(), end())
> post: size() == 0
>
> Does this allow deallocation? The Dinkumware implementation (VC 7.1)
> does so (capacity() == 0 after clear()) and IMO that's a really bad
> practice.
( NB. I did see PJPs response, but this isn't a reply to that )
No. Even in C++98, that erase should not invalidate
existing pointers poiting to begin(). All other iterators
would be invalidated. If deallocation happens, begin() would
probably change, which invalidates the precondition.
An iterator implemented as a vector&+offset would of course
remain valid, but that's not enough.
HTH,
Michiel Salters
---
[ 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: brangdon@cix.co.uk (Dave Harris)
Date: Mon, 13 Dec 2004 02:38:44 CST Raw View
Michiel.Salters@logicacmg.com (msalters) wrote (abridged):
> No. Even in C++98, that erase should not invalidate
> existing pointers poiting to begin(). All other iterators
> would be invalidated. If deallocation happens, begin() would
> probably change, which invalidates the precondition.
More usefully, iterators and pointers should remain valid as the array
grows, until it reaches its old capacity. Eg:
std::vector<int> v( 100 );
assert( v.capacity() >= 100 );
v.erase( v.begin(), v.end() );
v.push_back( 10 );
v.push_back( 20 );
int *p = &v[1];
v.push_back( 30 );
++*p;
assert( v.capacity() >= 100 && *p == 21 );
The final push_back() must not reallocate. With VC++7.1, this works, but
it fails if we use clear() instead of erase(). (I don't believe the
standard has ever permitted them to behave differently.)
-- Dave Harris, Nottingham, UK
---
[ 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 ]