[STL]How do I delete a vector

Started by
8 comments, last by hehenoobhehe 20 years ago
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"?
Advertisement
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.
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?
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.
You shouldn''t be using memcpy with a std::vector.

As for the virtual memory being low, are you sure you don''t have memory leaks ?
"THE INFORMATION CONTAINED IN THIS REPORT IS CLASSIFIED; DO NOT GO TO FOX NEWS TO READ OR OBTAIN A COPY." , the pentagon
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

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.
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.
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);
A nice template version to trim capacity for vectors and
strings etc.
template <typename Cont>inline Cont& trim_capacity(Cont &c){	Cont(c).swap(c);	return c;}
HardDrop - hard link shell extension."Tread softly because you tread on my dreams" - Yeats

This topic is closed to new replies.

Advertisement