Topic: Deleting pointer to const
Author: bkline%occs.nlm.nih.gov (Bob Kline)
Date: Wed, 8 Jun 94 15:49:54 GMT Raw View
We notice that the following code is accepted by Borland's C++ compilers:
class C { public: ~C() {} };
void foo {
const C *p = new C;
delete p;
}
Is this a reflection of (or anticipation of) a proposed change to the
rule that a pointer to constant cannot be deleted (ARM 5.3.4)? If so,
could we get a summary of the reasoning behind the change?
/*----------------------------------------------------------------------*/
/* Bob Kline CSI Technologies */
/* bkline@smtp.csof.com Corporate Software, Inc. */
/* voice: (703) 522-0820 fax: (703) 522-5407 */
/*----------------------------------------------------------------------*/
Author: pete@genghis.interbase.borland.com (Pete Becker)
Date: Wed, 8 Jun 1994 18:07:52 GMT Raw View
In article <1994Jun8.154954.9386@nlm.nih.gov>,
Bob Kline <bkline%occs.nlm.nih.gov> wrote:
>
>We notice that the following code is accepted by Borland's C++ compilers:
>
>class C { public: ~C() {} };
>void foo {
> const C *p = new C;
> delete p;
>}
>
If you compile with -A (strict ANSI/ISO compliance) this code is not
accepted.
-- Pete
Author: bkline%occs.nlm.nih.gov (Bob Kline)
Date: Thu, 9 Jun 94 11:48:35 GMT Raw View
Pete Becker (pete@genghis.interbase.borland.com) wrote:
: In article <1994Jun8.154954.9386@nlm.nih.gov>,
: Bob Kline <bkline%occs.nlm.nih.gov> wrote:
: >
: >We notice that the following code is accepted by Borland's C++ compilers:
: >
: >class C { public: ~C() {} };
: >void foo {
: > const C *p = new C;
: > delete p;
: >}
: >
: If you compile with -A (strict ANSI/ISO compliance) this code is not
: accepted.
Thanks. Can you shed any light on why the compiler needs the flag
to recognize the illegal code in this case but does not need the flag
if the destructor is omitted? What's the rationale behind the
different behavior?
/*----------------------------------------------------------------------*/
/* Bob Kline CSI Technologies */
/* bkline@smtp.csof.com Corporate Software, Inc. */
/* voice: (703) 522-0820 fax: (703) 522-5407 */
/*----------------------------------------------------------------------*/
Author: pete@genghis.interbase.borland.com (Pete Becker)
Date: Thu, 9 Jun 1994 21:57:04 GMT Raw View
In article <1994Jun9.114835.9201@nlm.nih.gov>,
Bob Kline <bkline%occs.nlm.nih.gov> wrote:
>Pete Becker (pete@genghis.interbase.borland.com) wrote:
>: In article <1994Jun8.154954.9386@nlm.nih.gov>,
>: Bob Kline <bkline%occs.nlm.nih.gov> wrote:
>: >
>: >We notice that the following code is accepted by Borland's C++ compilers:
>: >
>: >class C { public: ~C() {} };
>: >void foo {
>: > const C *p = new C;
>: > delete p;
>: >}
>: >
>
>: If you compile with -A (strict ANSI/ISO compliance) this code is not
>: accepted.
>
>Thanks. Can you shed any light on why the compiler needs the flag
>to recognize the illegal code in this case but does not need the flag
>if the destructor is omitted? What's the rationale behind the
>different behavior?
The compiler doesn't "need" the flag to recognize illegal code. This
is a compatibility switch to avoid breaking existing code.
-- Pete
Author: bkline%occs.nlm.nih.gov (Bob Kline)
Date: Fri, 10 Jun 94 02:30:05 GMT Raw View
Pete Becker (pete@genghis.interbase.borland.com) wrote:
: In article <1994Jun9.114835.9201@nlm.nih.gov>,
: Bob Kline <bkline%occs.nlm.nih.gov> wrote:
:
: >Thanks. Can you shed any light on why the compiler needs the flag
: >to recognize the illegal code in this case but does not need the flag
: >if the destructor is omitted? What's the rationale behind the
: >different behavior?
: The compiler doesn't "need" the flag to recognize illegal code. This
: is a compatibility switch to avoid breaking existing code.
: -- Pete
Does this mean that it used to be legal to delete a const object if
the object had a destructor, but illegal if it didn't have one?
Also, wouldn't it be appropriate to at least issue a warning
(particularly when the flag for the maximum warning level has been
used) when the compiler encounters illegal code?
/*----------------------------------------------------------------------*/
/* Bob Kline CSI Technologies */
/* bkline@smtp.csof.com Corporate Software, Inc. */
/* voice: (703) 522-0820 fax: (703) 522-5407 */
/*----------------------------------------------------------------------*/