Thread View: comp.std.c++
5 messages
5 total messages
Started by Anil Kumar Vijendran
Mon, 02 Oct 1995 08:24
Re: Why not op delete(void* v&,size_t);?
Author: Anil Kumar Vijendran
Date: Mon, 02 Oct 1995 08:24
Date: Mon, 02 Oct 1995 08:24
33 lines
1718 bytes
1718 bytes
>>>>> On 01 Oct 1995 05:39:01 GMT, vandevod@cs.rpi.edu (David
Vandevoorde) said:
>>>>> "RG" == Ronald F Guilmette <rfg@monkeys.com> writes:
RG> In article <434me4$ov3@citadel.evolving.com>, Todd Blanchard
RG> <tblancha@evolving.com> wrote:
>>> void operator delete(void*,size_t); ... Nowhere can I find a
>>> rational explanation for why the pointer is not passed as a
>>> void*& (to allow zeroing). It would provide more flexibility
>>> in memory management schemes.
RG> Maybe the answer is that delete came before references... then
RG> again, maybe not.
David> Wouldn't this cause problems when attempting to delete
David> const pointers?
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.
In any case, none of the compilers that I use now complain.
Cheers,
Anil
---
[ 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. ]
Re: Why not op delete(void* v&,size_t);?
Author: David Vandevoorde
Date: Mon, 02 Oct 1995 16:48
Date: Mon, 02 Oct 1995 16:48
37 lines
2007 bytes
2007 bytes
>>>>> "A" == Anil Kumar Vijendran <akv@marsh.cacs.usl.edu> writes: >>>>> On 01 Oct 1995 05:39:01 GMT, vandevod@cs.rpi.edu (David A> Vandevoorde) said: In article <434me4$ov3@citadel.evolving.com>, Todd Blanchard <tblancha@evolving.com> wrote: >>> void operator delete(void*,size_t); ... Nowhere can I find a >>> rational explanation for why the pointer is not passed as a >>> void*& (to allow zeroing). It would provide more flexibility >>> in memory management schemes. [...] David> Wouldn't this cause problems when attempting to delete David> const pointers? David> X* const xp = new X; // ... delete xp; // Or is this David> illegal anyway? I suspect not. A> I don't understand how "delete xp" would be true. Because delete takes A> a void * and how can a X* const be converted to void* const? Is that a A> standard conversion by any chance? I'd be surprised if it is. Note that 8.3.5/3 says: " [about cv-qualifiers for parameter types...] Such cv-qualifiers affect only the definition of the parameter within the body of the function. [...]" and 13.1/3 further confirms that `operator delete(void*, size_t);' is equivalent to `operator delete(void* const, size_t);'. I think (not sure) that this would make `delete xp;' above legal. (After all, parameter passing in C++ is ``by value'' by default.) Daveed --- [ 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. ]
Re: Why not op delete(void* v&,size_t);?
Author: Fergus Henderson
Date: Tue, 03 Oct 1995 18:18
Date: Tue, 03 Oct 1995 18:18
80 lines
3518 bytes
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. ]
Re: Why not op delete(void* v&,size_t);?
Author: Joe Buck
Date: Thu, 05 Oct 1995 07:54
Date: Thu, 05 Oct 1995 07:54
40 lines
2119 bytes
2119 bytes
fjh@munta.cs.mu.OZ.AU (Fergus Henderson) writes:
>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.
IMHO, this decision was a serious mistake, as before the decision, a
program that did not cast away const could not alter an object that it
only had a const pointer or reference to; now it can. It can hardly
be argued that deleting an object does not modify it.
How many programmers would expect that the body of the function
void some_func(const char *);
might be
void some_func(const char * foo) { delete [] foo;}
I would hope that compilers at least emit warnings about this practice,
regardless of what mistake the committee might have made.
--
-- Joe Buck <jbuck@synopsys.com> (not speaking for Synopsys, Inc)
Anagrams for "information superhighway": Enormous hairy pig with fan
A rough whimper of insanity
---
[ 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. ]
Re: Why not op delete(void* v&,size_t);?
Author: Fergus Henderson
Date: Thu, 05 Oct 1995 14:56
Date: Thu, 05 Oct 1995 14:56
43 lines
2520 bytes
2520 bytes
jbuck@Synopsys.COM (Joe Buck) writes:
>fjh@munta.cs.mu.OZ.AU (Fergus Henderson) writes:
>>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.
>
>IMHO, this decision was a serious mistake, as before the decision, a
>program that did not cast away const could not alter an object that it
>only had a const pointer or reference to; now it can. It can hardly
>be argued that deleting an object does not modify it.
That's not correct. Even before the decision at Valley Forge, it
was legal for a function to destroy a const object. For example,
following code was always allowed (well, at least since Cfront 2.x):
void foo(const T *p) {
p->~T();
}
So the guarantee that you are arguing should have been preserved
never existed in the first place.
--
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. ]
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