DevMaster.net Forums
[[ Home | Forums | 3D Engines Database | Wiki | Articles/Tutorials | Game Dev Jobs | IRC Chat Network | Contact Us ]]

Go Back   DevMaster.net Forums > Site Discussions > Code & Snapshot Discussion
User Name
Password
Register FAQ Members List Search Today's Posts Mark Forums Read

Reply
 
Thread Tools Search this Thread Display Modes
Old 07-29-2003, 07:00 AM   #1
DrunkenCoder
Member
 
Join Date: Jul 2003
Posts: 97
Talking

How often have you written some thing like this:
Code:
for( ContainerType::iterator itr = c.begin(); itr != c.end(); ++itr) { delete *itr; }

shouldn't there be a easier more explicit way? of course there is,
using std::for_each and a simple functor we get this:

Code:
//some_header.h struct delete_object { template <typename T> void operator()(T *ptr){ delete ptr;} }; //later in the code... std::for_each( c.begin(), c.end(), delete_object());

ahhh, doesn't that feel nice? less to type extra clarity *and* it could actually
give you a speed gain becuase c.end() isn't recalculated oh and that it's less
error prone is just an added bonus =)
DrunkenCoder is offline   Reply With Quote
Old 07-29-2003, 07:02 AM   #2
davepermen
Senior Member
 
davepermen's Avatar
 
Join Date: Jan 2003
Location: Switzerland
Posts: 1,333
Default

and never forget, boost::checked_delete or boost::checked_deleter (spelling..) does exactly that..

but yes, its great for container cleanup..
___________________________________________
davepermen.net
-Loving a Person is having the wish to see this Person happy, no matter what that means to yourself.
-No matter what it means to myself....
davepermen is offline   Reply With Quote
Old 07-29-2003, 07:09 AM   #3
DrunkenCoder
Member
 
Join Date: Jul 2003
Posts: 97
Talking

I hadn't had the pleasure to use boost much but I really should have a look at it one of theese days.

For a "safe delete" you could use this
Code:
struct safe_delete { template <typename T> void operator()(T*& p) { if( p) { delete p; p = 0; } };

strictly speaking you should actually never need to test p against NULL because according to the standard delete NULL is safe (as is free(NULL); ) but some compilers on small platforms are known to get this wrong and do funny things
when trying to delete a NULL pointer.
DrunkenCoder is offline   Reply With Quote
Old 07-29-2003, 07:13 AM   #4
davepermen
Senior Member
 
davepermen's Avatar
 
Join Date: Jan 2003
Location: Switzerland
Posts: 1,333
Default

yep, its useless, the if(p) delete p. but on stupid compilers useless stuff gets quite important sometimes

like #define for if(0) {} else for

and similar stuff
___________________________________________
davepermen.net
-Loving a Person is having the wish to see this Person happy, no matter what that means to yourself.
-No matter what it means to myself....
davepermen is offline   Reply With Quote
Old 07-29-2003, 07:18 AM   #5
DrunkenCoder
Member
 
Join Date: Jul 2003
Posts: 97
Talking

Quote:
Originally Posted by davepermen
like #define for if(0) {} else for
Well you got to love M$VC++ 6.0 broken scope rules
DrunkenCoder is offline   Reply With Quote
Old 07-29-2003, 07:25 AM   #6
davepermen
Senior Member
 
davepermen's Avatar
 
Join Date: Jan 2003
Location: Switzerland
Posts: 1,333
Default

yeah, good old days
___________________________________________
davepermen.net
-Loving a Person is having the wish to see this Person happy, no matter what that means to yourself.
-No matter what it means to myself....
davepermen is offline   Reply With Quote
Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Forum Jump


All times are GMT -7. The time now is 07:44 AM.


Powered by vBulletin
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.