应该发生什么一个为size_t(即`-sizeof(结构富)`))的否定? [英] What should happen to the negation of a size_t (i.e. `-sizeof(struct foo)`))?

查看:112
本文介绍了应该发生什么一个为size_t(即`-sizeof(结构富)`))的否定?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我处理在工作中的一些code,包括形式的前pression

I'm dealing with some code at work that includes an expression of the form

-(sizeof(struct foo))

即。的否定一个为size_t ,我什么C和C ++标准要求编译器,当他们看到这个不清楚。具体来说,环视这里和其他地方,的sizeof 返回类型的无符号整数值为size_t 。否定一个无符号整数,当我找不到指定的行为的任何明确的参考。有没有,如果有,是什么呢?

i.e. the negation of a size_t, and I'm unclear on what the C and C++ standards require of compilers when they see this. Specifically, from looking around here and elsewhere, sizeof returns an unsigned integral value of type size_t. I can't find any clear reference for specified behavior when negating an unsigned integer. Is there any, and if so, what is it?

编辑:好了,所以有关于对无符号类型算术一些很好的答案,但目前还不清楚这是事实,这样的。当这种否定,是它在操作一个无符号整数,或者转换为符号的类型,做用的东西?从标准,以期望想象中那么的类似规模的负数,然后应用'溢出'规则无符号价值的行为?

Ok, so there are some good answers regarding arithmetic on unsigned types, but it's not clear that this is in fact such. When this negates, is it operating on an unsigned integer, or converting to a signed type and doing something with that? Is the behavior to expect from the standards "imagine it's the negative number of similar magnitude and then apply the 'overflow' rules for unsigned values"?

推荐答案

这两个ISO C和ISO C ++标准,保证无符号运算是模2 N - 也就是说,对于任何溢出或下溢,它环绕。对于ISO C ++,这是3.9.1 [basic.fundamental / 4:

Both ISO C and ISO C++ standards guarantee that unsigned arithmetic is modulo 2n - i.e., for any overflow or underflow, it "wraps around". For ISO C++, this is 3.9.1[basic.fundamental]/4:

无符号整数,宣布无符号,应当服从算术模的法律2 N 其中的 N 的是多少在整数的特定大小。 41

Unsigned integers, declared unsigned, shall obey the laws of arithmetic modulo 2n where n is the number of bits in the value representation of that particular size of integer.41

...

41)这意味着,无符号算术不会溢出,因为这不能被重新由所得的无符号整数psented $ P $结果
  类型被减小模数比,可以重复由所得的无符号整数psented $ P $的一个最大值大的数目
  类型。

41) This implies that unsigned arithmetic does not overflow because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting unsigned integer type.

有关ISO C(99),它是6.2.5 / 9:

For ISO C(99), it is 6.2.5/9:

涉及无符号的操作数可以从不溢出,因为这不能重新由所得无符号整型psented $ P $一个结果是减少的一种计算模比可重新presented的一个最大值大的数目通过将得到的类型。

A computation involving unsigned operands can never overflow, because a result that cannot be represented by the resulting unsigned integer type is reduced modulo the number that is one greater than the largest value that can be represented by the resulting type.

这意味着结果被保证是相同 SIZE_MAX - (的sizeof(结构富))+ 1

Which means the result is guaranteed to be the same as SIZE_MAX - (sizeof(struct foo)) + 1.


在ISO 14882:2003 5.3.1.7:

In ISO 14882:2003 5.3.1.7:

[...]负无符号的
  量中减去计算
  2其值 N ,其中
  的 N 的是位的数
  亲moted操作数。的类型的
  其结果是促进的类型
  操作数。

[...] The negative of an unsigned quantity is computed by subtracting its value from 2n, where n is the number of bits in the pro- moted operand. The type of the result is the type of the promoted operand.

这篇关于应该发生什么一个为size_t(即`-sizeof(结构富)`))的否定?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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