🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Referencing pointers after delete

#3765
From: Dietmar Kuehl
Date: Thu, 21 Mar 1996 20:01
56 lines
3002 bytes
Hi,

joe (j.) halpin (jhalpin@bnr.ca) wrote:
: In 3.7.3.2.4 the January working paper says:

: 4 A deallocation function can free the storage referenced by the pointer
:   given  as  its  argument and renders the pointer invalid.  The storage
:   can be made available for further allocation.  An invalid pointer con-
:   tains an unusable value:  it cannot even be used in an expression.

: This sounds as though, in the following:

: char *pc = new char[128];
: delete pc;
: pc = 0;

: it makes the final assignment (an expression) invalid.

No. You cannot use the value of the pointer but you can use the pointer
(actually, you get undefined behavior in the code above because you
delete an array object with 'delete' instead of 'delete[]'; I assume
you took the correct form). Code like this is non-portable:

  char *start = strcpy(new char[10], "foobar");
  char *found = strchr(start, 'o');
  delete[] start;
  if (found == start)
    ...

: Am I misunderstanding something, or is it illegal to zero out pointers
: after they've been deallocated? I'm assuming that the intent was to
: disallow dereferencing of pointers that have been handed to
: delete. The wording seems to disallow the above as well.

The wording is a little bit stronger than just disallowing
dereferencing: There is no portable use of the value of the pointer
after deleting the object. I.e. you cannot use the value to calculate
an pointer to another object and you cannot use the value to compare it
with another pointer.

: In fact, it sounds like it also rules out things like 'if(pc == 0)
: ...' after the above fragment.

No, it doesn't: If you apply 'delete' to a NULL pointer, nothing
happens.
--
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 
                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 
]
Technical Details
Message-ID: <4isca2$t20@news.BelWue.DE>
Path: arc1-ca.pugleaf.net!chronicle.mti.sgi.com!austern
References: <4is05t$ceo@engnews1.Eng.Sun.COM>

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!chronicle.mti.sgi.com!austern
From: kuehl@uzwil.informatik.uni-konstanz.de (Dietmar Kuehl)
Newsgroups: comp.std.c++
Subject: Re: Referencing pointers after delete
Date: 21 Mar 1996 12:01:42 PST
Organization: Fakultdt f|r Mathematik und Informatik
Approved: austern@isolde.mti.sgi.com
Message-ID: <4isca2$t20@news.BelWue.DE>
References: <4is05t$ceo@engnews1.Eng.Sun.COM>
Reply-To: dietmar.kuehl@uni-konstanz.de
NNTP-Posting-Host: isolde.mti.sgi.com
X-Original-Date: 21 Mar 1996 19:56:50 GMT
X-Newsreader: TIN [version 1.2 PL2]
X-Auth: PGPMoose V1.1 PGP comp.std.c++
 iQBVAwUBMVG1u0y4NqrwXLNJAQFR1QH/Wrk/dKwn2eGO5XYKKTYXnsO/tRqiD+pi
 n0PvG5nqAb5ZjGeZqEE239qEu1x7ONLyvSB+pOOrfmK/0HbUM1dYmA==
 =/Sxo
Originator: austern@isolde.mti.sgi.com