Hi, I just found, that code that ran under Visual Studio 8 doesnt run under 9 anymore and now im seeking confirmation of whether my code is unsafe and just accidentially worked or what one can expect from a standard conforming implementation of STL.
The basic thing is that, given a vector V with some elements in it, I want to push_back one of these elements, like this:
[CODE=c]#include <iostream>
#include <vector>
#include <cassert>
using namespace std;
int main()
{
typedef vector<int> X;
X x;
x.push_back( 6 );
// make a vector V and fill it
vector<X> V;
for ( unsigned int i = 0; i < 10; i++ )
{
if ( V.size() == V.capacity() )
cout << "[" << V.size() << "]";
V.push_back( x );
cout << ".";
}
cout << "\n";
// push_back some elements
size_t s = V.size();
for ( unsigned int i = 0; i < s; i++ )
{
if ( V.size() == V.capacity() )
cout << "[" << V.size() << "]";
V.push_back( V[0] );
cout << ".";
const X& first = V[0];
size_t size_first = first.size();
const X& last = *V.rbegin();
size_t size_last = last.size();
assert( size_last == size_first ); // this assertion fails under VS9
}
cout << "\n";
}
[/CODE]
In the second loop I do V.push_back(V[0]);
Is it safe to do that?
It worked in VS8 but in VS9, in iteration i=3, after the push_back, the last element, V[13] is not a copy of the vector containing the number 6, but it is an empty vector, thus the assertion fails.
The basic thing is that, given a vector V with some elements in it, I want to push_back one of these elements, like this:
[CODE=c]#include <iostream>
#include <vector>
#include <cassert>
using namespace std;
int main()
{
typedef vector<int> X;
X x;
x.push_back( 6 );
// make a vector V and fill it
vector<X> V;
for ( unsigned int i = 0; i < 10; i++ )
{
if ( V.size() == V.capacity() )
cout << "[" << V.size() << "]";
V.push_back( x );
cout << ".";
}
cout << "\n";
// push_back some elements
size_t s = V.size();
for ( unsigned int i = 0; i < s; i++ )
{
if ( V.size() == V.capacity() )
cout << "[" << V.size() << "]";
V.push_back( V[0] );
cout << ".";
const X& first = V[0];
size_t size_first = first.size();
const X& last = *V.rbegin();
size_t size_last = last.size();
assert( size_last == size_first ); // this assertion fails under VS9
}
cout << "\n";
}
[/CODE]
In the second loop I do V.push_back(V[0]);
Is it safe to do that?
It worked in VS8 but in VS9, in iteration i=3, after the push_back, the last element, V[13] is not a copy of the vector containing the number 6, but it is an empty vector, thus the assertion fails.
Comment