Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > About allocating memory to a node in a link list

Reply
Thread Tools

About allocating memory to a node in a link list

 
 
Kane
Guest
Posts: n/a
 
      07-07-2003
When you create node 1 you allocate memory and link it Again when you
create node 2 you would allocate memory for that node in a different
section of the code. Is there more efficient way where I can allocate
memory once and use it to create new node in list.
-Thanks
-Kane
 
Reply With Quote
 
 
 
 
Eric Sosman
Guest
Posts: n/a
 
      07-07-2003
Kane wrote:
>
> When you create node 1 you allocate memory and link it Again when you
> create node 2 you would allocate memory for that node in a different
> section of the code. Is there more efficient way where I can allocate
> memory once and use it to create new node in list.


I'm not sure what you're asking, but I'll make
an attempt at answering anyhow ...

If the linked list has two nodes, those two nodes
must occupy two chunks of memory. Three nodes require
three chunks, N nodes require N chunks. You might be
able to play strange games by having the various chunks
overlap each other partially, but that way lies madness.

The existence of two or three or N chunks doesn't
necessarily imply two or three or N separate allocations.
You could, for example, use malloc() to reserve enough
memory for twenty nodes and then dole them out one by
one internally; this technique has some advantages when
the nodes are very small and very numerous. However, you
cannot then free() or realloc() the individual nodes;
you can only use free() or realloc() on the original
twenty-node block as a whole.

The existence of multiple allocated blocks (whether
of one or many nodes each) doesn't imply the presence
of multiple malloc() calls in your code. You can (and
usually should) arrange to execute and re-execute and
re-re-execute a single malloc() call as many times as
needed. Sometimes it's convenient to treat the very
first node specially and use an executed-only-once
malloc() call for it, but you should almost always have
just a single malloc() for every subsequent block.

If that doesn't answer your question, try restating
it more clearly.

--
http://www.velocityreviews.com/forums/(E-Mail Removed)
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Allocating vector of strings seem to crash. Allocating array ofstrings seems to be ok . Rakesh Kumar C++ 5 12-21-2007 10:42 AM
About allocating memory to a node in a link list Kane C Programming 3 08-27-2007 06:52 AM
how to delete last node of a Linked list if you only know the address of last node. sangram C Programming 16 12-01-2006 08:57 PM
xsl variable $node/text() but $node can non-node-set help! Tjerk Wolterink XML 2 08-24-2006 03:28 AM
How to set the node indent property between the parent node and the leaf node viveknatani@gmail.com ASP .Net 0 02-13-2006 07:11 PM



Advertisments