How do the STL containers interact with destructors/constructors?

V

velthuijsen

just found the STL and now I'm mucking around with it to see how it
works.
First thing that I've found out is that I appearantly have no clue how
copying works inside the STL (or more likely no clue how it works in
general).
So I've got two questions
1) Where can I find a good explanation on how this works on the
internet?
2) Is the stumbling around I've done so far not to horrifying?

What I did was make my own class (including a constructor/destructor
to see how they would get called).
Then I added the following to the main.

list<MyOwnClass> MyOwnClassTestList; // global

void AddItemToTestList()
{
MyOwnClass TestItem;
MyOwnClassTestList.push_back(TestItem);
}

This resulted in the constructor being called once (at MyOwnClass
TestItem;)
and the destructor called twice (the } of AddItemToTestList is one
place, the other the place where I do MyOwnClassTestList.pop_front());
)
My current solutions to the double call to the destructor are the
following:

void AddItemToTestList()
{
MyOwnClass *TestItem = new MyOwnClass;
MyOwnClassTestList.push_back(*TestItem);
TestItem = NULL;
}

or

list<MyOwnClass*> MyOwnClassTestList; // global

void AddItemToTestList()
{
MyOwnClass *TestItem = new MyOwnClass;
MyOwnClassTestList.push_back(TestItem);
TestItem = NULL;
}

void RemoveItemFromTestList()
{
MyOwnClass *TestItem = MyOwnClassTestList.front();
MyOwnClassTestList.pop_front;
delete TestItem;
}

Is this the correct way to handle this situation? And if not what
should I do differently?
 
J

John Harrison

velthuijsen said:
just found the STL and now I'm mucking around with it to see how it
works.
First thing that I've found out is that I appearantly have no clue how
copying works inside the STL (or more likely no clue how it works in
general).

The STL is allowed to make copies of the objects you put into it at any time
(subject to the efficiency constraints). It is up to you to make sure that
the classes you put into STL containers are copyable and assignable. Of
course you should do this for most classes you write irrespective of whether
they are used in the STL or not.
So I've got two questions
1) Where can I find a good explanation on how this works on the
internet?
2) Is the stumbling around I've done so far not to horrifying?

What I did was make my own class (including a constructor/destructor
to see how they would get called).
Then I added the following to the main.

list<MyOwnClass> MyOwnClassTestList; // global

void AddItemToTestList()
{
MyOwnClass TestItem;
MyOwnClassTestList.push_back(TestItem);
}

This resulted in the constructor being called once (at MyOwnClass
TestItem;)
and the destructor called twice (the } of AddItemToTestList is one
place, the other the place where I do MyOwnClassTestList.pop_front());

That's because you forgot to define a copy constructor for your class. When
you do that you will find that the constructor is called once, the copy
constructor is called once and the destructor is called twice, so everything
is peachy.

Of course you just happened to run into this problem in the STL but you
could have run into it any time (if you had tried to return your class from
a function for instance).
)
My current solutions to the double call to the destructor are the
following:

void AddItemToTestList()
{
MyOwnClass *TestItem = new MyOwnClass;
MyOwnClassTestList.push_back(*TestItem);
TestItem = NULL;
}

or

list<MyOwnClass*> MyOwnClassTestList; // global

void AddItemToTestList()
{
MyOwnClass *TestItem = new MyOwnClass;
MyOwnClassTestList.push_back(TestItem);
TestItem = NULL;
}

void RemoveItemFromTestList()
{
MyOwnClass *TestItem = MyOwnClassTestList.front();
MyOwnClassTestList.pop_front;
delete TestItem;
}

Is this the correct way to handle this situation? And if not what
should I do differently?

No, define a copy constructor (and an assignment operator) then everything
will work correctly.

john
 
R

Rob Williscroft

velthuijsen wrote in
What I did was make my own class (including a constructor/destructor
to see how they would get called).
Then I added the following to the main.

list<MyOwnClass> MyOwnClassTestList; // global

void AddItemToTestList()
{
MyOwnClass TestItem;
MyOwnClassTestList.push_back(TestItem);
}

This resulted in the constructor being called once (at MyOwnClass
TestItem;)
and the destructor called twice (the } of AddItemToTestList is one
place, the other the place where I do MyOwnClassTestList.pop_front());
)
My current solutions to the double call to the destructor are the
following:

You neglected to add a copy constructor and assignment op to
your test class:

class MyOwnClass
{
// original code

MyOwnClass( MyOwnClass const &rhs )
{
std::cout << "MyOwnClass( MyOwnClass const & )\n";
}


MyOwnClass &operator =( MyOwnClass const &rhs )
{
std::cout << "MyOwnClass::eek:perator = ( MyOwnClass const & )\n";
}

};

Do this and you figures should balence out.
void AddItemToTestList()
{
MyOwnClass *TestItem = new MyOwnClass;
MyOwnClassTestList.push_back(*TestItem);
TestItem = NULL;
}

You have a memory leak above, *TestItem is copied (via the copy
constructor) into MyOwnClassTestList, then you just leave (leak)
TestItem on the heap.
or

list<MyOwnClass*> MyOwnClassTestList; // global

void AddItemToTestList()
{
MyOwnClass *TestItem = new MyOwnClass;
MyOwnClassTestList.push_back(TestItem);
TestItem = NULL;
}

This is Ok, but later whan MyOwnClassTestList gets destroyed
all of it elements get destroyed, but since they are pointers
the items they dont to don't get destroyed. Objects created
with new *must* be destroyed with delete.

As for an online reference you really should start with this
newsgroups FAQ:

http://www.parashift.com/c++-faq-lite/

HTH.

Rob.
 
V

velthuijsen

Thank you for the link and the pointing out what I forgot (that is you and John).
And thank you for giving me the next problem to solve on this. :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top