Re: freeing memory

From: Mike Breuer (
Date: 04/13/01

----- Original Message -----
From: "Kras Kresh" <kras_kresh@HOTMAIL.COM>
Sent: Friday, April 13, 2001 3:03 PM

> 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.


   | FAQ: |
   | Archives: |

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