| | |
Pointer, vector, and/or deque problem
Please support our C++ advertiser: Intel Parallel Studio Home
Thread Solved |
•
•
Join Date: Sep 2005
Posts: 4
Reputation:
Solved Threads: 0
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.
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.
•
•
•
•
Originally Posted by nikto
No suggestion is too obvious.
Last edited by Dave Sinkula; Sep 13th, 2005 at 10:47 pm. Reason: D'oh! Pokey again. :(
"One of the methods used by statists to destroy capitalism consists in establishing controls that tie a given industry hand and foot, making it unable to solve its problems, then declaring that freedom has failed and stronger controls are necessary." --Ayn Rand
•
•
Join Date: Jul 2005
Posts: 164
Reputation:
Solved Threads: 5
lol there is no bug dave. its a feature!
•
•
Join Date: Sep 2005
Posts: 4
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Jul 2005
Posts: 164
Reputation:
Solved Threads: 5
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.
•
•
Join Date: Sep 2005
Posts: 4
Reputation:
Solved Threads: 0
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.
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.
•
•
Join Date: Sep 2005
Posts: 4
Reputation:
Solved Threads: 0
Turns out I do need
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.
C++ Syntax (Toggle Plain Text)
Viruses.reserve( 1000 );
It's like you're both right.
![]() |
Similar Threads
- pointer and array arithmetic (C)
- passing vector to a pointer? (C++)
- Error pushing pointers to a vector (C++)
Other Threads in the C++ Forum
- Previous Thread: C++ Question...with uncompleted answer..anyone can solve it?
- Next Thread: Decleration and defination Problem
| Thread Tools | Search this Thread |
api array arrays based beginner binary bitmap c++ c/c++ calculator char char* class code coding compile compiler console conversion count data database delete deploy developer dll download dynamic dynamiccharacterarray email encryption error file forms fstream function functions game getline givemetehcodez graph gui homeworkhelp homeworkhelper iamthwee ifstream input int java lib linker list loop looping loops map math matrix memory multiple news node number numbertoword output parameter pointer problem program programming project proxy python random read recursion recursive reference rpg sorting string strings temperature template test text text-file tree url variable vector video visual visualstudio win32 windows winsock word wordfrequency wxwidgets






