473,320 Members | 1,746 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,320 software developers and data experts.

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

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

Nov 15 '05 #1
4 3808


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

--
Er*********@sun.com

Nov 15 '05 #2

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

Nov 15 '05 #3
> 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

Nov 15 '05 #4
Thanks for all the info guy's it really helped me a lot

Greetings Henk

Nov 15 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

0
by: TNGgroup | last post by:
Hi, I have a dynamic list box with all my records. Whan selecting a value in this box I want to be able to delete the value. but no matter what my selection is, He always deleting the first...
8
by: jmbn2k | last post by:
Hi Can anyone pls teme how to allocate memory using malloc in a 3 D diemensional array.............
13
by: shsingh | last post by:
I have a class A containing some map as data variables. I creat an object of class A on heap by allocatiing memory by using "malloc". This will return me the required memory but the object is not...
11
by: tara99 | last post by:
I was wondering if can some help me with creating a dynamic list box. I have a combobox with the list of items (item1, item2, item3). item1 has 5 sub items item2 has 10 sub items item3 has 2...
5
subashini Thiyagarajan
by: subashini Thiyagarajan | last post by:
hi, any one can help me in dynamic list box selection with simple codings. when the first list box item selected depents on the selected item another list box item should change.
8
by: yfguo | last post by:
Do somebody know how to come up with a dynamic file name when ftp it ? I need to download a monthly updated file. The file name appears like datafile_yyyymm.zip where yyyy mean year 2008, and mm mean...
6
by: dasrasmikant | last post by:
Hello, I am new in Ajax. I want to create Ajax dynamic list with ASP and XML. can anybody will help me regarding This. Thanks in advance..
2
by: =?Utf-8?B?SmFzb24gQmFybmV0dA==?= | last post by:
I wrote an aspx page that creates a .pdf file upon request and transfers the file to the client for viewing (via an Adobe Acrobat plug-in). Afterwards, the file is no longer needed and should be...
4
by: pghantasala | last post by:
How to get available physical memory using javascript NOT USING WMI ?? any other way? I need to achieve this by using javascript/vbscript and I cannot use WMI. Please let me know the possibilities.
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.