M
Martin Magnusson
I'm having trouble clearing and resizing a static std::vector of
std::vectors (segmentation fault). Is it OK to call clear() and resize()
on a static attribute? My code is similar to the one posted below. The
code below works, though. My original code crashes on the second call to
X::f().
My question is: is this code valid, or is it just a coincidence that it
doesn't crash?
#include <iostream>
#include <vector>
using namespace std;
class X
{
private:
static vector< vector< int > > v;
public:
void f( unsigned x, unsigned y )
{
v.clear();
v.resize( x );
for (unsigned Y = 0; Y < x; ++Y)
v[Y].resize( y );
for (unsigned X = 0; X < x; ++X)
for (unsigned Y = 0; Y < y; ++Y)
v[X][Y] = 10*X + Y;
}
};
vector< vector< int > > X::v;
int main( )
{
X x;
x.f(10,10);
x.f(5,5);
x.f(15, 15);
return 0;
}
std::vectors (segmentation fault). Is it OK to call clear() and resize()
on a static attribute? My code is similar to the one posted below. The
code below works, though. My original code crashes on the second call to
X::f().
My question is: is this code valid, or is it just a coincidence that it
doesn't crash?
#include <iostream>
#include <vector>
using namespace std;
class X
{
private:
static vector< vector< int > > v;
public:
void f( unsigned x, unsigned y )
{
v.clear();
v.resize( x );
for (unsigned Y = 0; Y < x; ++Y)
v[Y].resize( y );
for (unsigned X = 0; X < x; ++X)
for (unsigned Y = 0; Y < y; ++Y)
v[X][Y] = 10*X + Y;
}
};
vector< vector< int > > X::v;
int main( )
{
X x;
x.f(10,10);
x.f(5,5);
x.f(15, 15);
return 0;
}