fopen and memory usage

Cameron Simpson cs at zip.com.au
Thu Sep 27 10:53:54 UTC 2007


On 26Sep2007 13:58, aragonx at dcsnow.com <aragonx at dcsnow.com> wrote:
| Okay, so I use fopen and it gives me a stream.  Now when I call fread (say
| for 100K) on my large file (2G) it will try to read the first 64K
| regardless of size and populate *ptr.

Yes. It will read a chunk of the data into the buffer. For a regular
file it will usually be the whole 64kb. For some other things it may be
less (eg a terminal).

| If size is larger than 64K (100K
| here) it will then read another 64K?

After you, the programmer, have consumed the data currently in the
buffer, yes. If you call fread (or scanf etc) with data in the buffer,
that data will be used to satisfy your reuqest. If more is needed, more
is read from the file into the (now empty) buffer).

| Does it use the same buffer space
| (effectively only using 64K)

Yes.

| and is that buffer space outside the memory
| footprint of my application?

No. fopen uses stdin, and that is a library inside your app.
-- 
Cameron Simpson <cs at zip.com.au> DoD#743
http://www.cskk.ezoshosting.com/cs/

Early to bed and early to rise makes a man healthy and wealthy with bags
under his eyes. - Dave Cochran, <cochran at dg-rtp.dg.com>




More information about the fedora-list mailing list