473,714 Members | 2,475 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Dynamic memory allocation:Read ing 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 3847


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
1236
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 record. ?? This is my code: <form action="<%=MM_editAction%>" method="POST" name="form_remove" class="content"d="form_remove">
8
7713
by: jmbn2k | last post by:
Hi Can anyone pls teme how to allocate memory using malloc in a 3 D diemensional array.............
13
9720
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 initialized properly as constructor same is not get called ( as per the behavior). How to call a constructor explicitly if we want to allocate memory using malloc ?
11
11763
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 sub items I want the size of the list box to change depending on the number of sub items available. I hope this is clear.
5
2862
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
10425
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 month 02.
6
2585
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
1440
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 deleted from the server. Here is a code snippet as an example: Response.ContentType = "application/pdf"; Response.Clear(); Response.TransmitFile(filename); Response.Flush(); //Response.End();
4
7889
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
8704
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9170
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9071
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9009
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
1
6627
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 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 a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4462
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4715
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3155
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
3
2105
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.