Boost logo

Boost :

Subject: [boost] [range][stride] broken?
From: Maxim Yanchenko (maximyanchenko_at_[hidden])
Date: 2010-12-22 00:01:34


Consider the following test:
  std::vector<int> v;
  boost::push_back( v, boost::irange(0, 30) );
  boost::copy( v, std::ostream_iterator<int>(std::cout, " "));
  std::cout << std::endl << std::endl;

  for ( int i = 1; i <= 30; i++ )
  {
    std::cout << std::setw(2) << i << ": ";
    boost::copy( v | boost::adaptors::strided(i),
      std::ostream_iterator<int>(std::cout, " "));
    std::cout << std::endl;
    
    //std::cout << std::setw(2) << "" << ": ";
    //BOOST_FOREACH( int x, v | boost::adaptors::strided(i) )
    // std::cout << x << ' ';
    //std::cout << std::endl;
  }

Here is the output:
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29

 1: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 2: 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28
 3: 0 3 6 9 12 15 18 21 24 27
 4: 0 4 8 12 16 20 24 ///< where is 28?
 5: 0 5 10 15 20 25
 6: 0 6 12 18 24
 7: 0 7 14 21 ///< where is 28?
 8: 0 8 16 ///< where is 24?
 9: 0 9 18 ///< where is 27?
10: 0 10 20
11: 0 11 ///< where is 22?
...
16: 0 ///< where is 16?
etc

If you uncomment the BOOST_FOREACH code, it crashes at stride=4 (prints 28 and
then crashes):
0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 1: 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
  : 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 2: 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28
  : 0 2 4 6 8 10 12 14 16 18 20 22 24 26 28
 3: 0 3 6 9 12 15 18 21 24 27
  : 0 3 6 9 12 15 18 21 24 27
 4: 0 4 8 12 16 20 24
/usr/include/stlport-5.2.1/stlport/stl/debug/_iterator.h(170): STL assertion
failure : _Incrementable(*this, __n, _Iterator_category())
  : 0 4 8 12 16 20 24 28 unknown location:0: fatal error in "test": signal:
SIGABRT (application abort requested)

Nothing changes if I replace "v | boost::adaptors::strided(i)" with
"boost::adaptors::stride(v,i)".

It's Boost 1.45.0 on STLPort 5.2.1, gcc 4.4.
IF I switch from std::vector to boost::array, BOOST_FOREACH just doesn't stop
and keeps printing memory until crash, so I don't think it's an STLPort problem.

Am I doing anything wrong, or range::stride is broken?

Thanks,
Maxim


Boost list run by bdawes at acm.org, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk