By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
426,179 Members | 2,175 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 426,179 IT Pros & Developers. It's quick & easy.

boost/scoped_ptr or new/delete

P: n/a
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
Share this Question
Share on Google+
4 Replies


P: n/a
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

P: n/a
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

P: n/a
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

P: n/a
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 discussion thread is closed

Replies have been disabled for this discussion.