Take the 2-minute tour ×
Unix & Linux Stack Exchange is a question and answer site for users of Linux, FreeBSD and other Un*x-like operating systems.. It's 100% free, no registration required.

From Robert Love's Linux System Programming (2007, O'Reilly), this is what is given in the first paragraph (Chapter 1, Page 10):

The file position’s maximum value is bounded only by the size of the C type used to store it, which is 64-bits in contemporary Linux.

But in the next paragraph he says:

A file may be empty (have a length of zero), and thus contain no valid bytes. The maximum file length, as with the maximum file position, is bounded only by limits on the sizes of the C types that the Linux kernel uses to manage files.

I know this might be very, very basic, but is he saying that the file size is limited by the FILE data type or the int data type?

share|improve this question

1 Answer 1

up vote 5 down vote accepted

He's saying it's bound by a 64-bit type, which has a maximum value of (2 ^ 64) - 1 unsigned, or (2 ^ 63) - 1 signed (1 bit holds the sign, +/-).

The type is not FILE; it's what the implementation uses to track the offset into the file, namely off_t, which is a typedef for a signed 64-bit type.1 (2 ^ 63) - 1 = 9223372036854775807. If a terabyte is 1000 ^ 4 bytes, that's ~9.2 million TB. Presumably the reason a signed type is used is so that it can hold a value of -1 (for errors, etc), or a relative offset.

Functions like fseek() and ftell() use a signed long, which on 64-bit GNU systems is also 64-bits.


1. See types.h and typesizes.h in /usr/include/bits.

share|improve this answer
1  
It is bound by type off_t. This will be typedefed to int64 for the next few years. However history tells us that this will change, it used to be 32 bit. So always use the correct type off_t or your program will become obsolete. (not size_t: in 32bit system size_t is 32bit, off_t is not) –  richard Jul 16 at 13:56
    
@richard Thanks for the clarification -- corrected. –  goldilocks Jul 16 at 14:28
    
The range of a N-bit integer type without special tricks is 0 .. (2^N - 1) for unsigned, and -(2^(N-1)) .. +(2^(N-1) - 1) for signed, all inclusive, given a two-complement architecture (not guaranteed with C). Both allow 2^N discrete values, but the range is shifted. Hence, unsigned 16 bits integer is 0..65535 inclusive (2^16 = 65536), and signed 16 bits integer is -32768..32767 inclusive (2^15 = 32768). For longer integers, just use larger two-exponents. –  Michael Kjörling Jul 16 at 14:36
    
As for fseek() in particular, note that it can seek from the current position or file end, hence the requirement to be able to take a negative offset. –  Michael Kjörling Jul 16 at 14:39
    
@MichaelKjörling Right -- I forgot the zero. So 9223372036854775807. Added -1 to the range formula. –  goldilocks Jul 16 at 15:38

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.