473,396 Members | 1,799 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.

boost/scoped_ptr or new/delete

Hello,
At work we have someone who believes that using new&delete is a hideous
sin. He believes it should never be used. Instead scoped_ptr is the
enlightened way.
Does anyone have a view?
Jul 10 '07 #1
4 3415
none <""john\"@(none)"wrote in news:46940d23$0$31720$db0fefd9
@news.zen.co.uk:
Hello,
At work we have someone who believes that using new&delete is a hideous
sin. He believes it should never be used. Instead scoped_ptr is the
enlightened way.
Does anyone have a view?
Many people have a view.

Look up RAII (Resource Aquisition Is Initialization). Basically you want
to wrap up your resource management into classes to manage that for you.
That way when the class instance goes out of scope (by whatever means), the
resource is appropriately disposed of. Note that scoped_ptr isn't the only
class that does this... see also std::auto_ptr, std::tr1:shared_ptr, etc.
Jul 10 '07 #2
none wrote:
Hello,
At work we have someone who believes that using new&delete is a hideous
sin. He believes it should never be used. Instead scoped_ptr is the
enlightened way.
Does anyone have a view?
I think you confuse some things. Even when using scoped_ptr one has to use
new to get the memory in the first place (until C++0x with rvalue
references will provide for a way to have "perfect forwarding" functions
and then you can call "scoped_ptr_new<Class>(parameters)" instead of
the "new" expression).

I think what the person tells you is that you should not play with "blind
pointers" (as I call them) but instead always wrap them into something that
expresses the ownership semantics you wish to have over that allocated
object. For that you may use scoped_ptr, auto_ptr, shared_ptr and whatever
pointer wrapper you make yourself.

Instead of:
Class* ptr = new Class;

Do:
scoped_ptr<Classptr(new Class);

It provides for exception safe, less error prone code (you dont need to
worry about deleting anymore unless you cache a pointer/reference to that
object in code outside of that scope and you haven't released the ownership
of scoped_ptr).

See http://en.wikipedia.org/wiki/RAII or google about it (other people
prefer the term "Scope Bound Resource Management" considering RAII a bad
term for what it does).

--
Dizzy

Jul 11 '07 #3
On Tue, 10 Jul 2007 23:08:31 +0000 (UTC), Andre Kostur wrote:
>Look up RAII (Resource Aquisition Is Initialization). Basically you want
to wrap up your resource management into classes to manage that for you.
That way when the class instance goes out of scope (by whatever means), the
resource is appropriately disposed of. Note that scoped_ptr isn't the only
class that does this... see also std::auto_ptr, std::tr1:shared_ptr, etc.
The essence of RAII is that "allocation and deallocation disappear
from the surface level of your code"
(http://www.artima.com/intv/modern3.html). 'Smart pointers' do not
encapsulate allocation and are therefore not really suitable examples
of RAII.
--
Roland Pibinger
"The best software is simple, elegant, and full of drama" - Grady Booch
Jul 11 '07 #4
Dizzy <di***@roedu.netwrote:
See http://en.wikipedia.org/wiki/RAII or google about it (other people
prefer the term "Scope Bound Resource Management" considering RAII a bad
term for what it does).
Yes, even the FAQ mentions that RAII isn't the greatest name for it:

http://www.parashift.com/c++-faq-lit....html#faq-6.18

If you dissect the words of the RAII acronym (Resource Acquisition Is
Initialization), you will think RAII is about acquiring resources
during initialization. However the power of RAII comes not from
tying acquisition to initialization, but from tying reclamation to
destruction. A more precise acronym might be RRID (Resource
Reclamation Is Destruction), perhaps DIRR (Destruction Is Resource
Reclamation), but since so many people already understand RAII, using
it properly is far more important than complaining about the term.

--
Marcus Kwok
Replace 'invalid' with 'net' to reply
Jul 11 '07 #5

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

Similar topics

5
by: ctick | last post by:
Are there any advantages of using boost::shared_ptr other than auto_ptr from standard library?
8
by: Capstar | last post by:
Hi NG, I was looking through the code of boost::shared_ptr, and found the following piece of code in some referenced header file. As far as I understand it, it is used to delete the object the...
9
by: BekTek | last post by:
How do you think? and why?
16
by: Jeff Flinn | last post by:
At the risk of raising the OT ire of some here, I'd like to know what might be done to raise the awareness of the boost libraries. I've found boost and it's libraries invaluable in my work for ~5...
2
by: krema2ren | last post by:
Hi I've the following header problem that I need two classes to know each other through a boost::shared_ptr. Does any of you smart guys have a solution? A.h ---------------------- #include...
3
by: yinglcs | last post by:
I am reading the Boost scoped_ptr library, and I wonder what is the advantage of using that. Here is an example from Boost.org web site: #include <boost/scoped_ptr.hpp> #include <iostream> ...
1
by: deluded.soul | last post by:
Hi everyone, I am trying to use the boost multithreading library. I am having a problem as the join() function for the thread never returns. I am using a boolean variable to indicate when the...
2
by: kathy | last post by:
I have code: boost::scoped_ptr< CSpectrograph > m_pSpectrograph; .... m_pSpectrograph.reset( new CSpectrograph(init_data) ); .... But I found the CSpectrograph destructor never been...
9
by: Christopher | last post by:
If a method is declared to return a type boost::shared_ptr<sometype>, how can the method be changed to do the equivalent of returning NULL when it was declared to return a raw pointer?
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?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
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...

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.