Say hello to size_t and ptrdiff_t.
size_t is the "the unsigned integer type of of the result of the sizeof operator"
ptrdiff_t is the "signed integer type of the result of subtracting two pointers"
These two types are not supposed to be same size all the time, but mostly on all systems I care about they are. They differ based on 64-bit and 32-bit systems. e.g. on 32-bits they are both 32-bits, but on 64-bits they are 64-bits integers.
How to use these types? size_t can represent the number of items you store in MyArray. ptrdiff_t can be used for iterator arithmetic for MyArray's iterators. For example MyArray may provide random access iterators which should support things like ptr-=
What's off_t doing in this post? Well, it is a type used to pass offset to various file related functions, such as ftell etc... In 32-bit systems usually it is declared as 32-bit signed integer, since we might want to give a negative offset compared to current file position. But this prevents us to open files larger than 2GB, or at least we can't we can't read from offsets larger than 2GB. To solve this problem many systems define another type called off64_t and related functions. e.g. in addition to pread, they define pread64. In such systems, we either need to use the regular open function with O_LARGEFILE flag or directly use open64 function.
No comments:
Post a Comment