boost/scoped_ptr or new/delete

N

none

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?
 
A

Andre Kostur

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.
 
D

Dizzy

none said:
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<Class> ptr(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).
 
R

Roland Pibinger

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.
 
J

James Kanze

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?

There's no silver bullet. Scoped_ptr only works when object
lifetime corresponds to some scope. Shared_ptr is also useful
for some short lived objects that you pass around, but that you
cannot easily copy (typically because they have to be
polymorphic). I use auto_ptr a lot to transfer ownership
between threads, but that's a special case. In most
applications, however, most pointers will be just your classical
T*, since most of the objects you will be allocating dynamically
will be entity objects, with application determined lifetimes.
 
M

Marcus Kwok

Dizzy said:
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-lite/big-picture.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.
 
D

Dizzy

James said:
There's no silver bullet. Scoped_ptr only works when object
lifetime corresponds to some scope. Shared_ptr is also useful
for some short lived objects that you pass around, but that you
cannot easily copy (typically because they have to be
polymorphic). I use auto_ptr a lot to transfer ownership
between threads, but that's a special case. In most
applications, however, most pointers will be just your classical
T*, since most of the objects you will be allocating dynamically
will be entity objects, with application determined lifetimes.

I believe than when you cannot even bind the lifetime semantics to an
existent wrapper such as auto_ptr or shared_ptr you should still wrap them
into a smart pointer that at least doesn't have the ++/--/+/- operators and
that is always initialized (to 0 at least) and so on to reduce from the
functionality of a normal pointer that you don't need and as such is error
prone. Normal C++ pointers are just too rich semantically to make sense to
ever use them other than low-level code IMO.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,767
Messages
2,569,572
Members
45,045
Latest member
DRCM

Latest Threads

Top