473,396 Members | 2,057 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,396 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 2747
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? For example, if I have (ignore the fluff, it is...
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 introduce containers in xml documents even where not...
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 email discussion; i have posted them here with...
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 the container or whether it also gets deleted in...
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 non-const data members: class X { public: X(const...
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, but that's beside the point, for the most part. ...
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 origin of the error and it happened as I tried to...
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 unaware of. The problem, I see, with multisets is...
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 me know how to do it? #include...
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: 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: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
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...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
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,...

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.