What is the difference between ssize_t and ptrdiff_t?

我的梦境 提交于 2019-12-17 18:51:06

问题


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?

回答1:


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




回答2:


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.



来源:https://stackoverflow.com/questions/8649018/what-is-the-difference-between-ssize-t-and-ptrdiff-t

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!