Re: if (blah) ...

From: Peter Ajamian (pajamian@access1.net)
Date: 08/19/99


Chris Proctor wrote:

> At 05:45  19/08/99 -0700, you wrote:
> >"Daniel A. Koepke" wrote:
> >
> >> On Aug 18, Chris Proctor spake to the list:
> >>
> >> > Can you then:
> >> > free(blah);
> >> > blah = NULL;
> >> > if (blah) printf("This is still printed.\n");
> >> > ?
> >>
> >> Yes, you can, and, in fact, it's done throughout the CircleMUD code.
> >> Several instances can be found just in the nanny() function.
> >
> >Really?  Could you please point out one single instance where this is
> >done in nanny?  I looked and could not find even one of the "several"
> >instances you mention.
>
> Having had a look myself, and in relation to my question (which was really
> "Is it safe to set a pointer equal to null after freeing it") dak is
> certainly correct.
> This was from bpl15 (didn't bother checking whatever archaic version I'm on).
> Obviously it doesn't set the pointer equal to null and then do an if
> (pointer) on it immediately (that'd be a bit silly, wouldn't it?).
> Not sure exactly which bit you were referring to there . . .
>
> from nanny(), in interpreter.c
>
>         if (PLR_FLAGGED(d->character, PLR_DELETED)) {
>           /* We get a false positive from the original deleted character. */
>           free_char(d->character);
>           d->character = NULL;
>
> and a bit lower down:
>     } else if (*arg == 'n' || *arg == 'N') {
>       SEND_TO_Q("Okay, what IS it, then? ", d);
>       free(d->character->player.name);
>       d->character->player.name = NULL;
>
> and again:
>     case '2':
>       if (d->character->player.description) {
>         SEND_TO_Q("Old description:\r\n", d);
>         SEND_TO_Q(d->character->player.description, d);
>         free(d->character->player.description);
>         d->character->player.description = NULL;
>       }

Ceartainly there are several places where it frees memory, then sets the pointer
to null, but where does it then turn around and base an if statement on the value
of the pointer which was just set to null as in the following line from above...?

>> > if (blah) printf("This is still printed.\n");

Regards,


Peter


     +------------------------------------------------------------+
     | Ensure that you have read the CircleMUD Mailing List FAQ:  |
     |  http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html  |
     +------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/15/00 PST