Click here to Skip to main content
Click here to Skip to main content
Add your own
alternative version

A Functor that deletes Pointers from STL Sequence Containers

, 21 Mar 2004 BSD
A policy based deletion functor that can be used with for_each function.
freeptrfnc_demo.zip
freeptrfnc_src.zip
#include <vector>
#include <algorithm>
#include "FreePtr.h"
using namespace std;
using namespace FreePtr;


class C
    {
    char* data_;
    public:
        C()
            {
            data_ = new char[512];
            }
        ~C()
            {
            delete[] data_;
            }
    };



int main ()
    {
    vector <C*> objects;
    for (int i = 0; i < 20; ++i)
        objects.push_back(new C());
    for_each(objects.begin(), objects.end(), free_ptr<C>());
    objects.clear();

    for (int i = 0; i < 20; ++i)
        objects.push_back(new C[10]);
    for_each(objects.begin(), objects.end(), free_ptr<C, DeleteArray, ZeroPtr>());
    objects.clear();

    for (int i = 0; i < 20; ++i)
        objects.push_back((C*)malloc(sizeof(C)));
    for_each(objects.begin(), objects.end(), free_ptr<C, Free>());
    objects.clear();
    }

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The BSD License

Share

About the Author

Nemanja Trifunovic
Software Developer (Senior) SAP
United States United States
Born in Kragujevac, Serbia. Now lives in Boston area with his wife and daughters.
 
Wrote his first program at the age of 13 on a Sinclair Spectrum, became a professional software developer after he graduated.
 
Very passionate about programming and software development in general.

| Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.141220.1 | Last Updated 22 Mar 2004
Article Copyright 2004 by Nemanja Trifunovic
Everything else Copyright © CodeProject, 1999-2014
Layout: fixed | fluid