This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
Possible bug in list::size()? Or my misreading?
- From: Brad Spencer <spencer at infointeractive dot com>
- To: libstdc++ at gcc dot gnu dot org
- Date: Thu, 3 Jan 2002 17:20:06 -0400
- Subject: Possible bug in list::size()? Or my misreading?
While I was debugging today, I ended up looking at the definition of
list::size(), initially on 2.95.3. I was concerned by what I saw, so
I checked 3.0.3 with libstdc++-v3.0.95, but found (roughly) the same thing:
size_type
size() const
{ return distance(begin(), end()); }
Please tell me I've just misunderstood this all these years, but my
copy of the standard says, in Section 23.1, Table 65:
Those entries marked "(Note A)" should have constant complexity.
Sure enough, size() is marked with "(Note A)". However, the trivial
program below is enough to show that this is not the case and list
computes its size in O(n) with each call to size! What have I missed?
The program:
#include <list>
#include <iostream>
int
main()
{
std::list<int> li;
for(unsigned int i = 0; i < 10000; i++) {
li.push_back(i);
}
std::cout << li.size() << std::endl;
}
And a snippet from my gdb session, in which I traced it into the
InputIterator version of distance, as expected:
(gdb) l stl_iterator_base_funcs.h:116
111 {
112 // concept requirements
113
__glibcpp_function_requires(_InputIteratorConcept<_InputIterator>)
114 typename
iterator_traits<_InputIterator>::difference_type __n = 0;
115 while (__first != __last) {
116 ++__first; ++__n;
117 }
118 return __n;
119 }
120
(gdb) info break 2
Num Type Disp Enb Address What
2 breakpoint keep y 0x0804a644 in
__distance<std::_List_iterator<int, const int&, const int*> >
at
stl_iterator_base_funcs.h:116
breakpoint already hit 10000 times
:( I can't see anywhere where the list container maintains a count of
the current number of elements, so it looks like this would also be a
non-trivial fix. I haven't looked in detail at any of the other
containers yet.
Should I file a bug report?
--
-----------------------------------------------------------------
Brad Spencer - spencer@infointeractive.com - "It's quite nice..."
Systems Architect | InfoInterActive Corp. | An AOL Company