Sorry your browser is not supported!

You are using an outdated browser that does not support modern web technologies, in order to use this site please update to a new browser.

Browsers supported include Chrome, FireFox, Safari, Opera, Internet Explorer 10+ or Microsoft Edge.

Dark GDK / Vectors (STL Container)

Author
Message
Michael P
18
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 28th Jul 2008 21:20
I'm learning to use vectors and I'm having trouble with vectors and classes.

In the below code, after the resizing the vector to 50 I would have expected 50 constructions to occur and 0 destructions. However, 1 destructor and 1 constructor occurs. Why is this?



Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 28th Jul 2008 21:22
I imagine it's for efficiency. It should runs the constructor for each as their used.

Windows Vista Home Premium Intel Pentium Dual-Core 1.6 Ghz 1GB DDR2 RAM GeForce 8600GT Twin Turbo
Michael P
18
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 28th Jul 2008 21:29
I thought that too, but no it doesn't :



Output is the same as the OP but the string is filled properly.
Lilith
16
Years of Service
User Offline
Joined: 12th Feb 2008
Location: Dallas, TX
Posted: 28th Jul 2008 21:39 Edited at: 28th Jul 2008 21:40
Most like case is that resizing only reserves the space but doesn't fill the slots. If you do a push_back in order to append an item and it needs to re-size the vector for the one item then there's some CPU time taken to do the resize each time. If the size has been increased beyond enough to hold, say, an extra 10 items then it only has to do the resize one time instead of 10 times.

Lilith, Night Butterfly
I'm not a programmer but I play one in the office
Michael P
18
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 28th Jul 2008 22:09 Edited at: 28th Jul 2008 22:09
I tried using push_back and now I get 1 construction and 50 destructions . The code is below.



The reason why I care about the constructions and destructions is that I want to be able to allocate memory dynamically during the construction and deallocate memory during the destruction. Here is some code which is an example of what I want to do (unsurprisingly it does not work):
IanM
Retired Moderator
22
Years of Service
User Offline
Joined: 11th Sep 2002
Location: In my moon base
Posted: 28th Jul 2008 23:45 Edited at: 28th Jul 2008 23:46
What is happening is that a single instance of the class is created (as you've seen), then the rest are created using the copy constructor (the default one as you haven't provided one).



[EDIT]It's 'reserve' that allocates memory but doesn't construct the objects - the space should be treated as inaccessible.

Michael P
18
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 30th Jul 2008 12:08
Thanks, but I'm still a bit confused. In the below code I'm trying to increase the size of the vector by one without losing all the previously added data. How can this be done?



Output:
Mahoney
16
Years of Service
User Offline
Joined: 14th Apr 2008
Location: The Interwebs
Posted: 30th Jul 2008 18:40
That number is the value of an int that isn't set to any value.

Windows Vista Home Premium Intel Pentium Dual-Core 1.6 Ghz 1GB DDR2 RAM GeForce 8600GT Twin Turbo
Charles Thompson
16
Years of Service
User Offline
Joined: 30th Jul 2008
Location:
Posted: 30th Jul 2008 19:07
Just out of curiosity sakes set the size of the vector to 50 originally and perform that same code. If it works then I have a theory on why your current code doesn't. Let me know how it goes.

Charles Thompson
Michael P
18
Years of Service
User Offline
Joined: 6th Mar 2006
Location: London (UK)
Posted: 30th Jul 2008 20:08
I realized what the problem was; the copy constructor wasn't doing anything.

In the below code, the resize operation will properly allocate new memory to new elements. Let me know if you see anything wrong with this:
haYnguy
16
Years of Service
User Offline
Joined: 5th Jun 2008
Location: on my island
Posted: 1st Aug 2008 07:35
i was gonna say. The copy constructor hasn't been defined.

-- from my island, aloha wau 'ia 'oe --

Login to post a reply

Server time is: 2024-11-20 17:34:36
Your offset time is: 2024-11-20 17:34:36