473,385 Members | 1,838 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,385 software developers and data experts.

How do the STL containers interact with destructors/constructors?

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?
Jul 22 '05 #1
3 1410

"velthuijsen" <ve*********@hotmail.com> wrote in message
news:e5**************************@posting.google.c om...
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
Jul 22 '05 #2
velthuijsen wrote in
news:e5**************************@posting.google.c om:
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::operator = ( 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.
--
http://www.victim-prime.dsl.pipex.com/
Jul 22 '05 #3
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. :)
Jul 22 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Shubhadeep | last post by:
Dear Sir, Can anybody please tell me why cannot we have virtual destructors like virtual constructors in C++ ? Thanx, Regards, Shubhadeep
3
by: Rajesh Garg | last post by:
Can we have private constructors and destructors? IF yes what is the use of such constructors or destructors.....in the sense where can these be implemented in a system................. I have...
12
by: Ross Boylan | last post by:
I am trying to understand under what circumstances destructors get called with std::vector. I have an application in which I will put real objects, not just pointers, in the vector. 1. The...
3
by: Amit | last post by:
is there anything like static constructors or destructors in C++ ? if yes, how to implement it? Thanks, Amit.
3
by: rahul8143 | last post by:
hello, I write a following program and have problem in understanding constructors and destructors. #include <iostream.h> class vector { public: double x; double y;
5
by: PaulW | last post by:
Personally, I like to use simple abstractions to wrap resources For example consider a class that wraps a resource - say an IntPtr which is allocated in the constructor and deallocated in the...
5
by: ElanKathir | last post by:
Hi All ! I want to know about the constructors & Destructors, constructors ---> New Destructors --> What ? (Which keyword to use for Destructor) Thanks & Regards, ElanKathir.S.N, ASM...
5
by: Alok | last post by:
hii Would somebody clear my doubts on following qustions . What are virtual constructors and virtual destructors ? Why can't we have virtual constructors ? What are demerits of inheritence in...
15
by: Nindi73 | last post by:
HI If I define the class DoubleMap such that struct DoubleMap : public std::map<std::string, double>{}; Is there any overhead in calling std::map member functions ? Moreover are STL...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.