Re: Oh man is this a bad one...

From: Mr Stuart Lamble (sjlam1@aurora.cc.monash.edu.au)
Date: 05/08/96


> extract_char(ch)... in there it calls free_char(ch), which SHOULD free all
> memory used by a char, right? Then it frees the ch itself with free(ch) then
> ch = NULL; right?
> 
> SO when it returns from free_char, ch SHOUDL = NULL, right?
> 
> SO when it returns from extract_char, ch should be null, right?
[snip]
> void free_char(struct char_data *ch)
> {
[snip]
>   free(ch);
>   ch = NULL;
> }
> 
> Those last 2 lines should kill the char outta memory, even if it leaves the
> strings and such, right? Well.. I dunno... any help appreiciated.

Elementary, my dear Watson. :-) Remember that in C, parameters passed to
a function are passed by value, not reference.. so you're passing the
value of the pointer ch. Thus, the ch in free_char is a _copy_ of the
original variable, not the original variable itself. You're freeing the
character, that's working fine - but that last line only sets the value
of the ch *in free_char* to NULL - not the original variable.

You'll have to do one of two things: either change free_char so that
it expects a struct char_data **ch, and pass the address of the variable
(with all the code changes that entails), or remember to set ch to NULL
after calling free_char. Doing it in free_char won't help you.

Is this clear, or am I just babbling? :-)

-- 
I now have a .sig of my vewwy own. I shall hug him, and love him,
and call him George.



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