clearing a vector

E

ES Kim

It is well known that vector::swap can be used to effectively free
the allocated memory. ie,

vector<int> v;
vector<int>().swap(v);

Someone suggested another way using a placement-new.

#include <vector>
using std::vector;
int main()
{
vector<int> v(10);
v.~vector<int>();
new(&v) vector<int>();
}

Is this code portable?
 
W

Wouter Lievens

ES Kim said:
It is well known that vector::swap can be used to effectively free
the allocated memory. ie,

vector<int> v;
vector<int>().swap(v);

Someone suggested another way using a placement-new.

#include <vector>
using std::vector;
int main()
{
vector<int> v(10);
v.~vector<int>();
new(&v) vector<int>();
}

Is this code portable?


There's an std::vector<T>::clear() method, maybe that'll do?
 
J

John Harrison

There's an std::vector<T>::clear() method, maybe that'll do?

clear does not guarantee to free any memory. It reduces the size of the
vector to zero, it does not necessarily reduce the capacity of the vector to
zero.

john
 
J

Jeff Schwab

ES said:
It is well known that vector::swap can be used to effectively free
the allocated memory. ie,

vector<int> v;
vector<int>().swap(v);

Someone suggested another way using a placement-new.

#include <vector>
using std::vector;
int main()
{
vector<int> v(10);
v.~vector<int>();
new(&v) vector<int>();
}

Is this code portable?

I don't see anything non-portable about it; I think it's actually OK.
It reminds me of the explicit initialization and destruction code used
in the implementation of the standard containers. However, it does seem
odd to use placement new on stack (please don't whine) space. If you
get a definitive answer, please post it.
 
H

Howard Hinnant

"ES Kim" <[email protected]> said:
It is well known that vector::swap can be used to effectively free
the allocated memory. ie,

vector<int> v;
vector<int>().swap(v);

Someone suggested another way using a placement-new.

#include <vector>
using std::vector;
int main()
{
vector<int> v(10);
v.~vector<int>();
new(&v) vector<int>();
}

Is this code portable?

Yes and no. Both of the above code sequences will leave v with the
capacity of a default constructed vector. The standard does not
guarantee that the capacity of a default constructed vector is 0. That
being said, I'm not aware of any implementation which does not have a 0
capacity on default construction.

Assuming for the moment that the vector default constructor does acquire
some resource, either in terms of its capacity, or in the default
construction of std::allocator (again, I know of no implementation which
actually does this), then the swap idiom is safer than the
destruct-construct sequence. With the swap idiom if the default vector
constructor throws an exception, then v is left unmodified. With the
destruct-construct sequence if the default vector constructor throws an
exception, v is left in a destructed state. This is likely to cause a
crash later when v is destructed again (likely a double delete of heap
memory).

-Howard
 
S

Siemel Naran

It is well known that vector::swap can be used to effectively free
the allocated memory. ie,

Not sure if it is well known (to people who don't read the newsgroups).
{ // line 0
vector<int> v(10);
v.~vector<int>();
new(&v) vector<int>(); // line 3
} // line 4

What if line 3 throws an exception for some reason? It's unlikely to,
because line 2 releases lots of memory, and line 3 should take some of that
back. But you never know; the default constructor of class vector is
allowed to throw. Then the object 'v' will be deleted twice, once in line
2, and again in line 4. This will probably crash your program.
 

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

Forum statistics

Threads
474,169
Messages
2,570,917
Members
47,458
Latest member
Chris#

Latest Threads

Top