Re: freeing memory

From: Kras Kresh (kras_kresh@hotmail.com)
Date: 04/13/01


> > if I wanted to change the object description but not the prototype..
> > should I be freeing it first?
> > ex:
> > free(obj->description);
> > obj->description = str_dup("A dagger lies here.");
>
>A quick glance at my version of read_object() shows that it is doing a
>shallow copy of the prototype object, so you would not want to free that
>particular pointer since it is being used by all other objects of that
>type.
>Since extract_obj() does not free that pointer you will have to free it
>manually when you destroy your object, or you will have to point it to a
>constant string instead of a dynamically allocated string, which looks like
>it would work for you:
>
>static char *desc = "A dagger lies here.";
>
>...
>obj->description = desc;
>
>It is safest to make "desc" static, although it may not be necessary,
>depending on your compiler settings.

Do you think I can just check if the string is the prototyped first?

if (obj->description &&
    obj->description != obj_proto[GET_OBJ_RNUM(obj)].description)
  free(obj->description);
obj->description = str_dup("A dagger lies here.");

My code is going to replace the object strings with other strings,
but the object strings might already not be the proto strings.
So what I wrote above will work fine won't it?
_________________________________________________________________
Get your FREE download of MSN Explorer at http://explorer.msn.com

--
   +---------------------------------------------------------------+
   | FAQ: http://qsilver.queensu.ca/~fletchra/Circle/list-faq.html |
   | Archives: http://post.queensu.ca/listserv/wwwarch/circle.html |
   +---------------------------------------------------------------+



This archive was generated by hypermail 2b30 : 12/05/01 PST