Advertisement Jump to content
Sign in to follow this  

freeing a Vector of struct/class

This topic is 4683 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

lang: C++ / Win32 compiler: Visual C++.NET 2005 Say you have a sprite class or struct that contains a bitmap and all the other details required to animated the sprite at it's own animation speed and movement speed. Now in the program say you have this vector in the program: std::vector<CSprite> m_hSprites Now the sprites are being constantly used, but now when the program comes to a end, and the m_hSprites vector needs to delete all the sprites that are being used. What would be the best approach to freeing up the vector of sprite classes/structs? Note: The deconstructor for the CSprite class properly frees the memory used by the bitmap for the CSprite. 1] Just do a m_hSprite.clear(), and the deconstructor will get called for all the vector elements. 2] Iterate through the vector to free up memory by popping off the elements. 3] Is there a better way to go then 1 or 2 Thanks for the help, and replys. = ]

Share this post

Link to post
Share on other sites
well if the vector is defined as:

std::vector<CSprite> m_hSprites

you don't need to do any freeing because the vector will just clear itself and the memory will be gone. That is because when you push_back to a vector you push_back a copy of the type being passed. In this case it will fully copy your CSprite class into the vector. The vector is responsible for clearing any memory it allocates so you're safe.

if, however the definition is like this (which if CSprite is a large class it should be):

std::vector<CSprite*> m_hSprites

then you need to clear it like so:

std::vector<CSprite*>::iterator ite = m_hSprites.end();
for ( std::vector<CSprite*>::iterator it = m_hSprites.begin(); it != ite; ++it )
delete *it;

this is because with a vector declared with pointers to objects you would populate it like so:

m_hSprites.push_back( new CSprite() );

i.e. you declared the memory so you are responsible for delete'ing it


Share this post

Link to post
Share on other sites
actually once the program ends or the class goes out of scope, shouldn't the deconstructor automatically delete the vector?

but otherwise, the first choice is good.

Share this post

Link to post
Share on other sites
Original post by Alpha_ProgDes
shouldn't the deconstructor automatically delete the vector?

That's what I was thinking, but I wasn't sure.

Thank you Palidine, Alpha_ProgDes

Share this post

Link to post
Share on other sites
Sign in to follow this  

  • Advertisement

Important Information

By using, you agree to our community Guidelines, Terms of Use, and Privacy Policy. is your game development community. Create an account for your GameDev Portfolio and participate in the largest developer community in the games industry.

Sign me up!