473,401 Members | 2,127 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,401 software developers and data experts.

Cant understand how this code works....

gpraghuram
1,275 Expert 1GB
HI all,
I wrote the following piece of code
Expand|Select|Wrap|Line Numbers
  1. class A
  2. {
  3.     A(){cout<<"ctor"<<endl;
  4.     ~A(){cout<<"dtor"<<endl;
  5. };
  6.  
  7. int main()
  8. {
  9.     std::map<A> a_map(10);
  10.     cout<<"size is:"<<10<<endl;
  11.     return 0;
  12. }
  13.  
the output is
ctor
dtor
10
dtor
dtor
dtor
dtor
dtor
dtor
dtor
dtor
dtor
dtor

The destructor is called 10 times but the constructor is called only once.
I tried this in Linux using g++.

Can somebody throw some light on this.

Thanks
Raghuram
Nov 15 '07 #1
5 1316
Banfa
9,065 Expert Mod 8TB
This is rather dependent on how std::map is implemented on your system however here is my guess

the distructor is actually called 11 times (once before your output line) and the default constructor once.

I guess that the constructor of std:map constructs a single A (calling the default constructor once) then copies it to it's entries (calling the copy constuctor 10 times) then deletes the temporary A object calling the destructor once.

Then you output line runs, then the map is deleted and each item in the map is deleted calling the destructor.

You could implement a copy constructor to test this theory.
Nov 15 '07 #2
Ganon11
3,652 Expert 2GB
Doesn't a map disallow duplicates? Although I guess that, since A doesn't provide an overridden operator== function, the map will compare based on addresses or some other such comparison, preventing it from seeing these as duplicates.
Nov 15 '07 #3
weaknessforcats
9,208 Expert Mod 8TB
Your map needs to have pair<> objects.

The first member or the pair is the key and the second member is a A object.
Expand|Select|Wrap|Line Numbers
  1. map<int, A>  themap;
  2.  
By the way, your code won't compile. If it does, then you have enabled C++ language extenssions that are non-standard.
Nov 15 '07 #4
gpraghuram
1,275 Expert 1GB
Your map needs to have pair<> objects.

The first member or the pair is the key and the second member is a A object.
Expand|Select|Wrap|Line Numbers
  1. map<int, A>  themap;
  2.  
By the way, your code won't compile. If it does, then you have enabled C++ language extenssions that are non-standard.

I have made a mistake.
It is a vector and not a map.
I got some idea from Banfa, anyway i will try with copy constructor and post back again.

Raghuram
Nov 16 '07 #5
gpraghuram
1,275 Expert 1GB
I have made a mistake.
It is a vector and not a map.
I got some idea from Banfa, anyway i will try with copy constructor and post back again.

Raghuram
Banfa, u are right.
As u say the copy constructor is being called 10 times and due to which the destructor is being called 10 times.
Thanks for your explanation.

Raghuram
Nov 16 '07 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

9
by: Dan K. | last post by:
Hi Folks, Problem is, that on one of my controls the DEL key wont work in textboxes. i have different controls of this kind in my app and this is the only one the DEL Key wont do his job....
0
by: CMEDIA_SOUND | last post by:
I have a peculiar problem, I have a tabpage with a label control on it. When i set a background image to the tabpage and drag the label around it has paint issues in that it is slow, granted the...
16
by: Mike Fellows | last post by:
when i load my windows form i populate a combobox i use the code below Dim conn As New System.Data.SqlClient.SqlConnection(strConn) Dim sql As String = "AllLenders" Dim da As New...
2
by: g35rider | last post by:
Hi, I have the following code that is giving this error, I cant simplify the code, I was just testing some theory for something we are doing and was getting an issue here. Please someone point out...
3
by: arun.hallan | last post by:
Hi, I'm having problems with authentication. I have a set of users that are allowed to use a webpage. They are in domain A. My goal is to get the username of these users and then check them...
8
by: Jim Florence | last post by:
Hi, I've just add a couple of dropdowns to my ASP form, set up a sqldatasource and bound to the dropdown and it all works great. I want the first item of the dropdown to be blank soa fter...
2
by: moondaddy | last post by:
I had to repost this because I had to update and change my msdn alias. I will re-ask the question and clarify a few things that were not clear before. This code is all executed on my dev...
2
by: Sticksboy | last post by:
I keep getting this error message - Item cant be found in the collection corresponding to the requested name or ordinal I dont understand what it means to be honest. I am trying to get the code...
6
by: WolfgangS | last post by:
Ok first off, i am a total beginner at this groups stuff and i have no clue how this works. This is probabaly the wrong group for my problem but i will post it anyways. Learning by doing right? ...
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: 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
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

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.