Jump to content
  • Advertisement
Sign in to follow this  
nuclear123

std::vector help

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

I'm a little confused on using a vector On how to delete elements from a vector and make sure their memory is deallocated. The following 2 examples if anyone could help me out



// Using dynamic Memory?

std::vector<Object*> Vector;

void AddElement()
{
Object* pObject = new Object;
Vector.push_back( pObject );
return;
}

void DeleteElement()
{
// How do i remove my Object* from the vector and then deallocate it to prevent a leak?
}




// Using Stack Memory

std::vector<Object> Vector;

void AddElement()
{
Object pObject;
Vector.push_back( Object ); // will this cause an error since i'm referencing memory from the stack that no longer exists?? or does container perform a COPY?
return;
}

void DeleteElement()
{
// How do i remove my Object from the vector and then deallocate it to prevent a leak?
}



I guess this leads me to one more question as well. Are the Containers within the STL considered Reference or value containers?

Container classes generally come in two different varieties. Value containers are compositions that store copies of the objects that they are holding (and thus are responsible for creating and destroying those copies). Reference containers are aggregations that store pointers or references to other objects (and thus are not responsible for creation or destruction of those objects).

Share this post


Link to post
Share on other sites
Advertisement
In the first case you allocated it, so you have to delete it. So you would have to find it in the vector, delete Vector (or through an iterator), and then that index would contain a pointer to memory that was freed, so would want to erase that dangling pointer from the list to make them all valid again.
In the second case you didnt allocate anything, a copy was made, so you may want to erase that element from the vector if you dont want it in there, but you dont need to free anything.

Share this post


Link to post
Share on other sites
All containers in the standard library store values, and they will destruct any value stored in the container. Keep in mind though that if you store pointers, then only the pointers will be destroyed but not the object being pointed to, because those are not stored in the in the container.

So to remove a pointer from the vector, you need to remove it from the vector and call delete or delete [] on it. Otherwise, you don't have to do anything.

Also, if you need to store pointers, consider boost's pointer containers. They store pointers, but with value semantics, and they also claim ownership of the pointers you store so you don't have to bother with manually releasing the pointers.

Share this post


Link to post
Share on other sites

All containers in the standard library store values, and they will destruct any value stored in the container. Keep in mind though that if you store pointers, then only the pointers will be destroyed but not the object being pointed to, because those are not stored in the in the container.

So to remove a pointer from the vector, you need to remove it from the vector and call delete or delete [] on it. Otherwise, you don't have to do anything.

Also, if you need to store pointers, consider boost's pointer containers. They store pointers, but with value semantics, and they also claim ownership of the pointers you store so you don't have to bother with manually releasing the pointers.


i know how to delete a element from the vector but how do i obtain its reference/address so i can prevent a leak?

vector.erase();

but how do i obtain the reference of the particular element im deleting to i can free it :(?

Share this post


Link to post
Share on other sites
vector::erase cannot be called without parameter like you imply. It takes an iterator (or an iterator range) to the element being removed, so you already have the reference to the element with the iterator. Delete it, then erase the pointer from the vector.

Share this post


Link to post
Share on other sites
Are you doing random access removal (e.g. a library where the client code could ask to remove any object in the vector)?

Are you doing bulk removal?

Are you doing conditional removal?

There are different idioms and approaches for all of the above.

Share this post


Link to post
Share on other sites
If the order of the elements in the vector doesn't matter, then you can swap the element you want to delete with the last one and do a pop_back (after the necessary cleanup mentioned above, of course).

Share this post


Link to post
Share on other sites

[quote name='Brother Bob' timestamp='1327612918' post='4906541']
All containers in the standard library store values, and they will destruct any value stored in the container. Keep in mind though that if you store pointers, then only the pointers will be destroyed but not the object being pointed to, because those are not stored in the in the container.

So to remove a pointer from the vector, you need to remove it from the vector and call delete or delete [] on it. Otherwise, you don't have to do anything.

Also, if you need to store pointers, consider boost's pointer containers. They store pointers, but with value semantics, and they also claim ownership of the pointers you store so you don't have to bother with manually releasing the pointers.


i know how to delete a element from the vector but how do i obtain its reference/address so i can prevent a leak?

vector.erase();

but how do i obtain the reference of the particular element im deleting to i can free it sad.png?
[/quote]
Well, if your iterator is called say iter then to get at the thing it points to you do *iter.
As the thing the iterator points to is itself a pointer, you delete the thing that the pointer points to by doing delete *iter;.

Share this post


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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

GameDev.net 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!