C89:有符号/无符号不匹配

发布于 2024-08-22 02:07:54 字数 260 浏览 10 评论 0原文

有符号/无符号不匹配一定是不好的吗?

这是我的程序:

int main(int argc, char *argv[]) {
    unsigned int i;

    for (i = 1; i < argc; i++) { // signed/unsigned mismatch here

    }
}

argc 已签名,i 未签名。这是一个问题吗?

Are signed/unsigned mismatches necessarily bad?

Here is my program:

int main(int argc, char *argv[]) {
    unsigned int i;

    for (i = 1; i < argc; i++) { // signed/unsigned mismatch here

    }
}

argc is signed, i is not. Is this a problem?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

忘东忘西忘不掉你 2024-08-29 02:07:54

“有符号/无符号不匹配”可能很糟糕。在你的问题中,你问的是比较。当比较相同基类型的两个值(一个有符号,一个无符号)时,有符号值将转换为无符号值。所以,

int i = -1;
unsigned int j = 10;

if (i < j)
    printf("1\n");
else
    printf("2\n");

打印 2,而不是 1。这是因为在 i i i i i i ji 转换为 unsigned int(unsigned int)-1 等于 UINT_MAX,一个非常大的数字。因此,条件的计算结果为 false,您将进入 else 子句。

对于您的特定示例,argc 保证为非负数,因此您不必担心“不匹配”。

"signed/unsigned mismatches" can be bad. In your question, you are asking about comparisons. When comparing two values of the same base type, but one signed and one unsigned, the signed value is converted to unsigned. So,

int i = -1;
unsigned int j = 10;

if (i < j)
    printf("1\n");
else
    printf("2\n");

prints 2, not 1. This is because in i < j, i is converted to an unsigned int. (unsigned int)-1 is equal to UINT_MAX, a very large number. The condition thus evaluates to false, and you get to the else clause.

For your particular example, argc is guaranteed to be non-negative, so you don't have to worry about the "mismatch".

ゝ偶尔ゞ 2024-08-29 02:07:54

在您的特定情况下,这不是一个真正的问题,但编译器无法知道 argc 始终具有不会导致任何问题的值。

It is not a real problem in your particular case, but the compiler can't know that argc will always have values that will not cause any problems.

倥絔 2024-08-29 02:07:54

还不错。我会修复有关有符号/无符号不匹配的编译器警告,因为即使不太可能或不可能,坏事也可能发生。当您确实由于有符号/无符号不匹配而必须修复错误时,编译器基本上会说“我告诉过您了”。不要忽视警告,它的存在是有原因的。

Its not bad. I'd fix compiler warnings concerning signed/unsigned mismatch because bad things can happen even if they are unlikely or impossible. When you do have to fix a bug because of signed/unsigned mismatch the compiler is basically saying "I told you so". Don't ignore the warning its there for a reason.

初吻给了烟 2024-08-29 02:07:54

这只是间接的问题。

如果您使用有符号整数进行按位运算,例如 &|<<> ,则可能会发生不好的事情。 >.
如果使用无符号整数进行算术,可能会发生完全不同的坏事(下溢、测试数字是否为 >= 0 时的无限循环等)。

因此,某些编译器和静态检查工具会发出问题当您在任一类型的操作(算术或位操作)中混合有符号和无符号整数时,

会出现警告。尽管在像您的示例这样的简单情况下混合它们是安全的,但如果这样做,则意味着您无法使用这些静态检查工具(或必须禁用这些警告),这可能意味着其他错误未被检测到。

有时您别无选择,例如在内存管理代码中对 size_t 类型的值进行算术时。

在您的示例中,我会坚持使用 int,只是因为类型较少会更简单,并且 int 无论如何都会在那里,因为它是main() 的第一个参数。

It is only indirectly a problem.

Bad things can happen if you use signed integers for bitwise operations such as &, |, << and >>.
Completely different bad things can happen if you use unsigned integers for arithmetic (underflow, infinite loops when testing if a number is >= 0 etc.)

Because of this, some compilers and static checking tools will issue warnings when you mix signed and unsigned integers in either type of operation (arithmetic or bit manipulation.)

Although it can be safe to mix them in simple cases like your example, if you do that it means you cannot use those static checking tools (or must disable those warnings) which may mean other bugs go undetected.

Sometimes you have no choice, e.g. when doing arithmetic on values of type size_t in memory management code.

In your example I would stick to int, just because it is simpler to have fewer types, and the int is going to be in there anyway as it is the type of the first argument to main().

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文