How to manage the memory in C?

Y

yezi

If the file is very huge, but it is needed to be read in the memory,
how to manage the memory,? Thanks for any comment.
 
M

Mike Wahler

yezi said:
If the file is very huge, but it is needed to be read in the memory,
how to manage the memory,? Thanks for any comment.

Determine the file's size (can be done by opening it
and reading, counting bytes until eof) then allocate
that much memory, and read it in. If the memory allocation
fails, then the whole file won't fit in memory. In that case
you can read what part will fit, then when done with it, read a
different sufficiently small part of the file into that same
memory. Repeat as necessary. (this is known as 'paging', and
is done automatically on systems which feature 'virtual' memory.)

-Mike
 
J

Joe Wright

yezi said:
If the file is very huge, but it is needed to be read in the memory,
how to manage the memory,? Thanks for any comment.
You need to be more precise. What does 'very huge' mean? How much memory
is available to you? What do you mean by 'managing memory'.
 
R

Richard Heathfield

yezi said:
If the file is very huge, but it is needed to be read in the memory,
how to manage the memory,? Thanks for any comment.

Read as little (from the file into memory) as possible but as much as
necessary. If "as much as necessary" is more than you can handle, find a
different algorithm.

For example, if you're right-justifying some text, you need only read one
line into memory at a time.
 

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
474,173
Messages
2,570,939
Members
47,475
Latest member
NovellaSce

Latest Threads

Top