Thread View: comp.std.c++
6 messages
6 total messages
Started by joe (j.) halpin
Thu, 21 Mar 1996 16:29
Referencing pointers after delete
Author: joe (j.) halpin
Date: Thu, 21 Mar 1996 16:29
Date: Thu, 21 Mar 1996 16:29
34 lines
1831 bytes
1831 bytes
To Moderator: This may be a duplicate, my newsreader software was having trouble sending this, so I'm mailing it as well. 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. 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. Joe [ 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 ]
Re: Referencing pointers after delete
Author: Tom Payne
Date: Thu, 21 Mar 1996 19:19
Date: Thu, 21 Mar 1996 19:19
37 lines
2583 bytes
2583 bytes
joe (j.) halpin (jhalpin@bnr.ca) wrote:
: To Moderator: This may be a duplicate, my newsreader software was having
: trouble sending this, so I'm mailing it as well.
:
: 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.
[...]
: 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.
Apparently there are architectures (80386?) that perform such validity
checks on read access to pointer registers and trap on certain bogus
values. So, the standard deems that after deallocation a pointer
object might contain a value that is so bogus that even reading it
will provoke "undefined behavior" and assigning the pointer a valid
value is the only valid operation. It seems that to accomodate
certain hardware-based validity checkers for pointers, the standard
has precluded the possibility of portable software-based validity
checking. IMHO an unfortunate decision.
Tom Payne (thp@cs.ucr.edu)
---
[ 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
]
Re: Referencing pointers after delete
Author: Dietmar Kuehl
Date: Thu, 21 Mar 1996 20:01
Date: Thu, 21 Mar 1996 20:01
56 lines
3002 bytes
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
]
Re: Referencing pointers after delete
Author: 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 ]
Re: Referencing pointers after delete
Author: Tom Payne
Date: Fri, 22 Mar 1996 22:20
Date: Fri, 22 Mar 1996 22:20
33 lines
2232 bytes
2232 bytes
Steve Clamage (clamage@Eng.Sun.COM) wrote: : 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. [...] : 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: [...] : 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. ... just as for arithmetic exceptions, which also engender "undefined behavior." But why the leeway to, say, reformat the disk, rather than specifying the normal practice, which is to invoke a signal handler, thereby, keeping the behavior defined? Tom Payne (thp@cs.ucr.edu) [ 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 ]
Re: Referencing pointers after delete
Author: John Bain
Date: Sat, 23 Mar 1996 17:09
Date: Sat, 23 Mar 1996 17:09
50 lines
2270 bytes
2270 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.
I interpret this as meaning the _value_ of the pointer cannot be used in
an expression.
>
>This sounds as though, in the following:
>
>char *pc = new char[128];
>delete pc;
^^^^
delete[] pc;
>pc = 0;
>
>it makes the final assignment (an expression) invalid.
The value of the pointer is not used in the expression, so it's OK.
>In fact, it sounds like it also rules out things like 'if(pc == 0)
>...' after the above fragment.
After the final assignment, the pointer's value is no longer invalid, so
it can be used (though obviously not dereferenced while NULL).
By my interpretation, (pc == 0) would, however, be invalid after the
delete but before the assignment of 0.
Cheers,
John
-----------------------------------------------------------------
John Bain
johnb@pivotal-dm.ccmail.compuserve.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 ]
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