This is the mail archive of the
libstdc++@gcc.gnu.org
mailing list for the libstdc++ project.
debug/vector anomalous behaviour
- From: Stefano Soffia <soffia at cs dot unipr dot it>
- To: libstdc++ at gcc dot gnu dot org
- Date: Sat, 10 May 2008 10:45:40 +0200 (CEST)
- Subject: debug/vector anomalous behaviour
I was playing with the -D_GLIBCXX_DEBUG flag when I stumbled upon the
following example.
#include <vector>
#include <algorithm>
#include <iterator>
typedef std::vector<unsigned> array_t;
typedef std::back_insert_iterator<array_t> bii_t;
array_t a;
int main() {
// Push 5 elements.
a.push_back(0);
a.push_back(1);
a.push_back(2);
a.push_back(3);
a.push_back(4);
// Ensure that there is enough space for other two elements.
// (2 + 5 = 7)
if (a.capacity() < 7)
a.reserve(7);
// Add two new elements.
std::copy(a.begin(),
a.begin() + 2,
bii_t(a));
}
$ g++ -o test -D_GLIBCXX_DEBUG test.cc
$ ./test
/usr/lib/gcc/i686-pc-linux-gnu/4.2.3/include/g++-v4/debug/safe_iterator.h:209:
error: attempt to increment a singular iterator.
Objects involved in the operation:
iterator "this" @ 0x0xbffa87cc {
type =
N11__gnu_debug14_Safe_iteratorIN9__gnu_cxx17__normal_iteratorIPjNSt6__nor
m6vectorIjSaIjEEEEENSt7__debug6vectorIjS6_EEEE
(mutable iterator);
state = singular;
references sequence with type `NSt7__debug6vectorIjSaIjEEE' @
0x0xbffa87cc
}
Aborted
$
Digging in the include files I found the following:
/4.2.3/include/g++-v4/debug/vector
180 using _Base::capacity;
...
362 bool
363 _M_requires_reallocation(size_type __elements)
364 {
365 #ifdef _GLIBCXX_DEBUG_PEDANTIC
366 return __elements > this->capacity();
367 #else
368 return __elements > _M_guaranteed_capacity;
369 #endif
370 }
which seems quite strange to me. Indeed, compiled in pedantic mode, it gives
no errors.
$ g++ -o test -D_GLIBCXX_DEBUG -D_GLIBCXX_DEBUG_PEDANTIC test.cc
$ ./test
$
Am I missing something?
Stefano Soffia
Applied Formal Methods Laboratory
Department of Mathematics, University of Parma, Italy
http://www.cs.unipr.it/afmlab/