gpt4 book ai didi

C语言If语句

转载 作者:太空宇宙 更新时间:2023-11-04 06:30:18 26 4
gpt4 key购买 nike

我是C语言新手,想弄明白下面代码的意思。

在这里 if (!msize) 检查 msize 是否为零或者 msize 是否为 NULL?

if (!msize)
msize = 1 / msize; /* provoke a signal */

//Example 1: A division-by-zero misuse, in lib/mpi/mpi-pow.c of the Linux kernel, where the entire code will be optimized away.
//Compilers, GCC 4.7 and Clang 3.1

最佳答案

这取决于msize的类型。

  • 如果msize是一个指针,它会测试它是否为NULL

  • 如果 msize 不是指针,则测试它是否为 0

这种区别看似迂腐,但却很重要。虽然 NULL 在大多数系统上实际上是 0,但 C 标准允许它是任何其他值。


我做了一些进一步的阅读,因为我开始怀疑我上面的理解是否正确。

这里是 C 标准的相关部分。

§6.5.3.3 Unary arithmetic operators

(5) The result of the logical negation operator ! is 0 if the value of its operand compares unequal to 0, 1 if the value of its operand compares equal to 0. The result has type int. The expression !E is equivalent to (0==E).

§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. 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.

(6) Any pointer type may be converted to an integer type. Except as previously specified, the result is implementation-defined. If the result cannot be represented in the integer type, the behavior is undefined. The result need not be in the range of values of any integer type.

Footnotes: 66 The macro NULL is defined in <stddef.h> (and other headers) as a null pointer constant; see 7.19.

如您所见,0 在 C 语言中是一种魔数(Magic Number)。对于具有非零 NULL 的系统,我希望 !msize 的实际行为可能是实现定义的。无论如何,这有点挑剔。

我在论文中找到了您示例的来源:Undefined Behavior: What Happened to My Code? .讨论您的示例的文本指出:

As mentioned earlier, at the instruction set level, x86 raises an exception for a division by zero [17, 3.2], while MIPS [22, A.6] and PowerPC [15, 3.3.38] silently ignore it. A division by zero in C is undefined behavior [19, 6.5.5], and a compiler can thus simply assume that the divisor is always non-zero.

Figure 1 shows a division-by-zero misuse in the Linux kernel. From the programmer’s comment it is clear that the intention is to signal an error in case msize is zero. When compiling with GCC, this code behaves as intended on an x86, but not on a PowerPC, because it will not generate an exception. When compiling with Clang, the result is even more surprising. Clang assumes that the divisor msize must be non-zero—on any system—since otherwise the division is undefined. Combined with this assumption, the zero check !msize becomes always false, since msize cannot be both zero and non-zero. The compiler determines that the whole block of code is unreachable and removes it, which has the unexpected effect of removing the programmer’s original intention of guarding against the case when msize is zero.

所以在您的情况下,您真正​​需要的答案是肯定的:它测试 msize 是否为 0。

关于C语言If语句,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21516703/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com