STL vector

D

Daniel T.

Does an STL vector ever reduce its capacity? (i.e. after a lot of
deletions)

No, and there is no way to explicitly tell it to do so. If you want to
reduce the capacity, you need to swap the vector with one that has the
capacity you want.

For example:

void fn( vector<int>& foo ) {
// trim capacity to minimum needed
vector<int>( foo ).swap( foo );
}

What does the line do? It copy constructs a temp vector from 'foo' then
swaps its contents with those that are currently in 'foo' then destroys
the temp. I'm not sure I'd ever use the idiom though. It seems
pointless in a virtual memory environment, and would probably cause too
much fragmentation in one that doesn't have virtual memory.
 
E

Erik Wikström

No, and there is no way to explicitly tell it to do so. If you want to
reduce the capacity, you need to swap the vector with one that has the
capacity you want.

For example:

void fn( vector<int>& foo ) {
// trim capacity to minimum needed
vector<int>( foo ).swap( foo );
}

What does the line do? It copy constructs a temp vector from 'foo' then
swaps its contents with those that are currently in 'foo' then destroys
the temp. I'm not sure I'd ever use the idiom though. It seems
pointless in a virtual memory environment, and would probably cause too
much fragmentation in one that doesn't have virtual memory.

That, of course, depends on what kinds of applications you are running.
Despite the virtual memory you only have 2GB to play with by default on
a Windows machine, and for some applications that might not be plenty.
 
D

Daniel T.

Erik Wikström said:
That, of course, depends on what kinds of applications you are
running. Despite the virtual memory you only have 2GB to play with
by default on a Windows machine, and for some applications that
might not be plenty.

But with something that large, unless you are using the idiom to
completely clear out memory, you will run into the same fragmentation
problems that systems without virtual memory have to worry about.

The point to the OP is, don't get in the habit of doing the above just
because you did a bunch of deletions, only do it if you have identified
a specific problem that the idiom will fix.
 
R

Rahul

That, of course, depends on what kinds of applications you are running.
Despite the virtual memory you only have 2GB to play with by default on
a Windows machine, and for some applications that might not be plenty.

Why do you say the memory to be only 2 GB? I know its not related to
the OP's question, but just was curious about it...
 
B

Bo Persson

Rahul wrote:
:: On 2007-12-23 05:53, Daniel T. wrote:
::
::
::
::: (e-mail address removed) wrote:
::
:::: Does an STL vector ever reduce its capacity? (i.e. after a lot of
:::: deletions)
::
::: No, and there is no way to explicitly tell it to do so. If you
::: want to reduce the capacity, you need to swap the vector with one
::: that has the capacity you want.
::
::: For example:
::
::: void fn( vector<int>& foo ) {
::: // trim capacity to minimum needed
::: vector<int>( foo ).swap( foo );
::: }
::
::: What does the line do? It copy constructs a temp vector from
::: 'foo' then swaps its contents with those that are currently in
::: 'foo' then destroys the temp. I'm not sure I'd ever use the
::: idiom though. It seems pointless in a virtual memory environment,
::: and would probably cause too much fragmentation in one that
::: doesn't have virtual memory.
::
:: That, of course, depends on what kinds of applications you are
:: running. Despite the virtual memory you only have 2GB to play with
:: by default on a Windows machine, and for some applications that
:: might not be plenty.
::
:: --
:: Erik Wikström
:
: Why do you say the memory to be only 2 GB? I know its not related to
: the OP's question, but just was curious about it...

Because Windows and other popular 32 bit operating systems divide the
available address space as a 2 GB user space and 2 GB for the OS
itself.

Think about how many bytes you can address using a 32 bit pointer!


Bo Persson
 
E

Erik Wikström

Why do you say the memory to be only 2 GB? I know its not related to
the OP's question, but just was curious about it...

Because it is. The 4GB virtual address space on a 32-bit Windows machine
is divided in two parts, 2GB to user applications and 2GB for the
kernel. Using an option during boot you can increase the user space part
to 3GB. I do not know how big the address space is for 64-bit
applications on a 64-bit Windows but I'd imagine that it is quite a bit
larger.

IIRC, on Linux the address space is divided 1GB kernel and 3GB user
space by default, but it is tunable.
 
J

Juha Nieminen

Does an STL vector ever reduce its capacity? (i.e. after a lot of
deletions)

Btw, if you want a vector which reduces its own capacity, use std::deque.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
474,181
Messages
2,570,970
Members
47,537
Latest member
BellCorone

Latest Threads

Top