🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.std.c++
Article #2104

Re: Why not op delete(void* v&,size_t);?

#2104
From: Fergus Henderson
Date: Tue, 03 Oct 1995 18:18
80 lines
3518 bytes
akv@marsh.cacs.usl.edu (Anil Kumar Vijendran) writes:

>>>>>> vandevod@cs.rpi.edu (David Vandevoorde) said:
>    David> 	X* const xp = new X; // ...  delete xp; // Or is this
>    David> illegal anyway? I suspect not.
>
>I don't understand how "delete xp" would be true. Because delete takes
>a void * and how can a X* const be converted to void* const? Is that a
>standard conversion by any chance? I'd be surprised if it is. 

There appears to be some confusion here between `X * const' and `const X *'.
Conversion from a `X * const' to a `void * const' is perfectly OK,
just as conversion from `X *' to `void *' is; the top-level consts
are irrelevant in this case.

The code 

	X* const xp = new X;
	// ...
	delete xp;

is certainly quite legal, and as far as I known always has been - all
compilers ought to accept it.

However, consider a closely related example:

	const X* xp = new X;
	// ...
	delete xp;

According to the ARM, this code was illegal - it was an error to delete
a pointer to const.  However, the ARM never spelt out whether or not
`new const X' was legal.  At the November 94 meeting in Valley Forge,
Pa., the C++ committee decided to allow `new const X'; having decided
to allow this, it was pretty much a requirement to allow deletion of a
pointer to const, otherwise there would be no convenient way to delete
objects allocated with `new const X'.  So that example is also legal,
although some compilers may not yet allow it.

In that example, even though the operand of the delete operator is
a pointer of type `const X *', the pointer that is passed to the
`operator delete' function is of type `void *', not `const void *'
or `const X *'.  The reason is because write protection only lasts
from when the constructor returns until the destructor is called.
The `this' pointer that is passed to the destructor has type `X *',
not `const X *'.  Once the destructor has finished executing, the
pointer points only to raw memory, and so has type `void *'.

An object proceeds through a variety of stages during its lifetime,
and the pointer to the object has a different type at each stage:

	stage				type
	-----				----
	memory allocated		void *

	constructor entered		X *

	constructor completes,
	write protection enabled	const X *

	write protection disabled,	X *
	destructor entered

	destructor completes,		void *
	memory deallocated

These conversions between the different types are done implicitly by
the compiler.

-- 
Fergus Henderson             |  "Australia is the richest country in the world,
fjh@cs.mu.oz.au              |   according to a new system of measuring wealth
http://www.cs.mu.oz.au/~fjh  |   announced by the World Bank yesterday."
PGP: finger fjh@128.250.37.3 |  - Melbourne newspaper "The Age", 18 Sept 1995.

---
[ comp.std.c++ is moderated.  Submission address: std-c++@ncar.ucar.edu.
  Contact address: std-c++-request@ncar.ucar.edu.  The moderation policy
  is summarized in http://dogbert.lbl.gov/~matt/std-c++/policy.html. ]
Technical Details
Message-ID: <9510031355.2074@mulga.cs.mu.OZ.AU>
Path: arc1-ca.pugleaf.net!128.32.155.1!matt
References: <9510020800.AA06591@marsh.cacs.usl.edu>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!128.32.155.1!matt
From: fjh@munta.cs.mu.OZ.AU (Fergus Henderson)
Newsgroups: comp.std.c++
Subject: Re: Why not op delete(void* v&,size_t);?
Date: 03 Oct 1995 18:18:25 GMT
Organization: Computer Science, University of Melbourne, Australia
Approved: matt@physics2.Berkeley.EDU (Matt Austern)
Message-ID: <9510031355.2074@mulga.cs.mu.OZ.AU>
References: <9510020800.AA06591@marsh.cacs.usl.edu>
NNTP-Posting-Host: physics2.berkeley.edu