pushing back to vector produces bad data in first member - c++

Started by
2 comments, last by Evil Steve 16 years, 3 months ago
Hi In main() I have a line of code

unitList.push_back(addUnit())

where addUnit is my unit creation function which returns an object of type Unit, and unitList is a vector for Units (type defined as ListOfUnits). This appears to work as intended in all circumstances Elsewhere, I have a function from main which is passed unitList by reference: The function's prototype

Army createArmy(ListOfUnits &unitList)

which gives it access to the vector, since the vector is not a global, but exists within main. There is also a vector of my Army class. This class holds a pointer to the Unit type that the army consists of. In the createArmy function, it is sometimes necessary to add a new unit type, and since I have passed in the units vector by reference, I have tried to accomplish this with

unitList.push_back(addUnit())

But weird behaviour ensues!! If I skip this step and make a number of armies with existing units, everything works properly. However, if I make a new army and want to make a new unit to go into that army, the push-back(addUnit()) line is called. This corrupts one part of the FIRST element in the Army vector, regardless of size. The corruption is that the pointer to a Unit type in the Army class is corrupted, and when access is attempted, this crashes the program. The debugger flags this pointer, which as I said is weirdly in the first element of the vector,as a bad pointer. Slow and painful debugging has proven that the pointer is fine before unitList.push_back(addUnit()) is called from within the createUnit function, but is corrupt immediately after. I have absolutely no idea what could be causing this. Does anyone out there have a clue?
Advertisement
Any type used in a STL container must have a constructor, destructor, copy constructor and operator=, or it must be a POD type. If you don't implement a copy constructor or operator=, the compiler will generate a default one which just calls operator= for every data member. If you have any pointers in your class, this is almost certainly not what you want to happen.

So, either implement a copy constructor and operator=, or you could store objects in the vector by pointer (And allocate them with new and delete them when needed), or, better - store smart pointers in the vector.


EDIT: Oh, I see you're storing pointers to the objects in the vector. You can't do that [smile] As soon as you add or remove an element from the vector, all iterators and pointers to elements are invalidated, since the vector might need to reallocate memory to make room for the new object.
How would this lead to push_back affecting previously added and confirmed-as-good members?
Quote:Original post by gah_ribaldi
How would this lead to push_back affecting previously added and confirmed-as-good members?
See my edit. You'll need to store pointers to objects in the vector (Or better, smart pointers).

This topic is closed to new replies.

Advertisement