What is the difference between ssize_t and ptrdiff_t?

前端 未结 2 470
眼角桃花
眼角桃花 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 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.

提交回复
热议问题