Topic: auto_ptr problem?: delete or delete[]


Author: willer@carolian.com (Steve Willer)
Date: 1996/04/23
Raw View
Alexander Krotoff <krotoff@boy.nmd.msu.ru> wrote:

>Yes, It's realy funny becouse I'll prefere to use array intstead
>of container class in several cases. In this case I'll prefer
>to have something like auto_array_ptr (for exception safety and
>for i_forgot_to_delete safety too).

Would you mind telling me which cases? I can't figure out a reason why
someone would want to use built-in arrays rather than a vector (except
because it's new and not all compilers support it).

>Probably my English is not clear enough. I have not said that I am
>expecting solution of multitude of memory problems from the C++ templates.
>Neither garbage collection, even nor memory leaks. I just saw another
>examples of auto_ptr usage in anonther thread.

You seemed to be expecting the auto_ptr to handle arrays as well. I was
saying that auto_ptr is only intended to solve a small number of problems.
If you want other pointers, then either lobby to get them in the standard
library or write them yourself (or use 'vector').


[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Alexander Krotoff <krotoff@boy.nmd.msu.ru>
Date: 1996/04/21
Raw View
In article <199604181455.QAA12877@bredex.bredex.de> you wrote:
> In a tutorial a student asked a very good question i think:
> How can auto_ptr imagine which delete to use.
> Consider:
>  int* p = new int;
> and
>  int* p = new int[10];
> should result in different delete operations.
> But if I let auto_ptr do the job, how does it know
> which one to use?

Reading WP I was confused too. Really, we need auto_ptr for array
of objects more often, than auto_ptr for single object. In the last
case we are able to use simple auto object instead of dynamic
allocation and closely link allocated object with the automatic
object (of auto_ptr type).

Yes, I know there are several applications of auto_ptr which cannot
be solved just by auto objects...

Probably somebody from ANSI committee library WG may clean the situation.

--
Alexander N. Krotoff  krotoff@such.srcc.msu.su
Research Computer Center tel: +7(095)939-2638
Moscow State University  fax: +7(095)939-4430


[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: "Nathan Myers <ncm@cantrip.org>" <ncm@cantrip.org>
Date: 1996/04/22
Raw View
Alexander Krotoff wrote:
>
> In article <199604181455.QAA12877@bredex.bredex.de> you wrote:
> > How can auto_ptr imagine which delete to use.
> > Consider:
> >  int* p = new int;
> > and
> >  int* p = new int[10];
> > should result in different delete operations.

> Reading WP I was confused too. ...
> Yes, I know there are several applications of auto_ptr which cannot
> be solved just by auto objects...
>
> Probably somebody from ANSI committee library WG may clean the situation.

Any changes in this area look extremely unlikely, judging from
experience at the last few meetings.

Nathan Myers
ncm@cantrip.org  http://www.cantrip.org/


[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: willer@carolian.com (Steve Willer)
Date: 1996/04/22
Raw View
Alexander Krotoff <krotoff@boy.nmd.msu.ru> wrote:

>Reading WP I was confused too. Really, we need auto_ptr for array
>of objects more often, than auto_ptr for single object. In the last
>case we are able to use simple auto object instead of dynamic
>allocation and closely link allocated object with the automatic
>object (of auto_ptr type).

It's funny, because I don't see it that way at all. If I want an array of
stuff, I will use the 'vector' or some other container object. If I want
low-cost control of single objects, I use auto_ptr. It's just that simple --
like apples and oranges.

>Yes, I know there are several applications of auto_ptr which cannot
>be solved just by auto objects...

I think it's simply a mistake to think of auto_ptr as representing a solution
to a multitude of memory problems. As someone else said in another thread, the
only smart pointer that fixes many problems is a garbage-collecting pointer.
The auto_ptr is intended as a solution to a small number of problems with
writing exception-safe and non-memory-leaking code -- that's it.
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: Alexander Krotoff <krotoff@boy.nmd.msu.ru>
Date: 1996/04/23
Raw View
In article <m0uBFlF-000OjzC@sqarc.sq.com> Steve Willer wrote:
> Alexander Krotoff <krotoff@boy.nmd.msu.ru> wrote:

> >Reading WP I was confused too. Really, we need auto_ptr for array
> >of objects more often, than auto_ptr for single object. In the last
> >case we are able to use simple auto object instead of dynamic
> >allocation and closely link allocated object with the automatic
> >object (of auto_ptr type).

> It's funny, because I don't see it that way at all. If I want an array of
> stuff, I will use the 'vector' or some other container object. If I want
> low-cost control of single objects, I use auto_ptr. It's just that simple --
> like apples and oranges.

Yes, It's realy funny becouse I'll prefere to use array intstead
of container class in several cases. In this case I'll prefer
to have something like auto_array_ptr (for exception safety and
for i_forgot_to_delete safety too).

Of course, using vector class is good way, too.

> >Yes, I know there are several applications of auto_ptr which cannot
> >be solved just by auto objects...

> I think it's simply a mistake to think of auto_ptr as representing a
> solution to a multitude of memory problems. As someone else said in
> another thread, the only smart pointer that fixes many problems is a
> garbage-collecting pointer.  The auto_ptr is intended as a solution to
> a small number of problems with writing exception-safe and
> non-memory-leaking code -- that's it.

Probably my English is not clear enough. I have not said that I am
expecting solution of multitude of memory problems from the C++ templates.
Neither garbage collection, even nor memory leaks. I just saw another
examples of auto_ptr usage in anonther thread.

--
Alexander
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]





Author: Nico Josuttis <nico@bredex.de>
Date: 1996/04/18
Raw View
In a tutorial a student asked a very good question i think:
How can auto_ptr imagine which delete to use.
Consider:
 int* p = new int;
and
 int* p = new int[10];
should result in different delete operations.
But if I let auto_ptr do the job, how does it know
which one to use?

Isn't it a BIG design problem ?
Reading the DWP it calls delete (without [])
what would be wrong in many cases since
using new for arrays is probaly made very often.

I fear we need two kind of auto_ptr, don't we?
--
Nico                             address: BREDEX GmbH, Nicolai Josuttis
email:   nico@bredex.de                   Fallersleber-Tor-Wall 23
phone:   +49 531 24330-0                  D-38100 Braunschweig
fax:     +49 531 24330-99                 Germany
---
[ comp.std.c++ is moderated.  To submit articles: Try just posting with your
                newsreader.  If that fails, use mailto:std-c++@ncar.ucar.edu
  comp.std.c++ FAQ: http://reality.sgi.com/austern/std-c++/faq.html
  Moderation policy: http://reality.sgi.com/austern/std-c++/policy.html
  Comments? mailto:std-c++-request@ncar.ucar.edu
]





Author: kuehl@uzwil.informatik.uni-konstanz.de (Dietmar Kuehl)
Date: 1996/04/19
Raw View
Hi,

Nico Josuttis (nico@bredex.de) wrote:
: In a tutorial a student asked a very good question i think:
: How can auto_ptr imagine which delete to use.

This is simple: 'auto_ptr' is to be used for non-array objects only.
The requirements for the pointer 'p' stored in an 'auto_ptr' is, that
you can apply 'delete p' (see lib.auto.ptr.cons).

: Consider:
:  int* p = new int;
: and
:  int* p = new int[10];
: should result in different delete operations.
: But if I let auto_ptr do the job, how does it know
: which one to use?

It knows because you are supposed to guarantee that you don't store a
pointer to an array object in the 'auto_ptr'.

: Isn't it a BIG design problem ?

Not really. You are not supposed to do many allocations of array
objects anyway: Use 'vector' instead. It is, however, a security
problem: A user might get trapped into the idea that an 'auto_ptr' can
be used for both array and non-array object. Especially, as there is no
equivalent for array objects (e.g. 'auto_array_ptr'). BTW, note that
the access methods for 'auto_ptr' are not very well suited to be used
for arrays:  E.g. the subscript operator is missing rendering the use
of 'auto_ptr' for array objects useless.

: Reading the DWP it calls delete (without [])
: what would be wrong in many cases since
: using new for arrays is probaly made very often.

Probably not, as there are several aspects which render newing built-in
arrays as quite dangerous. A much better alternative is using 'vector'
(or some other array class where 'vector' is not suitable).

: I fear we need two kind of auto_ptr, don't we?

If there are more than one kind of 'auto_ptr' I insist on having at
least four kinds of 'auto_ptr', i.e. for

  - non-array objects allocated with 'new T(...)'
  - array objects allocated with 'new T[]'
  - raw memory objects allocated with 'operator new()'
  - raw memory array objects allocated with 'operator new[]()'

I guess there are much more. You might want to have a look at the
article I wrote to the thread "auto_ptr problems" in
comp.lang.c++.moderated: There I present an implementation of
'auto_ptr' which makes use of a framework where both the resources to
be managed (the pointer types I mentioned above plus resources like
files, locks, windows, etc.) and the style of the management (like for
'auto_ptr', reference counting, and other styles) can be varied
independently.
--
dietmar.kuehl@uni-konstanz.de
http://www.informatik.uni-konstanz.de/~kuehl/
I am a realistic optimist - that's why I appear to be slightly pessimistic
---
[ comp.std.c++ is moderated.  To submit articles: try just posting with      ]
[ your news-reader.  If that fails, use mailto:std-c++@ncar.ucar.edu         ]
[ FAQ:      http://reality.sgi.com/employees/austern_mti/std-c++/faq.html    ]
[ Policy:   http://reality.sgi.com/employees/austern_mti/std-c++/policy.html ]
[ Comments? mailto:std-c++-request@ncar.ucar.edu                             ]