🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

2 total messages Started by Nico Josuttis Thu, 18 Apr 1996 16:42
auto_ptr problem?: delete or delete[]
#4103
Author: Nico Josuttis
Date: Thu, 18 Apr 1996 16:42
28 lines
1822 bytes
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 
]
Re: auto_ptr problem?: delete or delete[]
#4111
Author: Dietmar Kuehl
Date: Fri, 19 Apr 1996 14:48
68 lines
3662 bytes
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                             ]
Thread Navigation

This is a paginated view of messages in the thread with full content displayed inline.

Messages are displayed in chronological order, with the original post highlighted in green.

Use pagination controls to navigate through all messages in large threads.

Back to All Threads