Topic: Using pointers after delete
Author: "joe (j.) halpin" <jhalpin@bnr.ca>
Date: 1996/03/21 Raw View
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
--
Joe Halpin jhalpin@nortel.com
Nortel Wireless (214) 684-5657
Richardson, TX 75083-3871 <standard disclaimer applies>
-------------------------------------------------------------------------------
---
[ 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
]
Author: Eric Newhuis <newhuis@ix.netcom.com>
Date: 1996/03/22 Raw View
joe (j.) halpin wrote:
>
> char *pc = new char[128];
> delete pc;
> pc = 0;
>
> 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.
>
There is not problem using a pointer like that. If you re-read the
working paper closely you'll notice that the following phrase:
"An invalid pointer con-tains an unusable value: it cannot even be used
in an expression."
is refering to the value of the pointer, not the pointer itself. In
other words, replacing the value with zero is fine.
---
[ 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 ]
Author: johnw@jove.acs.unt.edu (John R. Williams)
Date: 1996/03/22 Raw View
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 ]