10/16 In C, What's the difference between offset_t and ptrdiff_t? I used to
use size_t for variables representing offsets, but now I think size_t
might not be right. Thanks. --- yuen
\_ I don't know for certain, but here's my thinking:
ptrdiff_t is signed and should be sufficient to store the
displacement between two items of the same type. size_t (produced
by sizeof) is unsigned and should be sufficient to store the
size of a type. offset_t (produced by offsetof) is unsigned (I
think) and should be sufficient to store the offset of an element
within a type. Therefore, aside from the sign differences,
the range of ptrdiff_t may be greater than that of size_t which
\_ I take this back. It makes
no sense. (Not that the rest
does.) --jameslin
may be greater than that of offset_t. Maybe you should ask on
comp.lang.c. --jameslin
\_ size_t is usually 32 bits, and is used to represent the size
of objects or buffers and the like, off_t is used to represent
offsets in files and is often 64bits. Why does this matter
though? Outside the interfaces these are used in, there
is no special meaning to them.
\_ I see. off_t (and offset_t on SunOS5) is for file offsets,
not memory offsets. So I should use ptrdiff_t for memory
offsets then. Thanks. --- yuen |