Archived

This topic is now archived and is closed to further replies.

[STL]How do I delete a vector

This topic is 5016 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 have a class member variable that is a vector, I use it for a given amount of time but I want to delete it and hence free memory IN a certain method.How do I make sure that the vector is deleted? resize() and clear() do not help, because the capacity() is still the same as the size as before. I want to reduce the current meomory requiremnets of this program on my machine because even though I have 512 MB RAM, I still get a warning that windows is low on virtual memory and that the page size is being adjusted.I use vectors for a class that is instantiated a number of times and my guess is that keeping the vectors right until the time that the class objects go out of scope and are automaticlly destroyed might not be the best idea and I must explicitly free memory when Im done and over with it. The vectors store data in excess of 50 mb sometimes,Do you think this could be the problem that leads to the warning about "low virtual memory"?

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
The swap trick:

std::vector<...>(v).swap(v);

I.e. you make a copy of the current vector, which will have minimal capacity() (as the implementation permits), then swap it with the vector you had so you can still keep on using the same ''v'' vector instead of the new copy. The swap operation is lightning fast and the construction of a new vector with only required space is a necessary step, so this is a fast method.

In your case, you probably just need an empty vector which you swap with your old vector, and do this operation inside a small code block { /* here */ } so that the new vector with which you swapped contents, will immediately remove the old data.

Share this post


Link to post
Share on other sites
Thanks, this might do the trick, will test it out later.

Can you please comment on the virtual memory issue?

Also,if using memcpy to copy the contents of a buffer to this vector could possibly result in an erroneous copy given the "windows being low on virual memory" warning?

Share this post


Link to post
Share on other sites
You could also try using a deque. A vector of 50mb needs 50mb of contiguous memory, a deque does not. The disadvantage is that you can''t use memcopy with a deque. You should instead use std::copy(begin-of-source,past-end-of-source,deque.begin()); from the header.

Share this post


Link to post
Share on other sites
quote:
Original post by George2
You shouldn''t be using memcpy with a std::vector.




What can I use instead of memcpy? Any suggestions?

I still suspect that there is an error in the copying that is resulting in such values such as null in a large part of the vector, I dont know to to ascertain this for sure though

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
quote:
Original post by hehenoobhehe
quote:
Original post by George2
You shouldn''t be using memcpy with a std::vector.




What can I use instead of memcpy? Any suggestions?

I still suspect that there is an error in the copying that is resulting in such values such as null in a large part of the vector, I dont know to to ascertain this for sure though



twanvl already said it: use std::copy.

Though basically (if not 100% truthfully) ALL std::vector implementations have the data as a continuous block, so it''s legal to use memcpy to copy stuff there. And at least on MSVC++7.0, memcpy was twice as fast as std::copy. I would use memcpy if it was applicable (i.e. copying raw data, and container was fixed to be a std::vector) and if speed was of concern.

Share this post


Link to post
Share on other sites
The low memory warning is almost certainly because it has to find 50Mb of contiguous memory. This is going to be very unlikely. Either store pointers to the actual data (unless that''s what you''re doing already) which would take up less room and the real data could be all over the place. Or use a deque. I''d go with the deque as it can just be a drop in replacement.

Share this post


Link to post
Share on other sites
quote:
Original post by twanvl
You could also try using a deque. A vector of 50mb needs 50mb of contiguous memory, a deque does not. The disadvantage is that you can''t use memcopy with a deque. You should instead use std::copy(begin-of-source,past-end-of-source,deque.begin()); from the header.


erm... that will crash...

...unless you''ve sized the deque to be the correct size first.

or use a back_inserter:
std::copy(begin-of-source,past-end-of-source,std::back_inserter(deque));

I''d create a deque and then swap it in though. So there are a number of ways to do these things.


std::deque<int> myDeque;
std::vector<int> myBigVector;
//add stuff to myBigVector;

//...

std::deque<int> tempDeque(myBigVector.begin(), myBigVector.end());
myDeque.swap(tempDeque);

Share this post


Link to post
Share on other sites