home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!olivea!sgigate!sgiblab!munnari.oz.au!yarrina.connect.com.au!warrane.connect.com.au!g2syd!senerat
- From: senerat@g2syd.genasys.com.au (Senerat Rajakaruna)
- Newsgroups: comp.lang.c
- Subject: Re: Btree, or Linked List Questions
- Message-ID: <1993Jan21.015358.24137@g2syd.genasys.com.au>
- Date: 21 Jan 93 01:53:58 GMT
- References: <C13soz.73L@murphy.com>
- Organization: Genasys II, Sydney, Australia
- Lines: 72
-
- In article <C13soz.73L@murphy.com> gregb@murphy.com (Greg Banschbach (Unigroup of New York)) writes:
- >
- >
- > Dear fellow C programmers,
- >
- > I have 2 questions regarding code I have seen:
- >
- > 1. When you create a Btree, most books detail how to
- > do deletions and insertions to add to the tree, and
- > it usually involves using "malloc" to create a node,
- > and "free" to delete it. I saw some code once
- > where, when they were finished with the tree, they
- > just said free(tree). I thought you had to free
- > each individual node throughout the tree before you
- > could free the root node. What gives ?
- >
- > 2. What happens with linked lists ? Do you simply
- > " free " the head - and the rest of the chain is
- > de-allocated ?
- >
- > Please try to support your answers with either quotes from
- > a known source OR something equally as secure ( proven ).
- >
- > Please e-mail me at: uunet!murphy!galunix!greg OR
- > gregb@murphy.com
- >
- > Thanks for the input guys
- > Greg Banschbach
- > ( Not an employee at murphy ).
-
- Blocks of memory obtained dynamically via calls to malloc or calloc should
- be freed by calling free() if dynamic memory is to be maintained for wider use.
- Each such allocation will cause the processes pre-allocated dynamic memory
- space to be reduced and it will subsequently run out if not freed. Once freed
- memory space can be regained via calls to malloc or calloc.
-
- 'There are no restrictions on the order in which space is freed,
- but it is a ghastly error to free something not obtained by calling
- calloc or malloc'
-
- - The C Programming Language by K & R (ANSI C) - p167
-
- Freeing the head of a linked list or a Btree only frees the space reserved
- in storing the address of the structure as defined for the head. Individual
- space gained via calls to malloc or calloc must be freed together with their
- head to save the precious dynamic memory.
-
- The right way to free a linked list is to save the next pointer of head
- before freeing:
-
- LIST *p, *q;
-
- for (p = head; p != NULL; p = q)
- {
- q = p -> next;
-
- /* free any sub-elements of current head */
- FreeData(p);
- (void) free((void*) p);
- }
-
- /* reset head */
- head = (LIST *) NULL;
-
- See K & R pages 185 - 189 for a detailed description of how memory lists are
- handled by the operating system.
-
-
- Senerat Rajakaruna | Genasys II Pty Ltd
- | 13th Level, 33 Berry St, North Sydney, NSW, Australia
- | Phone: +61-2-954-0022 (-9930 FAX)
- | Internet: senerat@g2syd.genasys.com.au
-