473,406 Members | 2,745 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,406 software developers and data experts.

odd behavior in STL

I've been working on a project that uses the STL classes, and twice now
I've run into some odd behavior I can't find the cause of. Basically,
it seems as if a vector is changing its contents by itself. I've tested
the code (C++ in a tcl wrapper) with gcc 2.96 on redhat 7.3 and gcc
2.95.3 on slackware 8.0 and have similar errors on both.

Here is some of the relevant code:

void Graph::update() {
printf("Graph::update()\n");
for (vector<Component*>::iterator iter = myComponents.begin();
iter < myComponents.end();
iter++) {
printf("updating %s(%d)\n", (*iter)->className(), (*iter)->myID());
(*iter)->update();
}
...
printf("updated successfully\n");
}

And here is the output:

graph loaded
Graph::update()
updating Component(3)
updating Component(4)
updating inductor(112)
updating resistor(116)
updating voltage(120)
updating ground(126)
updating junction(127)
updating junction(128)
updating junction(129)
updated successfully
updating node(49)
updating capacitor(14)
updating node(15)
updating node(16)
updating node(21)
updating node(22)
updating node(35)
updating node(36)
updating node(43)
updating node(44)
updating node(51)
Segmentation fault

There are a few really strange things about this.

- myComponents is only changed in the function that prints "graph
loaded", if I comment out the part that changes it then the program
doesn't crash.

- update() is usually called as part of a main loop, but the first
output in the output is from a call I added at the end of the function
that changes myComponents to make sure that the right values are getting
put in there (which they are).

- The first call to update() finishes, and another starts, but for some
reason the second one doesn't print "Graph::update()".

- myComponents is a vector<Component*> and although all of the classes
in the output from the first time update() is called are subclasses of
Component, the "node" class is not, but somehow it is in myComponents
the second time update() is called.

The first time I had a similar problem, I ended up using a different
approach, and it just went away. I've looked over every possible thing
I could think of causing this, and I can't find anything. Any ideas or
similar experiences? (Reply to group, email is not valid).

-Ed

Jul 19 '05 #1
3 1627
Thanks. I've made that and all similar changes in the code. I'm still
having the exact same problem though, so let me know if anyone knows of
a possible cause.

-Ed

Xenos wrote:
You should not use "iter < myComponents.end()". It should read "iter !=
myComponents.end()"
"Ed Platt" <no****@mudbrick.mit.edu> wrote in message
news:3F**************@mudbrick.mit.edu...
I've been working on a project that uses the STL classes, and twice now
I've run into some odd behavior I can't find the cause of. Basically,
it seems as if a vector is changing its contents by itself. I've tested
the code (C++ in a tcl wrapper) with gcc 2.96 on redhat 7.3 and gcc
2.95.3 on slackware 8.0 and have similar errors on both.

Here is some of the relevant code:

void Graph::update() {
printf("Graph::update()\n");
for (vector<Component*>::iterator iter = myComponents.begin();
iter < myComponents.end();
iter++) {
printf("updating %s(%d)\n", (*iter)->className(), (*iter)->myID());
(*iter)->update();
}
...
printf("updated successfully\n");
}

And here is the output:

graph loaded
Graph::update()
updating Component(3)
updating Component(4)
updating inductor(112)
updating resistor(116)
updating voltage(120)
updating ground(126)
updating junction(127)
updating junction(128)
updating junction(129)
updated successfully
updating node(49)
updating capacitor(14)
updating node(15)
updating node(16)
updating node(21)
updating node(22)
updating node(35)
updating node(36)
updating node(43)
updating node(44)
updating node(51)
Segmentation fault

There are a few really strange things about this.

- myComponents is only changed in the function that prints "graph
loaded", if I comment out the part that changes it then the program
doesn't crash.

- update() is usually called as part of a main loop, but the first
output in the output is from a call I added at the end of the function
that changes myComponents to make sure that the right values are getting
put in there (which they are).

- The first call to update() finishes, and another starts, but for some
reason the second one doesn't print "Graph::update()".

- myComponents is a vector<Component*> and although all of the classes
in the output from the first time update() is called are subclasses of
Component, the "node" class is not, but somehow it is in myComponents
the second time update() is called.

The first time I had a similar problem, I ended up using a different
approach, and it just went away. I've looked over every possible thing
I could think of causing this, and I can't find anything. Any ideas or
similar experiences? (Reply to group, email is not valid).

-Ed


Jul 19 '05 #2

"Ed Platt" <no****@mudbrick.mit.edu> wrote in message
news:3F**************@mudbrick.mit.edu...
Thanks. I've made that and all similar changes in the code. I'm still
having the exact same problem though, so let me know if anyone knows of
a possible cause.


I think you are going to have to post more code.

john
Jul 19 '05 #3
Thanks for the help everyone, I just figured it out. It's rediculously
simple now that I see it, but I suppose things like this tend to be like
that. It turns out that one of the Component pointers being updated
changed myComponents and invalidated the iterator. Here's an
explanation of all the odd behavior:
- myComponents is only changed in the function that prints "graph
loaded", if I comment out the part that changes it then the program
doesn't crash.
This is actually happening inside Graph::update().
- update() is usually called as part of a main loop, but the first
output in the output is from a call I added at the end of the function
that changes myComponents to make sure that the right values are getting
put in there (which they are).
Because the function that changes myComponents is called in
Graph::update(), the second call to Graph::update() is actually before
the first one has finished.
- The first call to update() finishes, and another starts, but for some
reason the second one doesn't print "Graph::update()".
The second call is actually finishing. It has a valid iterator because
it is called after myComponents has changed. Once it returns, the
iterator in the original Graph::update() is invalid and causes the crash.
- myComponents is a vector<Component*> and although all of the classes
in the output from the first time update() is called are subclasses of
Component, the "node" class is not, but somehow it is in myComponents
the second time update() is called.


Invalid iterator, I wish STL handled things like this better.

-Ed

Jul 19 '05 #4

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

Similar topics

12
by: Dave Rahardja | last post by:
Does the C++ standard specify the behavior of floating point numbers during "exceptional" (exceptional with respect to floating point numbers, not exceptions) conditions? For example: double...
19
by: E. Robert Tisdale | last post by:
In the context of the comp.lang.c newsgroup, the term "undefined behavior" actually refers to behavior not defined by the ANSI/ISO C 9 standard. Specifically, it is *not* true that "anything can...
23
by: Ken Turkowski | last post by:
The construct (void*)(((long)ptr + 3) & ~3) worked well until now to enforce alignment of the pointer to long boundaries. However, now VC++ warns about it, undoubtedly to help things work on 64...
38
by: Steven Bethard | last post by:
> >>> aList = > >>> it = iter(aList) > >>> zip(it, it) > > That behavior is currently an accident. >http://sourceforge.net/tracker/?group_id=5470&atid=105470&func=detail&aid=1121416
7
by: Mike Livenspargar | last post by:
We have an application converted from v1.1 Framework to v2.0. The executable references a class library which in turn has a web reference. The web reference 'URL Behavior' is set to dynamic. We...
66
by: gyan | last post by:
Hi All, I am using sprintf and getting starnge output in following case char temp_rn; memset(temp_rn,'\0',12); sprintf(temp_rn,"0%s",temp_rn); the final value in temp_rn is 00 how it...
12
by: Rajesh S R | last post by:
Can anyone tell me what is the difference between undefined behavior and unspecified behavior? Though I've read what is given about them, in ISO standards, I'm still not able to get the...
28
by: v4vijayakumar | last post by:
#include <string> #include <iostream> using namespace std; int main() { string str; str.resize(5); str = 't';
35
by: bukzor | last post by:
I've found some bizzare behavior when using mutable values (lists, dicts, etc) as the default argument of a function. I want to get the community's feedback on this. It's easiest to explain with...
33
by: coolguyaroundyou | last post by:
Will the following statement invoke undefined behavior : a^=b,b^=a,a^=b ; given that a and b are of int-type ?? Be cautious, I have not written a^=b^=a^=b ; which, of course, is undefined....
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
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
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...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.