944,103 Members | Top Members by Rank

Ad:
  • C++ Discussion Thread
  • Marked Solved
  • Views: 3979
  • C++ RSS
Sep 13th, 2005
0

Pointer, vector, and/or deque problem

Expand Post »
I have a vector of objects of the Virus class. This vector is called 'Viruses.'

I have a deque of objects of the Host class. Each Host class object has a deque of pointers to Virus objects, i.e., deque< Virus * > currentInfections

Each Host object is randomly infected with different Virus objects. This means they go in the currentInfections deque.

I create a new Virus object called 'mutant' and add it to the Viruses vector, i.e., Viruses.push_back( mutant ).

Creating this mutant and pushing it back to the Viruses vector always destroys any Host objects' pointers to the first element in Viruses, without affecting the others or the first element itself. Instead of V1, V2, V3 in currentInfections, I get V2837188, V2, V3. The Viruses vector contains V1, V2, V3, mutant. And even more exciting, this always happens after the second mutant has been added to the Viruses vector, and it happens without constructing a pointer to the mutant. In other words, just creating these two mutants seems to screw up pointers to the first element in the vector. I don't even have to touch currentInfections for the error to appear.

I'm a doctoral student in biology and new to programming. No suggestion is too obvious.

I'm also desperate. Thanks for any help. Hope this is enough information for a diagnosis of some kind.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nikto is offline Offline
4 posts
since Sep 2005
Sep 13th, 2005
0

Re: Pointer, vector, and/or deque problem

Reputation Points: 19
Solved Threads: 5
Junior Poster
Stoned_coder is offline Offline
164 posts
since Jul 2005
Sep 13th, 2005
0

Re: Pointer, vector, and/or deque problem

Quote originally posted by nikto ...
No suggestion is too obvious.
Try making a "debug" version of the project and start stripping away code until the bug goes away. Then put back in the latest removal that displays the bug, if you don't figure out what it is, and then post that code with any relevant "minimal" snippet of input required.
Last edited by Dave Sinkula; Sep 13th, 2005 at 10:47 pm. Reason: D'oh! Pokey again. :(
Team Colleague
Reputation Points: 2780
Solved Threads: 312
long time no c
Dave Sinkula is offline Offline
4,790 posts
since Apr 2004
Sep 13th, 2005
0

Re: Pointer, vector, and/or deque problem

lol there is no bug dave. its a feature!
Reputation Points: 19
Solved Threads: 5
Junior Poster
Stoned_coder is offline Offline
164 posts
since Jul 2005
Sep 13th, 2005
0

Re: Pointer, vector, and/or deque problem

I really appreciate the help. The link Stoned_coder referred me to was sobering. I was enjoying vectors.

Unfortunately, nothing's different after writing

Viruses.reserve( 1000 );

at the beginning of my run. I did plenty of code stripping before posting here, but I'm going to keep at it until I have something that might be intelligible to others.

I really don't want to use a list, because I like the "at()" function...

I'll be back.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nikto is offline Offline
4 posts
since Sep 2005
Sep 14th, 2005
0

Re: Pointer, vector, and/or deque problem

ok might be a bug. somethings trampling on your vector. Set a watch on the vectors first few elements and single step thru your code in a debugger. watch for the point where the trampling occurs and you should be able to see why its happening.
Reputation Points: 19
Solved Threads: 5
Junior Poster
Stoned_coder is offline Offline
164 posts
since Jul 2005
Sep 14th, 2005
0

Sheepishly...

After 8.5 h, I've discovered the problem is in the initial construction of the mutant. Deep in a tangle of for and while loops, I was assigning the Virus mutant an empty address instead of a parent Virus.

I'm grateful, however, for being forewarned about iterator invalidation, and for being referred to good programming practices--I'm sure both tips will buy me much sleep in the future.

Thanks again. Sorry for falsely advertising my problem.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nikto is offline Offline
4 posts
since Sep 2005
Sep 14th, 2005
0

Exciting follow up

Turns out I do need
C++ Syntax (Toggle Plain Text)
  1. Viruses.reserve( 1000 );
I fixed the mutant initialization problem above, then decided to take out all my extra cout lines and the Viruses.reserve(). As soon as I removed the reserve(), the invalidated pointer references returned.

It's like you're both right.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
nikto is offline Offline
4 posts
since Sep 2005

This thread is solved

Either the thread starter or a moderator has marked this thread as solved. You can most likely trust the responses and answers given. There is most likely no reason for any further responses to be posted here. If you have a related question, please start a new thread in this forum instead.

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in C++ Forum Timeline: C++ Question...with uncompleted answer..anyone can solve it?
Next Thread in C++ Forum Timeline: Decleration and defination Problem





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC