🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

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

Re: Using pointers after delete

#3777
From: John R. Williams
Date: Fri, 22 Mar 1996 06:55
44 lines
2451 bytes
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.

It says that the pointer *contains an unuseable value*. Assignment to the 
pointer replaces this with a potentially useable value. It's important 
not to confuse an object with the value it contains.

BTW "delete pc" is required to be "delete[] pc"

> 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.

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

No; the pointer is perfectly good now that it contains a valid value.

--
class JohnWilliams: public Student, public Programmer {
public:
  char const *operator&() const { return "johnw@jove.acs.unt.edu"; }
  char const *homepage () const { return "http://www.unt.edu/~johnw"; }
};
---
[ 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                             ]
Technical Details
Message-ID: <4itea2$mql@hermes.acs.unt.edu>
Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back
References: <4irkhs$8al@crchh327.rich.bnr.ca>

Show Full Headers
Complete Article Headers
From: johnw@jove.acs.unt.edu (John R. Williams)
Message-ID: <4itea2$mql@hermes.acs.unt.edu>
X-Original-Date: 22 Mar 1996 05:37:06 GMT
Path: arc1-ca.pugleaf.net!in1.uu.net!bounce-back
Date: 22 Mar 96 06:55:39 GMT
Approved: fjh@cs.mu.oz.au
Newsgroups: comp.std.c++
Subject: Re: Using pointers after delete
Organization: University of North Texas
References: <4irkhs$8al@crchh327.rich.bnr.ca>
X-Newsreader: TIN [version 1.2 PL2]
X-Auth: PGPMoose V1.1 PGP comp.std.c++
 iQBFAgUBMVJO9OEDnX0m9pzZAQEaLwF9ERCpdp6/Cpf/Rg/vCHnIUZZVL+3fc3Et
 Bfln+7vHyh8L/LPXN8B/tyy8IyjuHuPp
 =6jUQ