Skip to main content
GameDev.net gamedev.net
🔒 Locked

Quick question - Which STL container?

Started by Trillian Dec 6, 2005 at 5:43 PM 4 replies 600+ views
Original Post
Trillian
Trillian
Hello! I'm using a vector to stock some MyClass class. However, MyClass contains a pointer. That caused a lot of bugs. I recently discovered it by outputting some text when destroying a MyClass object. Here's the problem: Vector[0] contains MyClass with pointer to 0x1111 When I add a MyClass with a pointer to 0x2222, std::vector copies the vector[0] and deletes the old one. But the destructor of myclass deletes the pointed object. So the copy now points to a deleted object. Is there a stl container that doesn't copy the items like so? std::list? I want my container to delete its object only when i'm done with it.
Zahlman
Zahlman
No. If your class contains a pointer, implement a proper copy constructor - and assignment operator, and destructor. It is virtually guaranteed that you need them for some other reason anyway.
doynax
doynax
If I understood your request then I think want a std::deque or std::list container since they are guaranteed never to relocate objects, or at least that's how they're commonly implemented. A more direction solution would be to keep pointers instead of actual instances in the vector.
Quote:
Original post by Zahlman
No. If your class contains a pointer, implement a proper copy constructor - and assignment operator, and destructor. It is virtually guaranteed that you need them for some other reason anyway.
Ideally, yes, but that's not always possible or efficient.
Trillian
Trillian
Thanks guys!

As I don't currently know how to use copy constructors, i'm goign to use lists. Anyway, do not need a quick access to my container because it will probably only contain 1 to 4 textures.

EDIT: Yes! Now what I was working on for a couple of week's time now works!
Consider yourselves rated up!
darookie
darookie
// simple "save" implementationclass MyClass {   int * p;public:   // default ctor - - just initialises the data   MyClass( ) : p( new int[ 16 ] ) { }   // copy ctor - mostly invoked by STL containers and algorithms   MyClass( MyClass const & c ) : p( new int[ 16 ] ) {       std::copy ( c.p, c.p + 16, p );   }   // overloaded assignment operator, allows for MyClass a, b; a = b;   MyClass & operator = ( MyClass const & rhs ) {       std::copy( rhs.p, rhs.p + 16, p );       return *this;   }   // note that the dtor didn't need to be modified for this simple example   ~MyClass( ) {      delete[] p;   }};

You could optimise the above sample by using a ref-counted pointer instead. This would lead to a rather complicated design, though, since you'd also need some copy-on-write facilities with that to ensure data integrity.

HTH,
Pat
demonkoryu
demonkoryu
Store pointers to your objects.
To do this, create them with new and delete every of them with a for loop over the vector when you are done.

Alternatively, replace the pointer members of your class with std::auto_ptr (beware of auto_ptr<>'s non-intuitive copy semantics)or one of the boost::smart_ptr's like boost::shared_ptr.

Learn more about the important memory management ideom in C++: RAII.

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.