在双精度数组上使用 memset(…, 0, …) 是否合法?
将双精度数组的内存归零(使用 memset(..., 0, ...))或包含双精度的结构是否合法?
这个问题意味着两个不同的事情:
从 C 标准的角度来看:这是未定义的行为吗? (我认为,在任何特定平台上,这都不能是未定义的行为,因为它仅取决于浮点数的内存表示形式 - 仅此而已。)
从实际角度来看: 在Intel平台上可以吗? (无论标准怎么说。)
Is it legal to zero the memory of an array of doubles (using memset(…, 0, …)
) or struct containing doubles?
The question implies two different things:
From the point of view of C standard: Is this undefined behavior of not? (On any particular platform, I presume, this cannot be undefined behavior, as it just depends on the in-memory representation of floating-point numbers—that’s all.)
From practical point of view: Is it OK on Intel platform? (Regardless of what the standard is saying.)
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(7)
C99 标准附录 F 说:
并且,紧接着:
因此,由于 IEC 60559 基本上是 IEEE 754-1985,并且由于它指定 8 个零字节表示 0.0(如 @David Heffernan 所说),这意味着 如果您发现
__STDC_IEC_559__
已定义,您可以使用memset
安全地进行 0.0 初始化。The C99 standard Annex F says:
And, immediately after:
Thus, since IEC 60559 is basically IEEE 754-1985, and since this specifies that 8 zero bytes mean 0.0 (as @David Heffernan said), it means that if you find
__STDC_IEC_559__
defined, you can safely do a 0.0 initialization withmemset
.如果您谈论的是 IEEE754,则该标准将双精度的 +0.0 定义为 8 个零字节。如果您知道自己受 IEEE754 浮点支持,那么这是明确定义的。
至于Intel,我想不出在Intel x86/x64上不使用IEEE754的编译器。
If you are talking about IEEE754 then the standard defines +0.0 to double precision as 8 zero bytes. If you know that you are backed by IEEE754 floating point then this is well-defined.
As for Intel, I can't think of a compiler that doesn't use IEEE754 on Intel x86/x64.
David Heffernan 对你问题的第(2)部分给出了很好的答案。对于第 (1) 部分:
C99标准不保证一般情况下浮点值的表示。 §6.2.6.1 说:
...该子条款没有进一步提及浮点。
你说:
确实 - “未定义行为”和“未指定 行为”和“实现定义 行为”:
因此,由于浮点表示是未指定的行为,它可能会以一种未记录的方式因平台而异(其中“平台”在这里意味着“硬件和编译器的组合”而不仅仅是“硬件”) )。
(我不确定如果定义了
__STDC_IEC_559__
,则保证表示double
使得所有位零为+0.0
有多有用正如 Matteo Italia 的回答所述,实际上是在实践中,例如, GCC 从未定义过这一点。 ,尽管在许多硬件平台上使用 IEEE 754 / IEC 60559。)David Heffernan has given a good answer for part (2) of your question. For part (1):
The C99 standard makes no guarantees about the representation of floating-point values in the general case. §6.2.6.1 says:
...and that subclause makes no further mention of floating point.
You said:
Indeed - there a difference between "undefined behaviour", "unspecified behaviour" and "implementation-defined behaviour":
and so, as floating point representation is unspecified behaviour, it can vary in an undocumented manner from platform to platform (where "platform" here means "the combination of hardware and compiler" rather than just "hardware").
(I'm not sure how useful the guarantee that a
double
is represented such that all-bits-zero is+0.0
if__STDC_IEC_559__
is defined, as described in Matteo Italia's answer, actually is in practice. For example, GCC never defines this, even though is uses IEEE 754 / IEC 60559 on many hardware platforms.)即使您不太可能遇到出现问题的机器,但如果您真的像问题标题中所指出的那样谈论数组,并且如果这些数组是在编译时已知长度(即不是 VLA),那么只需初始化它们可能会更方便:
应该始终有效。如果稍后您必须再次将这样的数组归零,并且您的编译器符合现代 C (C99),您可以
在任何现代编译器上使用复合文字来完成此操作,这应该与 memset 一样高效>,但优点是不依赖于
double
的特定编码。Even though it is unlikely that you encounter a machine where this has problems, you may also avoid this relatively easily if you are really talking of arrays as you indicate in the question title, and if these arrays are of known length at compile time (that is not VLA), then just initializing them is probably even more convenient:
should always work. If you'd have to zero such an array again, later, and your compiler is compliant to modern C (C99) you can do this with a compound literal
on any modern compiler this should be as efficient as
memset
, but has the advantage of not relying on a particular encoding ofdouble
.正如 Matteo Italia 所说,根据标准这是合法的,但我不会使用它。类似的东西
至少快两倍。
As Matteo Italia says, that’s legal according to the standard, but I wouldn’t use it. Something like
is at least twice faster.
使用
memset
是“合法的”。问题在于它是否产生一个array[x] == 0.0
为 true 的位模式。虽然基本的 C 标准并不要求这是真的,但我有兴趣听到事实并非如此的例子!看起来通过 memset 设置为零相当于在 IBM-AIX、HP-UX (PARISC)、HP-UX (IA-64)、Linux(我认为是 IA-64)上分配 0.0 。
这是一个简单的测试代码:
It’s “legal” to use
memset
. The issue is whether it produces a bit pattern wherearray[x] == 0.0
is true. While the basic C standard doesn’t require that to be true, I’d be interested in hearing examples where it isn’t!It appears that setting to zero via
memset
is equivalent to assigning 0.0 on IBM-AIX, HP-UX (PARISC), HP-UX (IA-64), Linux (IA-64, I think).Here is a trivial test code:
好吧,我认为归零是“合法的”(毕竟,它是将常规缓冲区归零),但我不知道标准是否允许您对结果逻辑值进行任何假设。我的猜测是 C 标准将其保留为未定义。
Well, I think the zeroing is "legal" (after all, it's zeroing a regular buffer), but I have no idea if the standard lets you assume anything about the resulting logical value. My guess would be that the C standard leaves it as undefined.