是否标准定义空指针常量有设置为零的所有位? [英] Does Standard define null pointer constant to have all bits set to zero?

查看:105
本文介绍了是否标准定义空指针常量有设置为零的所有位?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

(我引用ISO / IEC 9899:201X)

在这里,我们看到的是,整型常量前pression有一个整数类型:


  

6.6恒前pressions


  
  

6。
  的整型常量前pression应有整型的,并应只有操作数
  是整型常量,枚举常量,字符常量的sizeof
  前pressions,其结果是整型常量,_Alignof前pressions和浮动
  是强制转换的立即数常量。在一个整型常量类型转换操作符
  前pression只能转换算术类型为整数类型,除了一个组成部分
  操作数sizeof和_Alignof运营商。


那么这对于任何整数类型也是如此:


  

6.2.6.2整数类型


  
  

5。
  任何填充位的值是unspecified.A有效(未阱)对象重新presentation
  有符号整数类型,其中的符号位为零的是重新的presentation有效的对象
  对应的无符号类型,并须重新present相同的值。的对于任何整数类型,
  重新presentation的对象,其中所有位都为零应的值的重新presentation
  在该类型为零。


然后,我们看到,一个空指针常量是使用整型常量前pression值为0定义。


  

6.3.2.3指针


  
  

3。
  的值为0,或者这样的前pression投整型常量前pression键入
  无效*,被称为空指针常数。
如果空指针常数被转换为一个
  指针类型,所得到的指针,称为空指针,​​被保证比较不等
  的指针的任何对象或功能。


因此​​,空指针常量必须拥有所有它的位置零。

但也有在线和StackOverflow上许多答案是说,这是不正确的。

我很难相信他们给出的报价部分。

(请回答使用参照最新的标准)


解决方案

没有,NULL并不一定是所有位为零。

N1570 6.3.2.3指针第3段:


  

值为0,或者这样的前pression投整型常量前pression键入
  void *的,被称为空指针常量。 66)如果一个空指针的常数被转换为一个
  指针类型
后,所产生的指针,叫做空指针,是保证不平等的比较
  的指针的任何对象或功能。


请参阅我的重点以上:整数 0 转换的如果需要的话,它没有必须有相同的位presentation <。 / p>

66。注意在页面底部说:


  

66)宏NULL被定义(和其他标题)作为空指针常数;见7.19。


我们哪会导致该章的一段话:


  

的宏


  
  

NULL


  
  

它扩展成一个实现定义空指针常量


和更有甚者,在附件J.3.12(可移植性问题,实现定义的行为,库函数)说:


  

- 到的宏NULL扩展的空指针常量(7.19)


( I'm quoting ISO/IEC 9899:201x )

Here we see that, integer constant expression has an integer type:

6.6 Constant expressions

6. An integer constant expression shall have integer type and shall only have operands that are integer constants, enumeration constants, character constants, sizeof expressions whose results are integer constants, _Alignof expressions, and floating constants that are the immediate operands of casts. Cast operators in an integer constant expression shall only convert arithmetic types to integer types, except as part of an operand to the sizeof or _Alignof operator.

Then this holds true for any integer type:

6.2.6.2 Integer types

5. The values of any padding bits are unspecified.A valid (non-trap) object representation of a signed integer type where the sign bit is zero is a valid object representation of the corresponding unsigned type, and shall represent the same value. For any integer type, the object representation where all the bits are zero shall be a representation of the value zero in that type.

Then we see that a null pointer constant is defined using an integer constant expression with the value 0.

6.3.2.3 Pointers

3. An integer constant expression with the value 0, or such an expression cast to type void*, is called a null pointer constant. If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.

Therefore the null pointer constant must have all it's bits set to zero.

But there are many answers online and on StackOverflow that say that that isn't true.

I have a hard time believing them given the quoted parts.

( Please answer using references to the latest Standard )

解决方案

No, NULL doesn't have to be all bits zero.

N1570 6.3.2.3 Pointers paragraph 3:

An integer constant expression with the value 0, or such an expression cast to type void *, is called a null pointer constant. 66) If a null pointer constant is converted to a pointer type, the resulting pointer, called a null pointer, is guaranteed to compare unequal to a pointer to any object or function.

See my emphasis above: Integer 0 is converted if necessary, it doesn't have to have same bit presentation.

Note 66 on bottom of the page says:

66) The macro NULL is defined in (and other headers) as a null pointer constant; see 7.19.

Which leads us to a paragraph of that chapter:

The macros are

NULL

which expands to an implementation-defined null pointer constant

And what is more, on Annex J.3.12 (Portability issues, Implementation-defined behaviour, Library functions) says:

— The null pointer constant to which the macro NULL expands (7.19).

这篇关于是否标准定义空指针常量有设置为零的所有位?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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