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

H

Henk

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
 
E

Eric Sosman

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 ...
 
G

Gregory Pietsch

Henk said:
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
 
H

Hemanth

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,770
Messages
2,569,586
Members
45,087
Latest member
JeremyMedl

Latest Threads

Top