🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

3 total messages Started by joe (j.) halpin Thu, 21 Mar 1996 19:08
Using pointers after delete
#3760
Author: joe (j.) halpin
Date: Thu, 21 Mar 1996 19:08
36 lines
2153 bytes
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 
]
Re: Using pointers after delete
#3776
Author: Eric Newhuis
Date: Fri, 22 Mar 1996 06:54
26 lines
1723 bytes
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                             ]
Re: Using pointers after delete
#3777
Author: 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                             ]
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