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.

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.

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.

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.

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.

Turns out I do need

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.

This question has already been answered. Start a new discussion instead.