473,320 Members | 1,839 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,320 software developers and data experts.

deallocating vector storage

In TC++PL (third edition), Section 16.3.8, Stroustrup gives the
following as a way to give memory from a std::vector back to the
system:

vector<int> tmp = v; // v.size() == 0
v.swap( tmp ); // v now has capacity of tmp (default), extra capacity
returned to system

We need to have a general method that returns memory back to the
system for vectors which are no longer needed, but have not yet gone
out of scope. Are there alternative suggestions for accomplishing
this?

Regards,
Ryan
Jul 22 '05 #1
3 1926
Tino wrote:
In TC++PL (third edition), Section 16.3.8, Stroustrup gives the
following as a way to give memory from a std::vector back to the
system:

vector<int> tmp = v; // v.size() == 0
v.swap( tmp ); // v now has capacity of tmp (default), extra capacity
returned to system

We need to have a general method that returns memory back to the
system for vectors which are no longer needed, but have not yet gone
out of scope. Are there alternative suggestions for accomplishing
this?


There is no guarantee that the storage will be returned. Swapping with
an empty vector, clearing the vector, resizing it to 0, all are good
attempts to accomplish what you need, but the Standard makes no promises
WRT the memory allocation behaviour. It's all platform-dependent and
implementation-dependent, I believe.

V
Jul 22 '05 #2
Tino wrote:
In TC++PL (third edition), Section 16.3.8, Stroustrup gives the
following as a way to give memory from a std::vector back to the
system:

vector<int> tmp = v; // v.size() == 0
v.swap( tmp ); // v now has capacity of tmp (default), extra capacity
returned to system
Why I do not see this in 16.3.8?

The above has no real effect (it exchanges emptiness) and no memory is
returned to the system.
We need to have a general method that returns memory back to the
system for vectors which are no longer needed, but have not yet gone
out of scope. Are there alternative suggestions for accomplishing
this?

Yes. The erase(), clear(), resize(), pop_back() member functions.


Regards,

Ioannis Vranos
Jul 22 '05 #3
Ioannis Vranos wrote:
We need to have a general method that returns memory back to the
system for vectors which are no longer needed, but have not yet gone
out of scope. Are there alternative suggestions for accomplishing
this?


Yes. The erase(), clear(), resize(), pop_back() member functions.


And if you mean to get rid of the container itself, you can make it on
the free store, however the whole thing sounds like a bad design. Why
you would not need a vector till the end of the scope? Perhaps the scope
is larger than it should.


Regards,

Ioannis Vranos
Jul 22 '05 #4

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

Similar topics

5
by: john smith | last post by:
HI, when I try the following code, I get a segfault when compiled with VC.NET and g++ under cygwin. #1 vector<int> vi; #2 vector<int>::iterator ii = vi.begin(); #3 vi.reserve(10); #4 ...
3
by: Alexandros | last post by:
Hi. How can I create a vector<bool> efficiently from a char* or a vector<char> ? For example, if char* c == (8,10) I want vector<bool> v to be: (0000100000001010)
5
by: Kenneth W Del Signore | last post by:
Hi, I'm working on a scheme to store several millions of records (Subdata), each of which contains a variable number of sub records (Celldata). My concern is that my prototype uses a lot more...
3
by: Rakesh Sinha | last post by:
This is about the vector template defined in standard C++ . Suppose I want to create a *huge* vector , as follows. void f1() { vector < int > data(1024); //may be not very huge, but for...
8
by: ma740988 | last post by:
Consider this statement in Excel's text Thinking in C++, Vol 2: /// 1 " A vector starts by grabbing a block of storage, as if it's taking a guess at how many objects you plan to put into it. ...
9
by: aaragon | last post by:
I am trying to create a vector of type T and everything goes fine until I try to iterate over it. For some reason, the compiler gives me an error when I declare std::vector<T>::iterator iter;...
11
by: mathieu | last post by:
Hello, I would like to implement a 'vector<uint12_t>' structure, where uint12_t is a 12bits unsigned integer. AFAIK I need to completely duplicate the implementation of let say vector<booland...
9
by: Jess | last post by:
Hello, I tried to clear a vector "v" using "v.clear()". If "v" contains those objects that are non-built-in (e.g. string), then "clear()" can indeed remove all contents. However, if "v"...
7
by: anonymous | last post by:
I have a pretty basic class, "Cube", that has a COM pointer member. The default constructor initializes this pointer using the correct API functions, the copy constructor copies the pointer and calls...
0
by: DolphinDB | last post by:
The formulas of 101 quantitative trading alphas used by WorldQuant were presented in the paper 101 Formulaic Alphas. However, some formulas are complex, leading to challenges in calculation. Take...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
1
by: Shællîpôpï 09 | last post by:
If u are using a keypad phone, how do u turn on JavaScript, to access features like WhatsApp, Facebook, Instagram....
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

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.