Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > Sequence container capacity after calling clear()

Reply
Thread Tools

Sequence container capacity after calling clear()

 
 
Leigh Johnston
Guest
Posts: n/a
 
      03-23-2013

Hi,

Can we please change the ISO C++ Standard so that explicitly states what
happens to a sequence container's capacity() after calling clear()?

Currently the behaviour is unspecified and I know of at least one
implementation that deallocates on vector<T>::clear().

If the behaviour remains unspecified then it is effectively impossible
to write portable code that uses clear() and you have to hope things
such as v.erase(v.begin(), v.end()) behave more consistently across
different implementations.

/Leigh


--
[ comp.std.c++ is moderated. To submit articles, try posting with your ]
[ newsreader. If that fails, use (E-Mail Removed) ]
[ --- Please see the FAQ before posting. --- ]
[ FAQ: http://www.comeaucomputing.com/csc/faq.html ]

 
Reply With Quote
 
 
 
 
Öö Tiib
Guest
Posts: n/a
 
      03-24-2013
On Saturday, 23 March 2013 23:30:02 UTC+2, Leigh Johnston wrote:
> Can we please change the ISO C++ Standard so that explicitly states what
> happens to a sequence container's capacity() after calling clear()?
>
> Currently the behaviour is unspecified and I know of at least one
> implementation that deallocates on vector<T>::clear().


OTOH both deque and vector have shrink_to_fit member.

> If the behaviour remains unspecified then it is effectively impossible
> to write portable code that uses clear() and you have to hope things
> such as v.erase(v.begin(), v.end()) behave more consistently across
> different implementations.


You are putting it too strongly, in most situations I do not care either
way. Given that we have both shrink_to_fit and reserve we can make it to
behave exatly same either way.
 
Reply With Quote
 
 
 
 
James Kanze
Guest
Posts: n/a
 
      03-24-2013
On Saturday, March 23, 2013 9:30:02 PM UTC, Leigh Johnston wrote:
> Can we please change the ISO C++ Standard so that explicitly states what
> happens to a sequence container's capacity() after calling clear()?


> Currently the behaviour is unspecified and I know of at least one
> implementation that deallocates on vector<T>::clear().


Has the specification in C++11 changed? In C++03, such an
implimentation would be illegal.

Could you tell me which implementation you are talking about.
I've not verified recently, but neither g++ nor VC++ behaved
like this in the past.

> If the behaviour remains unspecified then it is effectively
> impossible to write portable code that uses clear() and you
> have to hope things such as v.erase(v.begin(), v.end()) behave
> more consistently across different implementations.


There is nothing that I can see (in C++03) which allows an
implementation to reduce the value of container.capacity() when
calling clear.

--
James
 
Reply With Quote
 
Howard Hinnant
Guest
Posts: n/a
 
      03-24-2013
On Mar 24, 11:30*am, Leigh Johnston <(E-Mail Removed)> wrote:
> On 24/03/2013 15:04, James Kanze wrote:
>
> > On Saturday, March 23, 2013 9:30:02 PM UTC, Leigh Johnston wrote:
> >> Can we please change the ISO C++ Standard so that explicitly states what
> >> happens to a sequence container's capacity() after calling clear()?

>
> >> Currently the behaviour is unspecified and I know of at least one
> >> implementation that deallocates on vector<T>::clear().

>
> > Has the specification in C++11 changed? *In C++03, such an
> > implimentation would be illegal.

>
> As I said in my post it is unspecified what happens to a vector's
> capacity when calling clear().
>
>
>
> > Could you tell me which implementation you are talking about.
> > I've not verified recently, but neither g++ nor VC++ behaved
> > like this in the past.

>
> QNX.


This issue has been discussed by the LWG:

http://cplusplus.github.com/LWG/lwg-closed.html#1102

Note the [ Batavia (2009-05): ] comment in which the CEO of the vendor
that supplies QNX with its std::vector implementation believes the
complaint of the issue is already implied by the standard.

Note also the [ 2009-10 Santa Cruz: ] comment in which the LWG closes
the issue as NAD because the standard is already correct as written
(to agree with the [ Batavia (2009-05): ] comment.

Bottom line: It looks like QNX is using an older version of the
std::lib which has a bug and simply needs to be updated. All std::lib
implementations have bugs and active implementations are continually
updated with bug fixes.

I feel your frustration. Direct it towards QNX. The committee has
asked and answered this question.
 
Reply With Quote
 
Howard Hinnant
Guest
Posts: n/a
 
      03-24-2013
On Mar 24, 1:08*pm, Leigh Johnston <(E-Mail Removed)> wrote:
> On 24/03/2013 16:57, Howard Hinnant wrote:
> > I feel your frustration. *Direct it towards QNX. *The committee has
> > asked and answered this question.

>
> Unless I am missing something I don't see how the current wording in the
> Standard implies no deallocation on calling clear(). *IMO it is not NAD;
> without specifying the behaviour it is the implementation vendor that
> can claim NAD.


You always have this option:

http://cplusplus.github.com/LWG/lwg-...l#submit_issue
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      03-24-2013
On Sunday, March 24, 2013 3:30:10 PM UTC, Leigh Johnston wrote:
> On 24/03/2013 15:04, James Kanze wrote:
>
> > On Saturday, March 23, 2013 9:30:02 PM UTC, Leigh Johnston wrote:

>
> >> Can we please change the ISO C++ Standard so that explicitly states what

>
> >> happens to a sequence container's capacity() after calling clear()?

>
> >

>
> >> Currently the behaviour is unspecified and I know of at least one

>
> >> implementation that deallocates on vector<T>::clear().

>
> >

>
> > Has the specification in C++11 changed? In C++03, such an

>
> > implimentation would be illegal.

>
>
>
> As I said in my post it is unspecified what happens to a vector's
>
> capacity when calling clear().
>
>
>
> >

>
> > Could you tell me which implementation you are talking about.

>
> > I've not verified recently, but neither g++ nor VC++ behaved

>
> > like this in the past.

>
>
>
> QNX.
>
>
>
> >

>
> >> If the behaviour remains unspecified then it is effectively

>
> >> impossible to write portable code that uses clear() and you

>
> >> have to hope things such as v.erase(v.begin(), v.end()) behave

>
> >> more consistently across different implementations.

>
> >

>
> > There is nothing that I can see (in C++03) which allows an

>
> > implementation to reduce the value of container.capacity() when

>
> > calling clear.

>
>
>
> I repeat: the behaviour was and is unspecified.
>
>
>
> /Leigh


 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      03-24-2013
On Sunday, March 24, 2013 3:30:10 PM UTC, Leigh Johnston wrote:
> On 24/03/2013 15:04, James Kanze wrote:
> > On Saturday, March 23, 2013 9:30:02 PM UTC, Leigh Johnston wrote:
> >> Can we please change the ISO C++ Standard so that explicitly states what
> >> happens to a sequence container's capacity() after calling clear()?


> >> Currently the behaviour is unspecified and I know of at least one
> >> implementation that deallocates on vector<T>::clear().


> > Has the specification in C++11 changed? In C++03, such an
> > implimentation would be illegal.


> As I said in my post it is unspecified what happens to a vector's
> capacity when calling clear().


Not in C++03, at least. It may be overspecification, but
clear() is defined to be exactly the same a erase(begin(),
end()). And that doesn't allow reducing capacity.

> > Could you tell me which implementation you are talking about.
> > I've not verified recently, but neither g++ nor VC++ behaved
> > like this in the past.


> QNX.


I'm not familiar with it, but if clear() changes the capacity,
I'd send in a bug report.

> >> If the behaviour remains unspecified then it is effectively
> >> impossible to write portable code that uses clear() and you
> >> have to hope things such as v.erase(v.begin(), v.end()) behave
> >> more consistently across different implementations.


> > There is nothing that I can see (in C++03) which allows an
> > implementation to reduce the value of container.capacity() when
> > calling clear.


> I repeat: the behaviour was and is unspecified.


You can repeat it as much as you like. It's your word against
the standard, and in such cases, the standard rules. A quick
check does seem to indicate that C++11 has changed the
definition of clear(). The new definition is fairly ambiguous,
but I still don't see where it authorizes the capacity to
change.

--
James
 
Reply With Quote
 
James Kanze
Guest
Posts: n/a
 
      03-26-2013
On Sunday, 24 March 2013 19:47:59 UTC, Leigh Johnston wrote:
> On 24/03/2013 19:38, James Kanze wrote:


> > On Sunday, March 24, 2013 3:30:10 PM UTC, Leigh Johnston wrote:
> >> On 24/03/2013 15:04, James Kanze wrote:
> >>> On Saturday, March 23, 2013 9:30:02 PM UTC, Leigh Johnston wrote:
> >>>> If the behaviour remains unspecified then it is effectively
> >>>> impossible to write portable code that uses clear() and you
> >>>> have to hope things such as v.erase(v.begin(), v.end()) behave
> >>>> more consistently across different implementations.


> >>> There is nothing that I can see (in C++03) which allows an
> >>> implementation to reduce the value of container.capacity() when
> >>> calling clear.


> >> I repeat: the behaviour was and is unspecified.


> > You can repeat it as much as you like. It's your word against
> > the standard, and in such cases, the standard rules. A quick
> > check does seem to indicate that C++11 has changed the
> > definition of clear(). The new definition is fairly ambiguous,
> > but I still don't see where it authorizes the capacity to
> > change.


> Stop being obtuse; the new definition doesn't say that the capacity
> can't change either.


The standard, as written, doesn't allow the capacity to change
(or at least, it doesn't allow it to be reduced). The
restriction is very indirect, and you have to work through
a number of different constraints and guarantees on different
functions, but the guarantee is there. (It was slightly clearer
in the older versions, where clear was specified in terms of
erase. It's rather obvious that erase can't reallocate in
general, and there's no reason to assume that erasing everything
is a special case.)

> I want the Standard to be changed so that it
> explicitly states the behaviour re capacity when calling clear().


I would agree with that. I don't like rules which require
imagining special cases, then working through the constraints in
5 or 10 apparently unrelated sections in order to deduce them.

--
James
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
size vs. capacity in a container, such as vector puzzlecracker C++ 4 10-14-2008 07:29 AM
how to iterate over sequence and non-sequence ? stef mientki Python 13 10-20-2007 10:21 AM
Backward traversal of sequence container: stop condition SpOiLeR C++ 13 03-04-2005 06:51 PM
DVD stated capacity vs Ulead Movie factory stated capacity ftran999 Computer Support 5 11-23-2004 08:43 AM
BOOT SEQUENCE (how to change boot sequence) bird Computer Support 13 12-24-2003 02:20 AM



Advertisments