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

Using for_each() to deallocate memory

Hello, I have a std::vector storing pointers to objects that are dynamically
allocated. When I am done with the vector I want to delete all pointers. Can
I use std::for_each() or something to do that in one line? I am so tired of
seeing the following in my code:

for(vector<some_type>::size_type i = 0; i < myvec.size(); ++i)
do_something_with_each_element;

/ Eric
Jul 23 '05 #1
2 1744

Eric Lilja wrote:
Hello, I have a std::vector storing pointers to objects that are dynamically allocated. When I am done with the vector I want to delete all pointers. Can I use std::for_each() or something to do that in one line?


Yes - assuimg you have

class deleter_ {
template< typename T > operator()(T* t) { delete t; }
} deleter;

for_each( begin, end, deleter );

Of course, if you try this on int you'll get a message which tells
you that deleter(int) doesn't exist.

HTH,
Michiel Salters

Jul 23 '05 #2
Eric Lilja wrote:
Hello, I have a std::vector storing pointers to objects that are dynamically
allocated. When I am done with the vector I want to delete all pointers. Can
I use std::for_each() or something to do that in one line? I am so tired of
seeing the following in my code:

for(vector<some_type>::size_type i = 0; i < myvec.size(); ++i)
do_something_with_each_element;


Try it, why doncha?
-------------------------------
#include <vector>
#include <algorithm>
#include <iostream>
#include <iterator>

struct my_class {};

struct my_alloc {
void operator()(my_class*& p) { p = new my_class; }
};

struct my_dealloc {
void operator()(my_class*& p) { delete p; p = 0; }
};

int main() {
std::vector<my_class*> v(5, (my_class*)0);
std::copy(v.begin(), v.end(),
std::ostream_iterator<my_class*>(std::cout, " "));
std::cout << std::endl;
std::for_each(v.begin(), v.end(), my_alloc());
std::copy(v.begin(), v.end(),
std::ostream_iterator<my_class*>(std::cout, " "));
std::cout << std::endl;
std::for_each(v.begin(), v.end(), my_dealloc());
std::copy(v.begin(), v.end(),
std::ostream_iterator<my_class*>(std::cout, " "));
std::cout << std::endl;
}
-------------------------------

V
Jul 23 '05 #3

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

Similar topics

40
by: Elijah Bailey | last post by:
I want to sort a set of records using STL's sort() function, but dont see an easy way to do it. I have a char *data; which has size mn bytes where m is size of the record and n is the...
25
by: rokia | last post by:
in a project, I use many,many stl such as stack,list,vctor etc. somewhere the vector's size is more than 2K. is this a efficient way?
10
by: gogogo_1001 | last post by:
Dear all, I don't understand why "delete" works well on destructing a object, but fails to destruct a vector of it. Any of your comment is highly appreciated! Following is the program...
20
by: mariano.difelice | last post by:
Hi, I've a big memory problem with my application. First, an example: If I write: a = range(500*1024) I see that python process allocate approximately 80Mb of memory.
3
by: PolkaHead | last post by:
I was wondering if there's a way to traverse a two-dimensional vector (vector of vectors) with a nested for_each call. The code included traverses the "outer" vector with a for_each, than it...
15
by: Eric Lilja | last post by:
Hello, in one of my classes I have this member variable: Tile ***tiles_; where Tile is another one of my classes. dimensional array of Tile-pointers (dimensions are of equal sizes). Last, I...
13
by: Sarath | last post by:
What's the advantage of using for_each than the normal iteration using for loop? Is there any 'tweak' to use stream objects as 'Function' parameter of for_each loop. Seems copy function can do...
8
by: Chris Portka | last post by:
I need to be able to allocate large numbers of elements at a time but then delete them one at a time. I have settled on the design of using new to allocate many items at once, but am unsure what...
5
by: Anonymous | last post by:
I am writing memory allocation class policies for a template class. I have defined one allocated like this: template <class T> struct myMallocAllocator { static T* Allocate() { void * ptr...
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...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...
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: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
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...

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.