Sign in to follow this  

How does the STL vector clean up memory?

This topic is 3373 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

It's known that when removing an object from a STL vector it will correctly call it's deconstructor. Then, when the vector is deleted it will also call the deconstructors of the remaining objects. However I'm a little confused about what they doing to product this last result? For example, say we had a vector with size of 6 and enough memory allocated for 12 object. When the vector "delete"s that memory what is preventing the last 6 memory spaces from trying to call the deconstructor for the class type? I would imagine they are doing some memory trick, but I have yet to find anything that supports this. - Kiro www.wingsout.com

Share this post


Link to post
Share on other sites
Well, it calls the destructor if T is not a pointer:


std::vector<MyClass> foo; // will destruct

std::vector<MyClass*> foo; //will not destruct




Anyway it's using placement new to instantiate new members, it also knows it's size so it can correctly destruct only 5 when 5 items are present but memory is allocated for 10.

Placement New

[EDIT: oh damn you people and your fast fingers]

-me

Share this post


Link to post
Share on other sites
Okay, that would makes sense. I've had to use placement new before so I'm familiar with the functionality, very cool stuff. Though I was unsure if there really was any alternative method the STL vector could have been doing.

Thanks for the replies. ^^

I would assume then that the STL vector is doing runtime checking of the template type to see if it should call the constructor/deconstructors.

- Kiro
www.wingsout.com

Share this post


Link to post
Share on other sites
Quote:
Original post by KiroNeem
I would assume then that the STL vector is doing runtime checking of the template type to see if it should call the constructor/deconstructors.


What do you mean? It will always call an object's constructor when constructing it, and always call the destructor when destroying it. Why would you want an object that could be destroyed without a call to its destructor?

Share this post


Link to post
Share on other sites
Quote:
Original post by Driv3MeFar
Quote:
Original post by KiroNeem
I would assume then that the STL vector is doing runtime checking of the template type to see if it should call the constructor/deconstructors.


What do you mean? It will always call an object's constructor when constructing it, and always call the destructor when destroying it. Why would you want an object that could be destroyed without a call to its destructor?
Primitive types don't have destructors, so the following is invalid, isn't it?
	int data;
void* buffer = &data;
int* pInt = new(buffer) int(42);
pInt->~int();

As for KiroNeem's question - if the STL is checking if the type has a destructor or not, the check will definitely be done at compile-time, not runtime.

Share this post


Link to post
Share on other sites
Quote:
Original post by Hodgman
Quote:
Original post by Driv3MeFar
Quote:
Original post by KiroNeem
I would assume then that the STL vector is doing runtime checking of the template type to see if it should call the constructor/deconstructors.


What do you mean? It will always call an object's constructor when constructing it, and always call the destructor when destroying it. Why would you want an object that could be destroyed without a call to its destructor?
Primitive types don't have destructors, so the following is invalid, isn't it?*** Source Snippet Removed ***
As for KiroNeem's question - if the STL is checking if the type has a destructor or not, the check will definitely be done at compile-time, not runtime.



typedef int T;
T* t = static_cast<T*>(::operator new(sizeof(T)));
new (t) T();
t->~T();
::operator delete(t);


Is valid. :) Check your local standard for more interesting revelations about C++.

Share this post


Link to post
Share on other sites
Quote:
Original post by Washu
Quote:
Original post by Hodgman
Quote:
Original post by Driv3MeFar
Quote:
Original post by KiroNeem
I would assume then that the STL vector is doing runtime checking of the template type to see if it should call the constructor/deconstructors.


What do you mean? It will always call an object's constructor when constructing it, and always call the destructor when destroying it. Why would you want an object that could be destroyed without a call to its destructor?
Primitive types don't have destructors, so the following is invalid, isn't it?*** Source Snippet Removed ***
As for KiroNeem's question - if the STL is checking if the type has a destructor or not, the check will definitely be done at compile-time, not runtime.


*** Source Snippet Removed ***
Is valid. :) Check your local standard for more interesting revelations about C++.


Your right! That is magical and quite awesome. Thanks for bringing this up, it really glues together all of the pieces I had questions about.

- Kiro
www.wingsout.com

Share this post


Link to post
Share on other sites

This topic is 3373 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this