linux size t 头文件,sszie_t和size_t 的解释,是不是有问题啊。

ssize_t是什么类型的?

解释一:为了增强程序的可移植性,便有了size_t,它是为了方便系统之间的移植而定义的,不同的系统上,定义size_t可能不一样。

在32位系统上 定义为 unsigned int

也就是说在32位系统上是32位无符号整形。在64位系统上定义为 unsigned long

也就是说在64位系统上是64位无符号整形。size_t一般用来表示一种计数,比如有多少东西被拷贝等。例如:sizeof操作符的结果类型是size_t,该类型保证能容纳实现所建立的最大对象的字节大小。

它的意义大致是“适于计量内存中可容纳的数据项目个数的无符号整数类型”。所以,它在数组下标和内存管理函数之类的地方广泛使用。而ssize_t这个数据类型用来表示可以被执行读写操作的数据块的大小.它和size_t类似,但必需是signed.意即:它表示的是signed

size_t类型的。

typedef unsigned long size_t

但是我看到,

#ifndef __SIZE_T

#define __SIZE_T

typedef unsigned int size_t;   实际上是平***立的,看下面wiki的说法,这个可能是作者针对她的平台指定的

#endif

size_t isthe unsigned integer type of the result of the sizeof operator (and of the offsetof operator) so it is guaranteed to be big enough to contain the size of the biggest object your system can handle (e.g. a static array of 8Gb).

It may be bigger, equal or even smaller than an unsigned int and your compiler might make assumption about it for optimization purpose.

You may find more precise information on the C99 standard (section 7.17), a draft of which is available on the Internet in pdf format.

size_t

From Wikipedia, the free encyclopedia

size_t isan unsigned data type defined by several C and C++ standards (e.g., the C99 ISO/IEC 9899 standard) that is defined in stddef.h.[1] It can be further imported by inclusion of stdlib.h as this file internally sub includes stddef.h[2].

This type is used to represent the size of an object or the maximum size of an array.Library functions that take or return sizes expect them to be of this type or have the return type of size_t. Further, the most frequently used compiler-based operator sizeof should evaluate to a value that is compatible with size_t.

[edit]Range and storage size of size_t

The actual type of size_t is platform-dependent; a common mistake is to assume size_t is the same as unsigned int, which can lead to programming errors[3][4] for example when moving from 32 to 64-bit architectures.

The C99 standard introduced the SIZE_MAX constant, defined in the stdint.h header, which specifies the maximum value a size_t can hold. The actual value for SIZE_MAX is implementation-defined, but the standard requires it to be at least 65535.[5]

[edit]Signed variant ssize_t

On platforms supporting the POSIX 1003.1-1996 API standard, which includes most Unix-like systems, a signed variant of size_t named ssize_t is available, which was not part of the ANSI or ISO C standards. The ssize_t type follows the same rules as size_t for its storage size, location and inclusion, and uses the implementation's usual representation of signed values—2's complement in most, if not all, cases. It is frequently used for pointer differences (but see also ptrdiff_t), or for returning sizes in cases where negative values are used as an error value.

解释二:ssize_t是signed size_t,

size_t是标准C库中定义的,应为unsigned int。定义为typedef int

ssize_t。

而ssize_t:这个数据类型用来表示可以被执行读写操作的数据块的大小.它和size_t类似,但必需是signed.意即:它表示的是sign

size_t类型的。

《Unix 高级环境编程》里面是这么说的:

原始系统数据类型

前面所示的g e t p i d函数的原型定义了其返回值为p i d _

t类型,这也是P O S I X中的新规定。

U N I X的早期版本规定此函数返回一整型。与此类似, r e a d和w r i t e返回类型为s s

i z e _ t的值,并

要求第三个参数的类型是s i z e _ t。

以_

t结尾的这些数据类型被称为原始系统数据类型。它们通常在头文件< s y s / t y p e s . h >中定

义(头文件< u n

i s t d . h >应已包括该头文件)。它们通常以C typedef说明加以定义。t y p e d e f说明在C

语言中已超过1

5年了(所以这并不要求ANSI C),它们的目的是阻止程序使用专门的数据类型

(例如i n t , s h o r t或long)

来允许对于一种特定系统的每个实现选择所要求的数据类型。在需要存储

进程I D的地方,分配类型为p i d _ t的一个变量(注意,程序1 - 5已对名为p

i

d的变量这样做了)。在

各种不同的实现中,这种数据类型的定义可能是不同的,但是这种差别现在只出现在一个头文

件中。我们只需在另一个系统上重新编辑应用程序。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值