472,338 Members | 1,697 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,338 software developers and data experts.

deleting all elements in a STL container of pointers

Can anyone see anything wrong with the following code?

(CONTAINER can be list, vector, set, ...)

template <class T> void Destroy(T * p) { delete p; }

void CleanUp(std::CONTAINER<ContainerType *> & Container)
{
std::foreach(Container.begin(), Container.end(),
Destroy<ContainerType>);
Container.clear();
}

Dec 17 '05 #1
5 2688
ed**********@gmail.com wrote:
Can anyone see anything wrong with the following code?

(CONTAINER can be list, vector, set, ...)

template <class T> void Destroy(T * p) { delete p; }

void CleanUp(std::CONTAINER<ContainerType *> & Container)
{
std::foreach(Container.begin(), Container.end(),
Destroy<ContainerType>);
Container.clear();
}

Sorry for my english,

In your Cleanup function, where do you define the formal parameter of
your template ?

To erase a item collection, you can use a functor.
Look my struct template 'Destroy', with this declaration (and definition).

template <typename T> struct Destroy {
void operator () (T pointer) {
std::cout << "Destroy of this pointer" << std::endl;
delete pointer;
pointer = 0;
}
};

Here, you must to define the type of your container :
The type is detected by the instantiation of your template.

template <typename Container> void CleanUp (Container & pContainer) {
std::for_each (
pContainer.begin (),
pContainer.end (),
Destroy<typename Container::value_type> ());
}

in your main function :

int main (int argc, char **argv) {
std::vector <std::string *> vect;
vect.push_back (new std::string ("Stephane"));
CleanUp (vect);
}

Best regards,

Stephane
Dec 17 '05 #2
ed**********@gmail.com wrote:
Can anyone see anything wrong with the following code?

(CONTAINER can be list, vector, set, ...)

template <class T> void Destroy(T * p) { delete p; }

void CleanUp(std::CONTAINER<ContainerType *> & Container)
{
std::foreach(Container.begin(), Container.end(),
Destroy<ContainerType>);
Container.clear();
}

About this subject, I have good advice for you.

Buy the book : "C++ Templates : The Complete Guide"
Product Details

* Hardcover: 552 pages
* Publisher: Addison-Wesley Professional; 1st edition (November 12,
2002)
* Language: English
* ISBN: 0201734842
* Product Dimensions: 9.5 x 7.5 x 1.2 inches
Dec 17 '05 #3
I'd also add Scott Meyers' "Effective STL" and Item 7.

Cheers
--
Mateusz Loskot
http://mateusz.loskot.net

Dec 17 '05 #4

ed**********@gmail.com wrote:
Can anyone see anything wrong with the following code?

(CONTAINER can be list, vector, set, ...)

template <class T> void Destroy(T * p) { delete p; }

void CleanUp(std::CONTAINER<ContainerType *> & Container)
{
std::foreach(Container.begin(), Container.end(),
Destroy<ContainerType>);
Container.clear();
}


I recommend you use smart pointers in your container instead of raw
pointers.
If you use smart pointers, then you don't have to worry about
explicitly deleting the pointers, because the smart pointer will do
that for you.
You can use the boost::shared_ptr or clone smart pointers like copy_ptr
and cow_ptr.

http://www.boost.org/libs/smart_ptr/shared_ptr.htm

http://code.axter.com/copy_ptr.h
http://code.axter.com/cow_ptr.h

Example usage:
std::vector<boost::shared_ptr<foo> > vFoo;

std::vector<copy_ptr<foo> > vFoo;

Dec 18 '05 #5

ed**********@gmail.com wrote:
Can anyone see anything wrong with the following code?

(CONTAINER can be list, vector, set, ...)

template <class T> void Destroy(T * p) { delete p; }

void CleanUp(std::CONTAINER<ContainerType *> & Container)
{
std::foreach(Container.begin(), Container.end(),
Destroy<ContainerType>);
Container.clear();
}


The problem is that there can be a problem using Destroy<ContainerType>
as a template function. Although you can use a function in this
situation, it doesn't work with template functions.

So you should use a functor as other posters have suggested.

Dec 19 '05 #6

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

Similar topics

7
by: Xamalek | last post by:
Greetings, I have a question. When using an STL container, does deleting a container of pointers also call delete on the (contained) pointers? ...
1
by: Wolfgang Lipp | last post by:
my question is: do we need container elements for repeating elements in data-centric xml documents? or is it for some reason very advisable to...
0
by: Wolfgang Lipp | last post by:
From: Lipp, Wolfgang Sent: Tuesday, 27?January?2004 13:26 <annotation> the first eleven contributions in this thread started as an off-list...
8
by: Generic Usenet Account | last post by:
To settle the dispute regarding what happens when an "erase" method is invoked on an STL container (i.e. whether the element is merely removed from...
8
by: Nobody | last post by:
The requirement that STL container elements have to be assignable is causing me a problem. Consider a class X which contains both const and...
9
by: Aguilar, James | last post by:
Hey guys. A new question: I want to use an STL libarary to hold a bunch of objects I create. Actually, it will hold references to the objects,...
5
by: FefeOxy | last post by:
Hi, > I'm having a debug assertion error within the file dbgdel.cpp with the expression: _BLOCK_TYPE_IS_VALID(pHead->nBlockUse) I traced the...
1
by: Varun Kacholia | last post by:
I apologize if there exists a standard way of deleting multiple elements from a STL hash_multiset (or even multiset for that matter) that I am...
14
by: PengYu.UT | last post by:
In the following program, I want an iterator contain pointer pointing to constant object not const pointer. If it is possible would you please let...
0
better678
by: better678 | last post by:
Question: Discuss your understanding of the Java platform. Is the statement "Java is interpreted" correct? Answer: Java is an object-oriented...
0
by: CD Tom | last post by:
This happens in runtime 2013 and 2016. When a report is run and then closed a toolbar shows up and the only way to get it to go away is to right...
0
by: CD Tom | last post by:
This only shows up in access runtime. When a user select a report from my report menu when they close the report they get a menu I've called Add-ins...
0
jalbright99669
by: jalbright99669 | last post by:
Am having a bit of a time with URL Rewrite. I need to incorporate http to https redirect with a reverse proxy. I have the URL Rewrite rules made...
0
by: antdb | last post by:
Ⅰ. Advantage of AntDB: hyper-convergence + streaming processing engine In the overall architecture, a new "hyper-convergence" concept was...
0
by: Matthew3360 | last post by:
Hi there. I have been struggling to find out how to use a variable as my location in my header redirect function. Here is my code. ...
0
by: AndyPSV | last post by:
HOW CAN I CREATE AN AI with an .executable file that would suck all files in the folder and on my computerHOW CAN I CREATE AN AI with an .executable...
0
by: Arjunsri | last post by:
I have a Redshift database that I need to use as an import data source. I have configured the DSN connection using the server, port, database, and...
0
hi
by: WisdomUfot | last post by:
It's an interesting question you've got about how Gmail hides the HTTP referrer when a link in an email is clicked. While I don't have the specific...

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.