Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What are the usage differences between size_t and off_t?

Tags:

c++

linux

Other than the size of the values that each type can hold, what are the main differences in usage between size_t and off_t? Is it just a convention that size_t types are used for absolute sizes and off_t types are used for offsets? Or does it go deeper than that?

I am writing a wrapper class to enable the writing of large files using mmap and I want to know what the best types are to use for their arguments. Given that I want to write to files > 4GB, I'm tempted to use size_t for everything, but is that the best practice? (or should I be using some off64_t types for certain functions?)

For example, should my writeAt function be declared as:

MMapWriter::writeAt(off64_t offset, const void* src, size_t size)

or

MMapWriter::writeAt(size_t offset, const void* src, size_t size)
like image 909
Lee Netherton Avatar asked May 17 '12 11:05

Lee Netherton


People also ask

Why is Size_t used?

Using size_t appropriately can improve the portability, efficiency, or readability of your code. Maybe even all three. Numerous functions in the Standard C library accept arguments or return values that represent object sizes in bytes.

Is Off_t signed or unsigned?

This is a data type defined in the sys/types. h header file (of fundamental type unsigned long) and is used to measure the file offset in bytes from the beginning of the file.

What is Off_t?

off_t is normally defined as a signed, 32-bit integer. In the programming environment which enables large files, off_t is defined to be a signed, 64-bit integer. offset_t. 64-bit file offset, measured in bytes from the beginning of a file or device.

What is the definition of Size_t?

The datatype size_t is unsigned integral type. It represents the size of any object in bytes and returned by sizeof operator. It is used for array indexing and counting. It can never be negative. The return type of strcspn, strlen functions is size_t.


2 Answers

size_t is for objects, off_t is for files.

mmap merges the two concepts, pretty much by definition. Personally I think I'd use size_t, since no matter what else it is, a mapped file is also an array in (virtual) memory.

size_t is standard C++, off_t is Posix, and off64_t is a GNU extension that goes with the functions fopen64, ftello64, etc. I think it should always be the same type as off_t on 64 bit GNU systems, but don't bet your company on that without checking.

Should it be relevant, off_t is signed whereas size_t is unsigned. But the signed counterpart to size_t is ptrdiff_t, so when you need a signed type it doesn't automatically mean you should use off_t or off64_t.

like image 187
Steve Jessop Avatar answered Oct 09 '22 07:10

Steve Jessop


size_t is part of the C++ (and C) standards, and refers to the type of a sizeof expression. off_t is defined by the Posix standard, and refers to the size of a file.

like image 26
James Kanze Avatar answered Oct 09 '22 07:10

James Kanze