Re: [Q] about free(ptr)
From: kj (socyl_at_987jk.com.invalid)
Date: 12/06/04
- Next message: Jens.Toerring_at_physik.fu-berlin.de: "Re: declarator name equals type name weirdness"
- Previous message: jacob navia: "Re: New book - 'C of Peril'"
- In reply to: Chris Torek: "Re: [Q] about free(ptr)"
- Next in thread: Chris Torek: "Re: [Q] about free(ptr)"
- Reply: Chris Torek: "Re: [Q] about free(ptr)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Mon, 6 Dec 2004 14:11:10 +0000 (UTC)
In <covorf02cgi@news1.newsguy.com> Chris Torek <nospam@torek.net> writes:
>That dinky little my_free() function was worse than useless...
Too bad! I kinda liked that dinky little my_free(). The reason
is this. Suppose I have defined the following struct as the basic
node in a linked list:
struct elem {
void *payload;
struct elem *next_one;
};
...and I also have
struct linked_list {
struct elem *first;
struct elem *last;
struct elem *next;
int length;
};
Now I want to define a generic destroy_linked_list function, like this:
void destroy_linked_list(struct linked_list **ll,
void (*destroyer_of_payload)(void **)) {
(*ll)->next = (*ll)->first;
while ((*ll)->next) {
struct elem *hold;
hold = (*ll)->next;
(*ll)->next = (*ll)->next->next_one;
_destroy_element(&hold, destroyer_of_payload);
}
free(*ll);
*ll = NULL;
return;
}
void _destroy_element(struct elem **e,
void (*destroyer_of_payload)(void **)) {
if (destroyer_of_payload)
(*destroyer_of_payload)(&((*e)->payload));
free(*e);
*e = NULL;
return;
}
OK, I'm beyond my depth here (which is probably pretty obvious by
now), so I imagine there are already quite a few problems with the
code I've written so far, but what *I* can see as the first problem
with this scheme, in light of Chris's post, is in defining specific
destroyer_of_payload functions, since they must have a void **
argument, but free memory associated with a specific type.
Is there any way to get something like this to work (without
switching to C++ that is)?
TIA,
kj
-- NOTE: In my address everything before the first period is backwards; and the last period, and everything after it, should be discarded.
- Next message: Jens.Toerring_at_physik.fu-berlin.de: "Re: declarator name equals type name weirdness"
- Previous message: jacob navia: "Re: New book - 'C of Peril'"
- In reply to: Chris Torek: "Re: [Q] about free(ptr)"
- Next in thread: Chris Torek: "Re: [Q] about free(ptr)"
- Reply: Chris Torek: "Re: [Q] about free(ptr)"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]