About allocating memory to a node in a link list

Discussion in 'C Programming' started by Kane, Jul 7, 2003.

  1. Kane

    Kane Guest

    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
     
    Kane, Jul 7, 2003
    #1
    1. Advertising

  2. Kane

    Eric Sosman Guest

    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.

    --
     
    Eric Sosman, Jul 7, 2003
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Replies:
    0
    Views:
    1,648
  2. Tjerk Wolterink
    Replies:
    2
    Views:
    1,505
    Dimitre Novatchev
    Aug 24, 2006
  3. sangram
    Replies:
    16
    Views:
    2,081
  4. Kane
    Replies:
    3
    Views:
    349
    Chris Thomasson
    Aug 27, 2007
  5. Rakesh Kumar
    Replies:
    5
    Views:
    694
    James Kanze
    Dec 21, 2007
Loading...

Share This Page