What is the difference between ssize_t and ptrdiff_t?

CPosix

C Problem Overview


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

The POSIX standard (ISO/IEC 9945; IEEE Std 1003.1-2008) defines a type ssize_t in <sys/types.h>.

  • What is the difference between these types (or why were both deemed necessary)?
  • Is there an implementation where the underlying base type for ssize_t is not the same as for ptrdiff_t?

C Solutions


Solution 1 - C

> 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).

Solution 2 - C

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.

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
QuestionJonathan LefflerView Question on Stackoverflow
Solution 1 - Cdan04View Answer on Stackoverflow
Solution 2 - CAdrian McCarthyView Answer on Stackoverflow