Re: assigning pointer to NULL



In article <47a03c63.2858170722@xxxxxxxxxxxxxx>,
Richard Bos <rlb@xxxxxxxxxxxxxxxxxxxxxx> wrote:
"Roman Mashak" <mrv@xxxxxxxx> wrote:

is it legal to destroy the memory pointed to by a pointer and then assign
NULL to it? Say, like this:

char *ptr;
ptr = malloc(10);
free(ptr);
ptr = NULL;

Legal, but useless.

Not completely useless, only mostly.

If you know that the pointer you're setting to NULL was the only
pointer into that memory, then it's perfectly safe and useful.
(This is rare, but not unheard of.)

It's also not unreasonable to use the non-null-ness of a pointer as a
flag that you have useful information of some sort, and to want to
discard that information before you have something else to replace it
with.
(There are intelligent people who disagree with other intelligent
people about whether this indicates a design flaw.)


dave

--
Dave Vandervies dj3vande at eskimo dot com
I like "fun" risks, rather than "lazy" risks.
(If I'm going to kill myself, I want to have a good time on the way.)
--Graham Reed in the scary devil monastery
.



Relevant Pages

  • Re: assigning pointer to NULL
    ... ptr = NULL; ... Not completely useless, only mostly. ... If you know that the pointer you're setting to NULL was the only ... flag that you have useful information of some sort, ...
    (comp.lang.c)
  • Re: assigning pointer to NULL
    ... char *ptr; ... Not completely useless, only mostly. ... flag that you have useful information of some sort, ... compilers and rarely no registers to spare, ...
    (comp.lang.c)
  • Re: assigning pointer to NULL
    ... char *ptr; ... Not completely useless, only mostly. ... If you know that the pointer you're setting to NULL was the only ... flag that you have useful information of some sort, ...
    (comp.lang.c)
  • Re: Typecasting pointers
    ... array of shorts, and initialize them all to the same value. ... You're saving the value of ptr so you can freeit later. ... A pointer increment advances the pointer by one object size, ... optimizing compiler could have done a better job than you have. ...
    (comp.lang.c)
  • RE: Multiple pointer entry`s for pc`s
    ... Pointer (PTR) RRs are used to support the reverse ... resolve this information to the DNS domain name for that computer. ... PTR RRs can be added to a zone in several ways: ...
    (microsoft.public.win2000.dns)