473,394 Members | 1,658 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,394 software developers and data experts.

Clearing static std::vector fails

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;
}
Jul 22 '05 #1
7 3687
Martin Magnusson wrote:
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().
What's the damn point of posting working code when yours has a problem?
My question is: is this code valid, or is it just a coincidence that it
doesn't crash?
The class you store in the vectors (compared to the 'int' in the code
below) probably has some dynamic memory management ('new' in the c-tor
and 'delete' in the d-tor) which is screwed up (like not following the
"Rule of Three"). Without seeing the actual code it is impossible to
really help.
#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;
}


The code is valid. It doesn't make much sense to me that a non-static
member function of a class changes a static data member, but I guess
your particular design calls for it.

V
Jul 22 '05 #2
"Martin Magnusson" <martin@-xx-blecket-xx-.org> wrote in message
news:1102500815.13d5048dd8f2463266d8ef7e81655654@t eranews...
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;
}


Calling clear() and resize() on a vector that happens to be a static member
is no different than calling it on some other vector.

I haven't explicitly tried, but I'm pretty sure the code you posted won't
compile. Is X a class name or a index variable in your for loops? Based on
this example, it's hard to know for sure what you intend your code to do.

-Matt
Jul 22 '05 #3
Matt Wharton wrote:
[...]
I haven't explicitly tried, but I'm pretty sure the code you posted won't
compile. Is X a class name or a index variable in your for loops? Based on
this example, it's hard to know for sure what you intend your code to do.


You should explicitly try before expressing such assuredness. The code
compiles just fine. The variable X and type X do not interfere with each
other. This:

class A {};
int main() {
A a;
int A;
}

is a perfectly valid C++ program.

V
Jul 22 '05 #4
"Victor Bazarov" <v.********@comAcast.net> wrote in message
news:2Y*****************@newsread1.dllstx09.us.to. verio.net...
Matt Wharton wrote:
[...]
I haven't explicitly tried, but I'm pretty sure the code you posted won't
compile. Is X a class name or a index variable in your for loops? Based
on this example, it's hard to know for sure what you intend your code to
do.


You should explicitly try before expressing such assuredness. The code
compiles just fine. The variable X and type X do not interfere with each
other. This:

class A {};
int main() {
A a;
int A;
}

is a perfectly valid C++ program.


Ok, you got me--fair enough :). I'll still contend however that, even
though this is technically a "valid C++ program", this kind of a coding
practice (using a type name as a variable name) doesn't make for readable
code and it doesn't provide any tangible benefit (that I can see). If I
were on the standards committee, I would have voted against this, but what
do I know? :p

-M
Jul 22 '05 #5
Victor Bazarov <v.********@comAcast.net> writes:
You should explicitly try before expressing such assuredness. The code
compiles just fine. The variable X and type X do not interfere with each
other. This:

class A {};
int main() {
A a;
int A;
}

is a perfectly valid C++ program.


I see that this is true, but could you explain why the following
is not valid C++:

----
struct foo {};

struct bar {
foo foo;
};

int main()
{
bar bar;
foo foo;
}
----

Rename or remove the foo member from bar and the compiler is
happy, otherwise I get (using g++ 3.2):

t.cc:4: declaration of `foo bar::foo'
t.cc:1: changes meaning of `foo' from `struct foo'

....which I don't quite understand.

Josh
Jul 22 '05 #6
Josh wrote:
Victor Bazarov <v.********@comAcast.net> writes:

You should explicitly try before expressing such assuredness. The code
compiles just fine. The variable X and type X do not interfere with each
other. This:

class A {};
int main() {
A a;
int A;
}

is a perfectly valid C++ program.

I see that this is true, but could you explain why the following
is not valid C++:

----
struct foo {};

struct bar {
foo foo;
};

int main()
{
bar bar;
foo foo;
}
----

Rename or remove the foo member from bar and the compiler is
happy, otherwise I get (using g++ 3.2):

t.cc:4: declaration of `foo bar::foo'
t.cc:1: changes meaning of `foo' from `struct foo'


Is that a warning or an error message?
...which I don't quite understand.


If it is in fact an error message, the only explanation to that is that
g++ 3.2 has a bug in that area. The code is valid.

If it is in fact a warning, what do you not understand? It warns you that
inside the class 'bar' the use of 'foo' after that declaration is
different than before that declaration or outside the class. So, if you
later write something that uses 'foo' you will get bar's member instead of
the global type-id.

V
Jul 22 '05 #7
Victor Bazarov <v.********@comAcast.net> writes:
Josh wrote:

I see that this is true, but could you explain why the
following is not valid C++:

----
struct foo {};
struct bar {
foo foo;
};
int main()
{
bar bar;
foo foo;
}
----
Rename or remove the foo member from bar and the compiler is
happy, otherwise I get (using g++ 3.2):
t.cc:4: declaration of `foo bar::foo'
t.cc:1: changes meaning of `foo' from `struct foo'


Is that a warning or an error message?


An error message. Interesting, the only difference in output
between g++ 3.2 and 3.4 is the addition of "error: ":

t.cc:4: error: declaration of `foo bar::foo'
t.cc:1: error: changes meaning of `foo' from `struct foo'
...which I don't quite understand.


If it is in fact an error message, the only explanation to that
is that g++ 3.2 has a bug in that area. The code is valid.

If it is in fact a warning, what do you not understand? It
warns you that inside the class 'bar' the use of 'foo' after
that declaration is different than before that declaration or
outside the class. So, if you later write something that uses
'foo' you will get bar's member instead of the global type-id.


Ah, okay. The error text makes sense now. What I don't
understand is why the complier can clearly distinguish the two
inside the scope of main, but not inside the class definition.

The reason I'm bringing this up is that we ran into this issue at
work when a header including a similar structure definition was
included into a c++ source file. Previously, it had only been
built with gcc which does not seem to have a problem with the
syntax.

Josh
Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: Dylan | last post by:
what is the best way of initializing a static std::vector data member with some values? (currently I just push_back some values in the constructor if the size == 0) thanks
5
by: canned.net | last post by:
I have a class Scene that has several subclasses: World, Vault, etc. I fill a vector with these classes and then cannot go through and delete them. What's the trick to deleting pointers from a...
20
by: Anonymous | last post by:
Is there a non-brute force method of doing this? transform() looked likely but had no predefined function object. std::vector<double> src; std::vector<int> dest; ...
8
by: Ross A. Finlayson | last post by:
I'm trying to write some C code, but I want to use C++'s std::vector. Indeed, if the code is compiled as C++, I want the container to actually be std::vector, in this case of a collection of value...
6
by: zl2k | last post by:
I searched the topic and noticed that the initilization of static needs to be treated specially, but not know how. Here is my code which giving the link error: test.h...
6
by: Jia | last post by:
Hi all, I have a class foo which has a static vector of pointers of type base class, and a static function to set this vector. #include <iostream> #include <vector> using namespace std;...
4
by: Josefo | last post by:
Hello, is someone so kind to tell me why I am getting the following errors ? vector_static_function.c:20: error: expected constructor, destructor, or type conversion before '.' token...
2
by: Rockair | last post by:
hi! there is a class: class card { static vector<string> names; //... };
23
by: Mike -- Email Ignored | last post by:
In std::vector, is reserve or resize required? On: Linux mbrc32 2.6.22.1-41.fc7 #1 SMP Fri Jul 27 18:10:34 EDT 2007 i686 athlon i386 GNU/Linux Using: g++ (GCC) 4.1.2 20070502 (Red Hat...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.