473,411 Members | 2,285 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,411 software developers and data experts.

Does list.clear() delete the objects in the list? If not, what is the correct method?

I had a question about memory management. Please look at the two
functions below. Can you answer the two questions in the comments
below. Thanks so much.

-Mike
func1(std::vector<Point>& points)
{
points.clear(); // Is this the right way to deallocate
// memory allocated below? If not, how
// do I delete the object allocated below

...
points.push_back(Point(1,2)); //The Point object will on the heap
right?
...
}
func2()
{
vector<Point> points;

while(some condition)
{
..
func1(points);
..
}
}

Point::Point(const float& new_x, const float& new_y)
:x(new_x),
y(new_y)
{}
Jul 22 '05 #1
1 2569

"Michael Jasn" <Te*******@hotpop.com> wrote in message
news:99**************************@posting.google.c om...
I had a question about memory management. Please look at the two
functions below. Can you answer the two questions in the comments
below. Thanks so much.

-Mike
func1(std::vector<Point>& points)
{
points.clear(); // Is this the right way to deallocate
// memory allocated below? If not, how
// do I delete the object allocated below
There is no memory allocated below. The method clear() will resize the
vector to zero. Whether this involves deallocating any memory is
implementation defined.

...
points.push_back(Point(1,2)); //The Point object will on the heap
right?


No, the Point object will be in the vector. It will be a copy of the Point
object passed to push_back. This operation may or may not require memory
allocation but this isn't your concern, only vector needs to know this.

The important point is that you have no memory leak, any memory allocated by
vector will be freed eventually. If you want to resize a vector to zero and
free any memory that it is holding then there is no guaranteed way to do
that, but the following trick is reasonably sure to work

vector<Point>().swap(points);

This creates a vector using the default constructor and swaps it with
points. If the default constructor allocates no memory (which is a
reasonable assumption) then after this operation points will be a zero size
vector with no memory allocated.

john
Jul 22 '05 #2

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

Similar topics

12
by: Fred Pacquier | last post by:
First off, sorry for this message-in-a-bottle-like post... I haven't been able to phrase my questions well enough to get a meaningful answer from Google in my research. OTOH, it is standard...
2
by: dasod | last post by:
I would like to know if my method to remove list objects is correct in this small test program. It seems to me that there might be a simplier way, but I'm afraid I don't know enough about list...
17
by: pub | last post by:
When creating a list: list<class A*> l; How to delete all the objects whose pointers are contained in "l"? Thanks for your comments?
5
by: Noozer | last post by:
I have two Classes in use in an ASP application... The first Class is named "ORDER". It represents one job order. It has a Delete method, among others, which deletes it from my database. The...
77
by: Ville Vainio | last post by:
I tried to clear a list today (which I do rather rarely, considering that just doing l = works most of the time) and was shocked, SHOCKED to notice that there is no clear() method. Dicts have it,...
14
by: Anoop | last post by:
Hi, I am new to this newsgroup and need help in the following questions. 1. I am workin' on a GUI application. Does C# provides Layout Managers the way Java does to design GUI? I know that it...
5
by: mkaushik | last post by:
Hi everyone, Im just starting out with C++, and am curious to know how "delete <pointer>", knows about the number of memory locations to free. I read somewhere that delete frees up space...
19
by: Angus | last post by:
I have a socket class CTestClientSocket which I am using to simulate load testing. I create multiple instances of the client like this: for (int i = 0; i < 5; i++) { CTestClientSocket* pTemp...
2
by: phiefer3 | last post by:
Ok, first of all I'm not sure if this is the correct forum for this question or not. But hopefully someone can help me or at least point me in the direction of the forum this belongs. First of...
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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
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
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
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.