Dynamic memory allocation:Reading a file into buffers using a single linked list!

Discussion in 'C Programming' started by Henk, Sep 29, 2005.

  1. Henk

    Henk Guest

    Hi,

    I am new to the c-programming language and at the moment I am
    struggling with the following:

    I want to read a file using fread() and then put it in to memory. I
    want to use a (singel) linked list to continousely (dynamically) free
    up more memory when needed and put the buffers with the data of the
    file on a sort of stack.
    The problem is that i have no idea how to do this.

    My question is: Does anyone of you have an example of how to read in an
    file and put it into memory using a singel linked list or maybe know a
    site where I can find an example?

    Thanks in advance.

    Greetings Arno
     
    Henk, Sep 29, 2005
    #1
    1. Advertising

  2. Henk

    Eric Sosman Guest

    Re: Dynamic memory allocation:Reading a file into buffers using asingle linked list!

    Henk wrote On 09/29/05 11:02,:
    > Hi,
    >
    > I am new to the c-programming language and at the moment I am
    > struggling with the following:
    >
    > I want to read a file using fread() and then put it in to memory. I
    > want to use a (singel) linked list to continousely (dynamically) free
    > up more memory when needed and put the buffers with the data of the
    > file on a sort of stack.
    > The problem is that i have no idea how to do this.
    >
    > My question is: Does anyone of you have an example of how to read in an
    > file and put it into memory using a singel linked list or maybe know a
    > site where I can find an example?


    One way is to use a struct object to hold each
    chunk of data, plus a pointer to the struct that
    holds the next chunk:

    struct chunk {
    struct chunk_s *next;
    Whatever data;
    };

    You'll also need a pointer to the first chunk
    so you can find the start of the list after you're
    done reading, and a pointer to the last chunk read
    so far so you know where to add the next one, and
    a pointer to the chunk you're currently reading:

    struct chunk *head = NULL;
    struct chunk *tail;
    struct chunk *this;

    To read the data, you'll allocate memory for a
    chunk and read into its `data' portion. If there's
    nothing left to read, discard the unused chunk and
    return `head' as a pointer to the start of the list.
    Otherwise, append the newly-read chunk to the list
    and repeat:

    for (;;) {
    this = malloc(sizeof *this);
    if (this == NULL)
    ... die ...
    ... read into `this->data' ...
    if (... end of file ...) {
    free (this);
    return head;
    }
    if (... I/O error ...)
    ... die ...
    if (head == NULL) /* the first chunk */
    head = this;
    else /* subsequent chunks */
    tail->next = this;
    tail = this;
    this->next = NULL;
    }

    There are lots of variations on this approach.
    For example, you can do get rid of the `tail' pointer
    and the `if (head == NULL)' stuff by building the list
    backwards and then reversing it before returning. Or
    there might be a `size' field in the `struct chunk' to
    state how much of the `data' is actually used. Or ...

    --
     
    Eric Sosman, Sep 29, 2005
    #2
    1. Advertising

  3. Henk wrote:
    > Hi,
    >
    > I am new to the c-programming language and at the moment I am
    > struggling with the following:
    >
    > I want to read a file using fread() and then put it in to memory. I
    > want to use a (singel) linked list to continousely (dynamically) free
    > up more memory when needed and put the buffers with the data of the
    > file on a sort of stack.
    > The problem is that i have no idea how to do this.
    >
    > My question is: Does anyone of you have an example of how to read in an
    > file and put it into memory using a singel linked list or maybe know a
    > site where I can find an example?
    >
    > Thanks in advance.
    >
    > Greetings Arno


    If you want to use dynamic arrays to accomplish the same task, just
    grab FreeDOS Edlin, available from ibiblio or alt.sources.

    Gregory Pietsch
     
    Gregory Pietsch, Sep 29, 2005
    #3
  4. Henk

    Hemanth Guest

    > I want to read a file using fread() and then put it in to memory. I
    > want to use a (singel) linked list to continousely (dynamically) free
    > up more memory when needed and put the buffers with the data of the
    > file on a sort of stack.
    > The problem is that i have no idea how to do this.
    >


    ......I believe, a queue implementation suits well for this case (for
    eg. if you want to process file with size larger than main memory, you
    have to read it in chunks of constant size and store it in a buffer).
    Here the queue can act as a buffer.

    Queue can be implemented using a single linked list (maintain both
    first and last pointers, always add nodes at Last and remove First Node
    --FIFO)
    Eg.

    struct node
    {
    char* buffer; //Replace char* with whatever you want
    struct node* first;
    struct node* last;
    };
    - Construct a queue with "n" nodes;
    - Remove first node (process data and clear buffer)
    - Read another chunk of data from file and add it as a last node in
    queue.

    You can keep loop the last two steps.


    > My question is: Does anyone of you have an example of how to read in an
    > file and put it into memory using a singel linked list or maybe know a
    > site where I can find an example?
    >


    .......search for a Queue implementation on google.


    - Hemanth
     
    Hemanth, Sep 29, 2005
    #4
  5. Henk

    Henk Guest

    Thanks for all the info guy's it really helped me a lot

    Greetings Henk
     
    Henk, Oct 4, 2005
    #5
    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. dough
    Replies:
    6
    Views:
    755
    Skarmander
    Oct 4, 2005
  2. fool
    Replies:
    14
    Views:
    512
    Barry Schwarz
    Jul 3, 2006
  3. Ken
    Replies:
    24
    Views:
    3,875
    Ben Bacarisse
    Nov 30, 2006
  4. chris
    Replies:
    6
    Views:
    992
    chris
    Oct 28, 2005
  5. joshd
    Replies:
    12
    Views:
    674
    John Carson
    Oct 2, 2006
Loading...

Share This Page