You can use the functions in recycle.c as a template for a free function
for your new structure... let's say this:

struct your_structure
{
    char *name;
    struct new_struct * another_new_structure;
    struct yet_another_struct * a_third_structure;
    int blah;
    bool blahblah;
    bool valid;
};

in recycle.c, for your new_structure function that you would call as
such:
structure = new_structure();
you'll want to initialize the other fields of it there to something,
like such (after the allocation of memory for it):

structure->name = &str_empty[0];
structure->blah = 0;
structure->blahblah = FALSE;

VALIDATE(structure);

And then return the structure to the calling function that requested the
allocation... in the free_structure function you'd want to free anything
that ate up memory that was in the first layer of that structure (i.e.
if another_new_structure had a char *name field also, you wouldn't have
to worry about freeing structure->another_new_structure->name, as that
field would be freed from the free function for struct new_struct, of
which another_new_structure is a member), put simply, something like
this:

free_string(structure->name);
free_new_struct(structure->another_new_structure);
free_yet_another_struct(structure->a_third_structure);

INVALIDATE(structure);

Unless it's assigned as a pointer, you don't need to worry about it,
such as if you had a char whatever[256], or a char letter in the
structure, but if it is a pointer, it needs a function to free that
memory, even if it's a pointer to something like int or bool... hope
this helps...

wavewave
Richard Lindsey.

-----Original Message-----
From: [EMAIL PROTECTED] [mailto:[EMAIL PROTECTED] 
Sent: Tuesday, June 15, 2004 3:50 PM
To: [email protected]
Subject: Freeing Structures

I have a few new structures that I have created. This would be my first
time
adding them to the CHAR and PC_DATA structures.

In these structures I have a linked list. When I remove one of them I
just set
it to NULL, such as:

ch->structure_here = NULL;

I am not sure if this is enough, as the structure contains 5 or 6 fields
of
data. I suspect that the data is still there, but the pointer doesn't
point to
it. This then would increase memory over time.
So, you have to free it first with a function, such as:
free_structure_here(ch->structure_here);
then set it to NULL.

ch->structure_here = NULL;

Is this correct? Or does just setting the pointer to NULL do enough?

When I add to the linked list, I do this:
STRUCTURE_HERE *blah = new_structure();

Where in new_structure it has:
struct                 =   alloc_perm( sizeof(*struct) );

Thank you.

-- 
ROM mailing list
[email protected]
http://www.rom.org/cgi-bin/mailman/listinfo/rom

Reply via email to