I
Ivan Shmakov
I've got interested in using the search.h functions, yet I don't
seem to understand the meaning of the tdelete ()'s return value
description. Which is [1]:
[...] The tdelete() function shall return a pointer to the parent of
the deleted node, or an unspecified non-null pointer if the deleted
node was the root node, or a null pointer if the node is not found.
[1] http://pubs.opengroup.org/onlinepubs/9699919799/functions/tdelete.html
Somehow, I fail to understand if there's any valid way to use
the value returned, other than to check whether there was
actually a matching node in the tree or not.
Given that tsearch () may associate an arbitrary pointer with a
node of the tree, I'd expect for tdelete () to be able to return
that pointer somehow (so that, e. g., one may free () the
referenced memory.) Do I understand it correctly that to
properly remove the node and the associated memory object one
has to walk over the tree twice instead, e. g. as shown below?
#if 1
myobj *op
= tfind (key, &tree, myobj_cmp);
if (op != 0) {
tdelete (key, &tree, myobj_cmp);
myobj_free (op);
}
#else
/* instead of: */
myobj *op
= tdelete_and_return (key, &tree, myobj_cmp);
/* assuming myobj_free () ignores null pointers */
myobj_free (op);
#endif
TIA.
seem to understand the meaning of the tdelete ()'s return value
description. Which is [1]:
[...] The tdelete() function shall return a pointer to the parent of
the deleted node, or an unspecified non-null pointer if the deleted
node was the root node, or a null pointer if the node is not found.
[1] http://pubs.opengroup.org/onlinepubs/9699919799/functions/tdelete.html
Somehow, I fail to understand if there's any valid way to use
the value returned, other than to check whether there was
actually a matching node in the tree or not.
Given that tsearch () may associate an arbitrary pointer with a
node of the tree, I'd expect for tdelete () to be able to return
that pointer somehow (so that, e. g., one may free () the
referenced memory.) Do I understand it correctly that to
properly remove the node and the associated memory object one
has to walk over the tree twice instead, e. g. as shown below?
#if 1
myobj *op
= tfind (key, &tree, myobj_cmp);
if (op != 0) {
tdelete (key, &tree, myobj_cmp);
myobj_free (op);
}
#else
/* instead of: */
myobj *op
= tdelete_and_return (key, &tree, myobj_cmp);
/* assuming myobj_free () ignores null pointers */
myobj_free (op);
#endif
TIA.