向size_t添加或分配整数文字 [英] Adding or assigning an integer literal to a size_t

查看:188
本文介绍了向size_t添加或分配整数文字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在C语言中,我看到了很多代码,它们为size_t变量添加或分配整数文字.

In C I see a lot of code that adds or assigns an integer literal to a size_t variable.

size_t foo = 1;
foo += 1;

这里发生了什么转换?将size_t升级"为int,然后再转换回size_t时,会发生这种情况吗?如果我是最大的话,那还会环绕吗?

What conversion takes place here, and can it ever happen that a size_t is "upgraded" to an int and then converted back to a size_t? Would that still wraparound if I was at the max?

size_t foo = SIZE_MAX;
foo += 1;

这是定义的行为吗?这是一个无符号的类型size_t,其中添加了一个有符号的int(可能是更大的类型?),然后转换回了size_t.有符号整数溢出的风险吗?

Is that defined behavior? It's an unsigned type size_t which is having a signed int added to it (that may be a larger type?) and the converted back to a size_t. Is there risk of signed integer overflow?

写类似foo + bar + (size_t)1而不是foo + bar + 1有意义吗?我从未见过像这样的代码,但是我想知道整数促销是否有麻烦是否必要.

Would it make sense to write something like foo + bar + (size_t)1 instead of foo + bar + 1? I never see code like that, but I'm wondering if it's necessary if integer promotions are troublesome.

C89没有说明size_t的排名方式或确切的含义:

C89 doesn't say how a size_t will be ranked or what exactly it is:

结果的值是实现定义的,其类型(无符号整数类型)是在标头中定义的size_t.

The value of the result is implementation-defined, and its type (an unsigned integral type) is size_t defined in the header.

推荐答案

当前的C标准允许执行以下代码时可能导致未定义行为的实现,但是这种实现不存在,而且可能永远不会:

The current C standard allows for a possibility of an implementation that would cause undefined behavior when executing the following code, however such implementation does not exist, and probably never will:

size_t foo = SIZE_MAX;
foo += 1;

size_t类型为无符号类型 1 ,最小范围为: 2 [0,65535].

The type size_t is as unsigned type1, with a minimum range:2 [0,65535].

可以将类型size_t定义为无符号short类型的同义词.无符号短类型可以定义为具有16个精度位,范围为[0,65535].在这种情况下,SIZE_MAX的值为65535.

The type size_t may be defined as a synonym for the type unsigned short. The type unsigned short may be defined having 16 precision bits, with the range: [0,65535]. In that case the value of SIZE_MAX is 65535.

可以将int类型定义为具有16个精度位(加上一个符号位),二进制补码表示形式和范围:[-65536,65535].

The type int may be defined having 16 precision bits (plus one sign bit), two's complement representation, and range: [-65536,65535].

表达式foo + = 1等效于foo = foo + 1(除了foo仅被评估一次,但此处无关紧要).变量foo将使用整数Promotions 3 进行提升.它将被提升为int类型,因为int类型可以表示type_t类型的所有值,并且size_t的等级(作为unsigned short的同义词)低于int的等级.由于size_t和int的最大值相同,因此计算将导致有符号的溢出,从而导致未定义的行为.

The expression foo += 1, is equivalent to foo = foo + 1 (except that foo is evaluated only once but that is irrelevant here). The variable foo will get promoted using integer promotions3. It will get promoted to type int because type int can represent all values of type size_t and rank of size_t, being a synonym for unsigned short, is lower than the rank of int. Since the maximum values of size_t, and int are the same, the computation causes a signed overflow, causing undefined behavior.

这适用于当前标准,并且也适用于C89,因为它对类型没有任何更严格的限制.

This holds for the current standard, and it should also hold for C89 since it doesn't have any stricter restrictions on types.

对于任何可能的实现,避免有符号溢出的解决方案是使用无符号int整数常量:

Solution for avoiding signed overflow for any imaginable implementation is to use an unsigned int integer constant:

foo += 1u;

在这种情况下,如果foo的等级低于int,则将使用常规的算术转换将其提升为unsigned int.

In that case if foo has a lower rank than int, it will be promoted to unsigned int using usual arithmetic conversions.

1 (引自ISO/IEC 9899/201x 7.19通用定义2)
size_t
这是sizeof运算符结果的无符号整数类型;

1 (Quoted from ISO/IEC 9899/201x 7.19 Common definitions 2)
size_t
which is the unsigned integer type of the result of the sizeof operator;

2 (引自ISO/IEC 9899/201x 7.20.3其他整数类型2的限制)
size_t的限制
SIZE_MAX 65535

2 (Quoted from ISO/IEC 9899/201x 7.20.3 Limits of other integer types 2)
limit of size_t
SIZE_MAX 65535

3 (引自ISO/IEC 9899/201x 6.3.1.1布尔,字符和整数2)
在可能为int或unsigned int的表达式中可以使用以下内容 可以使用:
具有整数类型的对象或表达式(int或unsigned int除外) 其整数转换等级小于或等于int的等级,并且 无符号整数.
如果一个int可以表示原始类型的所有值(受宽度限制,对于 位字段),将值转换为int;否则,它将转换为无符号 诠释这些称为整数促销.所有其他类型均未更改 整数促销.

3 (Quoted from ISO/IEC 9899/201x 6.3.1.1 Boolean, characters, and integers 2)
The following may be used in an expression wherever an int or unsigned int may be used:
An object or expression with an integer type (other than int or unsigned int) whose integer conversion rank is less than or equal to the rank of int and unsigned int.
If an int can represent all values of the original type (as restricted by the width, for a bit-field), the value is converted to an int; otherwise, it is converted to an unsigned int. These are called the integer promotions. All other types are unchanged by the integer promotions.

这篇关于向size_t添加或分配整数文字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆