473,406 Members | 2,467 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,406 software developers and data experts.

clearing a vector

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?

--
ES Kim
Jul 22 '05 #1
7 1540
"ES Kim" <no@spam.mail> schreef in bericht
news:c7**********@news1.kornet.net...
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?
Jul 22 '05 #2
>

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
Jul 22 '05 #3
ES Kim wrote:
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.
Jul 22 '05 #4
In article <c7**********@news1.kornet.net>, "ES Kim" <no@spam.mail>
wrote:
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
Jul 22 '05 #5
"ES Kim" <no@spam.mail> wrote in message
news:c7**********@news1.kornet.net...
Is this code portable?


Yes, but it's yucky (that's a technical term).
Jul 22 '05 #6
"ES Kim" <no@spam.mail> wrote in message
news:c7**********@news1.kornet.net...
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.
Jul 22 '05 #7

"Andrew Koenig" <ar*@acm.org> wrote in message
news:82********************@bgtnsc04-news.ops.worldnet.att.net...
"ES Kim" <no@spam.mail> wrote in message
news:c7**********@news1.kornet.net...
Is this code portable?


Yes, but it's yucky (that's a technical term).


YUCKY:

Your Ugly Code Kills You.

-Mike
Jul 22 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Tino | last post by:
Hopefully a simple question: What is the best/safest/fastest way to clear/make empty a std::priority_queue? Thanks, Ryan
10
by: Lasse Skyum | last post by:
We had a thread earlies about how to clear the capacity af a std::vector... how about this: std::vector<int> v; v.~vector<int>(); // destruct it (don't know if I need the...
18
by: Niels | last post by:
Hi group, I have some problems with clearing floated divs. My usual method works fine in Konqueror, but not in Firefox. Here's an example: <html><body> <div id='left' style='float:left;...
7
by: Martin Magnusson | last post by:
I'm having trouble clearing and resizing a static std::vector of std::vectors (segmentation fault). Is it OK to call clear() and resize() on a static attribute? My code is similar to the one posted...
9
by: lallous | last post by:
Hello, Given: class X { int x, y; // and many many more.... X() { memset((void*)this, 0, sizeof(X));
3
by: Sascha T. | last post by:
Hi! I didn't get the idea of exactly *how* such an array of vectors work. Consider this minimal code: -------------------- #include <vector> using namespace std;
8
by: Jack | last post by:
Hi, This code I inherited worked under VC7 now fails under VC8 with error: error C2664: 'memcpy' : cannot convert parameter 1 from 'std::_Vector_iterator<_Ty,_Alloc>' to 'void *' vector...
1
by: Gary Wessle | last post by:
hi I have a design problem with this code, it is suppose to take a space delimited data file file_info x( f ); //f is a text file provided below and return the column of choice vector<stringbb...
12
by: Avalon1178 | last post by:
Hi, I have an application that periodically uses a std::string variable which is assigned a VERY VERY large string (15000000+ bytes long). This application is essentially a daemon, and it polls...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.