memset bool到0是否安全? [英] Is it safe to memset bool to 0?

查看:189
本文介绍了memset bool到0是否安全?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

假设我有一些旧版代码,除非发现错误,且该代码包含以下代码,因此无法更改:

Suppose I have some legacy code which cannot be changed unless a bug is discovered, and it contains this code:

bool data[32];
memset(data, 0, sizeof(data));

这是一个安全的方法来设置 bool

Is this a safe way to set all bool in the array to a false value?

更多一般来说, memset a bool 0 ,以使其值 false

More generally, is it safe to memset a bool to 0 in order to make its value false?

它是否可以在所有编译器上正常工作?

Is it guaranteed to work on all compilers? Or do I to request a fix?

推荐答案

我相信这个未指定,虽然它似乎可能是 false 将全部为零。 Boost.Container依靠此well emphasis mine ):

I believe this unspecified although it seems likely the underlying representation of false would be all zeros. Boost.Container relies on this as well (emphasis mine):


Boost.Container使用std :: memset值来初始化一些
类型,因为在大多数平台中,初始化产生期望的
值初始化,并且性能提高。

Boost.Container uses std::memset with a zero value to initialize some types as in most platforms this initialization yields to the desired value initialization with improved performance.

遵循C11标准,Boost .Container假定对于任何
整数类型,所有位为零的对象表示
将是该类型中的零值的表示。 由于
_Bool / wchar_t / char16_t / char32_t也是C中的整数类型,它认为所有C ++整数类型都可以通过std :: memset初始化。

Following the C11 standard, Boost.Container assumes that for any integer type, the object representation where all the bits are zero shall be a representation of the value zero in that type. Since _Bool/wchar_t/char16_t/char32_t are also integer types in C, it considers all C++ integral types as initializable via std::memset.

他们指出的C11引用实际上来自C99缺陷:缺陷263:全零位表示,添加了以下内容:

This C11 quote they they point to as a rationale actually comes from a C99 defect: defect 263: all-zero bits representations which added the following:


对于任何整数类型,其中所有位都是
zero的对象表示应该是该类型中零值的表示。

For any integer type, the object representation where all the bits are zero shall be a representation of the value zero in that type.

那么这里的问题是假设正确,是C和C ++之间的整数兼容的底层对象表示吗?
提案解决C之间的区别和C ++关于整数的对象表示试图回答这在一定程度上,据我可以告诉没有解决。我在标准草案中找不到这个的确凿证据。我们有几种情况下,它明确链接到C标准类型。

So then the question here is the assumption correct, are the underlying object representation for integer compatible between C and C++? The proposal Resolving the difference between C and C++ with regards to object representation of integers sought to answer this to some extent which as far as I can tell was not resolved. I can not find conclusive evidence of this in the draft standard. We have a couple of cases where it links to the C standard explicitly with respect to types. Section 3.9.1 [basic.fundamental] says:


[...]签名和无符号整数类型应满足C标准第5.2.4.2.1节中给出的
约束。

[...] The signed and unsigned integer types shall satisfy the constraints given in the C standard, section 5.2.4.2.1.

3.9 [basic.types]


类型T的对象的对象表示是由类型T的对象占用的N
个unsigned char对象的序列,其中N等于
sizeof(T )。对象的值表示是保存类型T的值的一组位
。对于普通可复制类型,值
表示是对象表示中的一组位,
确定一个值,它是
实现定义的一组值的一个离散元素。 44

其中脚注44(不是规范)说:


目的是记忆模型C ++与
ISO / IEC 9899编程语言C的兼容性。

The intent is that the memory model of C++ is compatible with that of ISO/IEC 9899 Programming Language C.

草案标准最远的 的底层代表位于部分中 $

The farthest the draft standard gets to specifying the underlying representation of bool is in section 3.9.1:


类型bool,char,char16_t,char32_t,wchar_t和signed和
无符号整数类型统称为积分类型。A整数类型的
同义词是整数类型。
整数类型的表示应通过使用纯二进制数字
system.51定义值[例如:本国际标准允许2的
补码,1的补码和符号幅度表示为
积分类型。 -end example]

Types bool, char, char16_t, char32_t, wchar_t, and the signed and unsigned integer types are collectively called integral types.50 A synonym for integral type is integer type. The representations of integral types shall define values by use of a pure binary numeration system.51 [ Example: this International Standard permits 2’s complement, 1’s complement and signed magnitude representations for integral types. —end example ]

该部分还说:


bool类型的值为true或false。

Values of type bool are either true or false.

但我们知道 true false 是:


布尔文字关键字false和true。这样的文字
是prvalues并且类型为bool。

The Boolean literals are the keywords false and true. Such literals are prvalues and have type bool.

,我们知道它们可以转换为 0 1

and we know they are convertible to 0 an 1:


可以转换为int类型的prvalue,
false变为零并成为true。

A prvalue of type bool can be converted to a prvalue of type int, with false becoming zero and true becoming one.

不接近底层表示。

据我所知,标准引用除了padding位之外的实际底层位的唯一地方是通过缺陷报告1796:空字符的全位零是有意义的要求吗?

As far as I can tell the only place where the standard references the actual underlying bit value besides padding bits was removed via defect report 1796: Is all-bits-zero for null characters a meaningful requirement? :


不清楚可移植程序可以检查表示的位;相反,似乎仅限于检查与值表示相对应的数字的位(3.9.1 [basic.fundamental] paragraph 1)。可能更适合的是要求空字符值比较等于0或'\0',而不是指定表示的位模式。

It is not clear that a portable program can examine the bits of the representation; instead, it would appear to be limited to examining the bits of the numbers corresponding to the value representation (3.9.1 [basic.fundamental] paragraph 1). It might be more appropriate to require that the null character value compare equal to 0 or '\0' rather than specifying the bit pattern of the representation.

还有更多缺陷报告处理标准中的差距,关于什么是有点和值和对象表示之间的差异。

There are more defect reports that deal with the gaps in the standard with respect to what is a bit and difference between the value and object representation.

实际上,我期望这个工作,我不会考虑它的安全,因为我们不能把这个标准。你需要改变它,不清楚,你显然有一个不平凡的权衡涉及。所以假设它现在工作的问题是,我们认为它可能会打破与各种编译器的未来版本,这是未知的。

Practically, I would expect this to work, I would not consider it safe since we can not nail this down in the standard. Do you need to change it, not clear, you clearly have a non-trivial trade-off involved. So assuming it works now the question is do we consider it likely to break with future versions of various compilers, that is unknown.

这篇关于memset bool到0是否安全?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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