Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > C > Re: no :of node...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 13 of 18 Topic 26106 of 26972
Post > Topic >>

Re: no :of nodes

by "Chris Thomasson" <cristom@[EMAIL PROTECTED] > May 7, 2008 at 09:11 PM

"Richard Harter" <cri@[EMAIL PROTECTED]
> wrote in message 
news:482268d8.100836171@[EMAIL PROTECTED]
> On Wed, 7 May 2008 17:22:50 -0700, "Chris Thomasson"
> <cristom@[EMAIL PROTECTED]
> wrote:
>
>>"Chris Thomasson" <cristom@[EMAIL PROTECTED]
> wrote in message
>>news:vLGdndQrSbqk2L_VnZ2dnUVZ_rjinZ2d@[EMAIL PROTECTED]
>>>
>>> Its no good. Use dynamic allocation if the level count of the tree is 
>>> not
>>> known in advance. Also, recursion is not safe. Passing some tress to 
>>> this
>>> function will blow the stack. There are ways to traverse a tree
without
>>> using recursion. Here is a simple example:
[...]
>>
>>http://pastebin.com/m3e5a9fd8
[...]
>>> Instead of threading the tree, you can include an auxiliary node that
a
>>> traversal function uses as an node to link into a local queue. Check
the
>>> 'destroy_tree()' function...
>
>
> If I read the code correctly, it makes a separate malloc call for
> each new node.  While legal, that makes for inefficient code.  It
> is better to allocate space for a block of nodes and link them
> into a free list.  There are other alternatives, but the essence
> is that one malloc per node is expensive and should be avoided in
> production code.

You can hook it up to the following crude region allocator I did:

http://pastebin.com/m52ba914

http://groups.google.com/group/comp.lang.c/browse_frm/thread/f8f65273b09b4229


Now, the tree program can look something like:

http://pastebin.com/m757377c3


The region allocator simply increments a counter and bumps a pointer along

the buffer until the end is reached, then another region is allocated. 
Deallocations consist of decrementing the counter and resetting the 
allocator state when zero is reached. It can dynamically expand and
shrink. 
It can also amortize calls to free into a single "reset" call. The example

code shows this...


Any thoughts?
 




 18 Posts in Topic:
no :of nodes
sophia <sophia.agnes@[  2008-05-07 11:16:16 
Re: no :of nodes
roberson@[EMAIL PROTECTED  2008-05-07 18:30:53 
Re: no :of nodes
Eric Sosman <Eric.Sosm  2008-05-07 14:39:58 
Re: no :of nodes
Thad Smith <ThadSmith@  2008-05-07 21:03:29 
Re: no :of nodes
Keith Thompson <kst-u@  2008-05-07 21:26:12 
Re: no :of nodes
Eric Sosman <esosman@[  2008-05-08 08:36:50 
Re: no :of nodes
"Chris Thomasson&quo  2008-05-08 06:37:01 
Re: no :of nodes
Thad Smith <ThadSmith@  2008-05-16 19:30:16 
Re: no :of nodes
fred.l.kleinschmidt@[EMAI  2008-05-07 15:19:02 
Re: no :of nodes
"Chris Thomasson&quo  2008-05-07 17:14:15 
Re: no :of nodes
"Chris Thomasson&quo  2008-05-07 17:22:50 
Re: no :of nodes
cri@[EMAIL PROTECTED] (R  2008-05-08 03:02:20 
Re: no :of nodes
"Chris Thomasson&quo  2008-05-07 21:11:29 
Re: no :of nodes
cri@[EMAIL PROTECTED] (R  2008-05-08 14:41:40 
Re: no :of nodes
"Chris Thomasson&quo  2008-05-08 07:57:12 
Re: no :of nodes
Spiros Bousbouras <spi  2008-05-08 05:04:02 
Re: no :of nodes
roberson@[EMAIL PROTECTED  2008-05-08 14:53:41 
Re: no :of nodes
Spiros Bousbouras <spi  2008-05-08 08:24:38 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 25 16:04:29 CDT 2008.