Article View: comp.std.c++
Article #3770Re: Referencing pointers after delete
From: Steve Clamage
Date: Fri, 22 Mar 1996 02:05
Date: Fri, 22 Mar 1996 02:05
48 lines
2595 bytes
2595 bytes
In article ceo@engnews1.Eng.Sun.COM, "joe (j.) halpin" <jhalpin@bnr.ca> writes: > >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. It is not the pointer object that becomes unusable, but the value of the pointer. You can always store a valid value (like a null pointer) into the object. The reason for the odd-looking condition (which follows the C standard) is to allow architectures which validate pointers in hardware to be standard-conforming. Example: Suppose a "tagged" architecture marks each pointer as to whether it contains a valid value. char *p; // p initially tagged as invalid value p = new char[100]; // p tagged as valid delete [] p; // p tagged as invalid p = 0; // p tagged as valid null pointer The hardware might trap any use of an invalid pointer value, even just reading the value without dereferencing it. You can't do anything useful in portable code with uninitialized pointers, or pointers which point to objects which are no longer available. The standard allows the implementation a great deal of leeway in trying to help by identifying such invalid uses. --- Steve Clamage, stephen.clamage@eng.sun.com --- [ 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:
<4is1a5$cag@engnews1.Eng.Sun.COM>Path:
arc1-ca.pugleaf.net!in2.uu.net!bounce-backReferences:
<4is05t$ceo@engnews1.Eng.Sun.COM>Show Full Headers
Complete Article Headers
From: clamage@Eng.Sun.COM (Steve Clamage) Message-ID: <4is1a5$cag@engnews1.Eng.Sun.COM> X-Original-Date: 21 Mar 1996 16:49:09 GMT Path: arc1-ca.pugleaf.net!in2.uu.net!bounce-back Date: 22 Mar 96 02:05:40 GMT Approved: fjh@cs.mu.oz.au Newsgroups: comp.std.c++ Subject: Re: Referencing pointers after delete Organization: Sun Microsystems Inc. References: <4is05t$ceo@engnews1.Eng.Sun.COM> Reply-To: clamage@Eng.Sun.COM X-Auth: PGPMoose V1.1 PGP comp.std.c++ iQBFAgUBMVILBeEDnX0m9pzZAQGdQgF/UkoOA7l3pdx2FjsEaJMGsFTsfC+xf3AF 5V5ubutqSCC7Zu1X7EQ/EINCnFlWQe0R =BArS