What is the difference between ssize_t and ptrdiff_t?

前端 未结 2 468
眼角桃花
眼角桃花 2020-12-09 14:47

The C standard (ISO/IEC 9899:2011 or 9899:1999) defines a type ptrdiff_t in .

The POSIX standard (ISO/IEC 9945; IEEE Std 10

相关标签:
2条回答
  • 2020-12-09 15:03

    The Open Group Base Specifications Issue 7, IEEE Std 1003.1, 2013 Edition, description of <sys/types.h> says:

    The type ssize_t is capable of storing values at least in the range [-1, SSIZE_MAX].

    In other words, ssize_t is signed, but the set of negative values it can represent may be limited to just {-1}.

    A ptrdiff_t, on the other hand, is guaranteed to have a more symmetric positive/negative range.

    I admit that in practice, it doesn't seem likely that ssize_t would be this limited in the negative range, but it is possible.

    Of course, another difference is that ptrdiff_t is available whenever you're programming in standard C or C++, but ssize_t may not be available unless you're targeting a standard POSIX system.

    0 讨论(0)
  • 2020-12-09 15:20

    Is there an implementation where the underlying base type for ssize_t is not the same as for ptrdiff_t?

    x86-16 with the large memory model. Pointers are far (32-bit), but individual objects are limited to one segment (so size_t is allowed to be 16-bit).

    0 讨论(0)
提交回复
热议问题