Skip to main content
GameDev.net gamedev.net
🔒 Locked

[STL]How do I delete a vector

Started by hehenoobhehe Mar 25, 2004 at 6:41 AM 8 replies 1.2k views
Original Post
hehenoobhehe
hehenoobhehe
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"?
hehenoobhehe
hehenoobhehe
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?
twanvl
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.
George2
George2
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
hehenoobhehe
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

Qw3r7yU10p!
Qw3r7yU10p!
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.
Qw3r7yU10p!
Qw3r7yU10p!
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);
DigitalDelusion
DigitalDelusion
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

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.