Using for_each() to deallocate memory

Discussion in 'C++' started by Eric Lilja, Mar 14, 2005.

  1. Eric Lilja

    Eric Lilja Guest

    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
    Eric Lilja, Mar 14, 2005
    #1
    1. Advertising

  2. Eric Lilja

    msalters Guest

    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
    msalters, Mar 14, 2005
    #2
    1. Advertising

  3. 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::eek:stream_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::eek:stream_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::eek:stream_iterator<my_class*>(std::cout, " "));
    std::cout << std::endl;
    }
    -------------------------------

    V
    Victor Bazarov, Mar 14, 2005
    #3
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    20
    Views:
    1,349
    Heiko Wundram
    May 12, 2006
  2. Tony Johansson
    Replies:
    3
    Views:
    319
    Old Wolf
    Aug 13, 2005
  3. Replies:
    2
    Views:
    340
    Ron Natalie
    Apr 3, 2008
  4. pereges
    Replies:
    6
    Views:
    299
    santosh
    Jul 7, 2008
  5. alessio211734

    handle deallocate memory on return

    alessio211734, Jun 30, 2009, in forum: C++
    Replies:
    3
    Views:
    558
    Thomas J. Gritzan
    Jul 3, 2009
Loading...

Share This Page