473,573 Members | 3,904 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 3704
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 .13d5048dd8f246 3266d8ef7e81655 654@teranews...
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.********@com Acast.net> wrote in message
news:2Y******** *********@newsr ead1.dllstx09.u s.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.********@com Acast.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.********@com Acast.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.********@com Acast.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
11123
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
12262
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 vector? Code: std::vector<Scene*> scenes; void initializeGame()
20
17762
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; std::vector<double>::size_type size = src.size(); dest.reserve(size); for (std::vector<int>::size_type i = 0;
8
5099
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 types or std::vector<int>. So where I would use an int* and reallocate it from time to time in C, and randomly access it via , then I figure to...
6
23574
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 ------------------------------------- #ifndef TEST_H #define TEST_H #include <vector> #include <iostream>
6
3980
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; class super{ protected:
4
3497
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 vector_static_function.c:21: error: expected constructor, destructor, or type conversion before '.' token
2
5593
by: Rockair | last post by:
hi! there is a class: class card { static vector<string> names; //... };
23
3447
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 4.1.2-12) The program below fails, but if the reserve(en)
0
7741
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
8165
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7730
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
6347
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
0
3692
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3686
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2163
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
1
1256
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
984
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

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.