简体   繁体   English

size_t总是未签名?

[英]is size_t always unsigned?

作为标题:size_t总是无符号的,即对于size_t xx总是>= 0

According to the 1999 ISO C standard (C99), size_t is an unsigned integer type of at least 16 bit (see sections 7.17 and 7.18.3). 根据1999 ISO C标准(C99), size_t是至少16位的无符号整数类型(参见第7.17和7.18.3节)。

The standard also recommends that size_t shouldn't have an integer conversion rank greater than long if possible, ie casting size_t to unsigned long is unproblematic if the recommendation is followed. 该标准还建议,如果可能, size_t不应具有大于long的整数转换等级,即如果遵循建议,则将size_tunsigned long是没有问题的。

The 1989 ANSI C standard (ANSI C) doesn't mention a minimal size or recommended conversion rank. 1989 ANSI C标准(ANSI C)未提及最小尺寸或推荐的转换等级。

The 1998 ISO C++ standard (C++98) (as well as the current draft for C++0x) refers to the C standard. 1998 ISO C ++标准(C ++ 98)(以及当前的C ++ 0x草案)是指C标准。 Section 18.1 reads: 第18.1节内容如下:

The contents are the same as the Standard C library header <stddef.h> [...] 内容与标准C库头文件<stddef.h> [...]相同

According to section 1.2, this means the library as defined by the 1990 ISO C standard (C90), including its first amendment from 1995 (C95): 根据第1.2节,这意味着1990年ISO C标准(C90)定义的图书馆,包括1995年的第一次修订(C95):

The library described in clause 7 of ISO/IEC 9899:1990 and clause 7 of ISO/IEC 9899/Amd.1:1995 is hereinafter called the Standard C Library . ISO / IEC 9899:1990的第7节中描述的库和ISO / IEC 9899 / Amd.1:1995的第7节在下文中称为标准C库

The parts regarding size_t should be inherited from ANSI C: Frontmatter and section numbering aside, the standards for C90 and ANSI C are identical. 关于size_t的部分应该继承自ANSI C:Frontmatter和部分编号,C90和ANSI C的标准是相同的。 I'd need a copy of the normative amendment to be sure that there weren't any relevant changes to stddef.h , but I doubt it. 我需要一份规范性修正案的副本,以确保stddef.h没有任何相关的变化,但我对此表示怀疑。 The minimal size seems to be introduced with stdint.h , ie C99. 最小的大小似乎是用stdint.h引入的,即C99。

Please also consider the following quote from section 1.2 of C++98: 还请考虑C ++ 98第1.2节中的以下引用:

All standards are subject to revision, and parties to agreements based on this International Standard are encouraged to investigate the possibility of applying the most recent editions of the standards indicated below. 所有标准都可以修订,鼓励根据本国际标准达成协议的各方研究是否可以应用下列标准的最新版本。

Yes . 是的 It's usually defined as something like the following (on 32-bit systems): 通常被定义为类似以下内容(在32位系统上):

typedef unsigned int size_t;

Reference: 参考:

C++ Standard Section 18.1 defines size_t is in <cstddef> which is described in C Standard as <stddef.h> . C ++标准第18.1节定义size_t<cstddef> ,在C标准中描述为<stddef.h>
C Standard Section 4.1.5 defines size_t as an unsigned integral type of the result of the sizeof operator C标准第4.1.5节将size_t定义为sizeof运算符结果的无符号整数类型

是的,size_t保证是无符号类型。

According to the standard it is unsigned, however I recall that some older implementations used a signed type for the typedef. 根据标准,它是无符号的,但我记得一些较旧的实现使用了带有签名类型的typedef。

From an older GCC doc: 来自较旧的GCC文档:

There is a potential problem with the size_t type and versions of GCC prior to release 2.4. 在版本2.4之前,size_t类型和GCC版本存在潜在问题。 ANSI C requires that size_t always be an unsigned type. ANSI C要求size_t始终是无符号类型。 For compatibility with existing systems' header files, GCC defines size_t in stddef.h to be whatever type the system's sys/types.h defines it to be. 为了与现有系统的头文件兼容,GCC将stddef.h size_t定义为系统的sys/types.h定义的类型。 Most Unix systems that define size_t in sys/types.h , define it to be a signed type. 大多数在sys/types.h中定义size_t Unix系统将其定义为有符号类型。 Some code in the library depends on size_t being an unsigned type, and will not work correctly if it is signed 库中的某些代码依赖于size_t是无符号类型,如果签名则无法正常工作

I'm not sure how important it would be to guard against that. 我不确定防范这一点有多重要。 My code assumes it's unsigned. 我的代码假定它是未签名的。

The size_t should follow the same definition as the C standard, and in several places in the C++ standard it implies it's unsigned natura (particularly in the allocator template argument definitions). size_t应该遵循与C标准相同的定义,并且在C ++标准中的几个地方它暗示它是无符号的natura(特别是在allocator模板参数定义中)。

On the C++ Standard, section 18.1 (ISO/IEC 14882 - First edition 1998-01-01): 在C ++标准中,第18.1节(ISO / IEC 14882 - 第一版1998-01-01):

Table 15 lists as defined types: ptrdiff_t and size_t 表15列出了定义的类型:ptrdiff_t和size_t

3 The contents are the same as the Standard C library header , with the following changes: 4 The macro NULL is an implementation-defined C++ null pointer constant in this International Standard (4.10). 3内容与标准C库头相同,但有以下更改:4宏NULL是本国际标准(4.10)中实现定义的C ++空指针常量。

The macro offsetof accepts a restricted set of type arguments in this International Standard. 宏offsetof在本国际标准中接受一组受限制的类型参数。 type shall be a POD structure or a POD union (clause 9). type应为POD结构或POD联合(第9条)。 The result of applying the offsetof macro to a field that is a static data member or a function member is undefined. 将offsetof宏应用于作为静态数据成员或函数成员的字段的结果是未定义的。 SEE ALSO: subclause 5.3.3, Sizeof, subclause 5.7, Additive operators, subclause 12.5, Free store, and ISO C subclause 7.1.6. 还请参见:子条款5.3.3,Sizeof,子条款5.7,附加运算符,子条款12.5,自由存储和ISO C子条款7.1.6。

Oh, this is just terrible: 哦,这太糟糕了:

vector<MyObject> arr;
Fill(arr);
size_t size = arr.size();
for(size_t i = 1; i < size - 1; ++i)
{
  auto obj = arr[i];
  auto next = arr[i+1];
}

Now contemplate the use case where arr is empty. 现在考虑arr为空的用例。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM