|
|
Up |
|
|
  |
Date: Jul 10, 2007 15:50
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?
|
| |
|
| | 6 Comments |
|
  |
Author: Andre KosturAndre Kostur Date: Jul 10, 2007 16:08
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.
|
| |
|
| | no comments |
|
  |
Author: DizzyDizzy Date: Jul 11, 2007 02:59
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(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;
|
| Show full article (1.35Kb) |
| no comments |
|
  |
Author: Roland PibingerRoland Pibinger Date: Jul 11, 2007 05:53
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
|
| |
| no comments |
|
  |
Author: James KanzeJames Kanze Date: Jul 11, 2007 07:30
On Jul 11, 12:50 am, none <""john\"@(none)"> wrote:
> 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.
--
James Kanze (GABI Software) email:james.kanze@ gmail.com
Conseils en informatique orient
|
| |
| no comments |
|
  |
Author: DizzyDizzy Date: Jul 11, 2007 08:29
James Kanze wrote:
> On Jul 11, 12:50 am, none <""john\"@(none)"> wrote:
>
>> 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...
|
| Show full article (1.33Kb) |
| no comments |
|
  |
|
|
  |
Author: Marcus KwokMarcus Kwok Date: Jul 11, 2007 08:25
Dizzy roedu.net> wrote:
> 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.
--
Marcus Kwok
Replace 'invalid' with 'net' to reply
|
| |
| no comments |
|
RELATED THREADS |
  |
|
|
|