未定义/未指定/实现定义的行为的警告? [英] Undefined/Unspecified/Implementation-defined behaviour warnings?

查看:272
本文介绍了未定义/未指定/实现定义的行为的警告?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

不能在编译器警告(即使它抛出错误更好),当它注意到未定义/未指定/实现定义的声明?

Can't a compiler warn (even better if it throws errors) when it notices a statement with undefined/unspecified/implementation-defined behaviour?

大概标志作为错误的陈述,标准应该这么说,但至少可以提醒$ C $铬。是否有实施这种选择任何技术上的困难?还是只是不可能的?

Probably to flag a statement as error, the standard should say so, but it can warn the coder at least. Is there any technical difficulties in implementing such an option? Or is it merely impossible?

原因我得到了这个问题,在语句中如 A [i] = ++我; 将不是被知道了code试图引用变量,并在同一个语句修改它,是达到一个序列点之前。

Reason I got this question is, in statements like a[i] = ++i; won't it be knowing that the code is trying to reference a variable and modifying it in the same statement, before a sequence point is reached.

推荐答案

这一切都归结到


  • 实施质量的:更准确和有用的警告是,它是更好的。始终打印编译器:这一计划可能会或可能不会援引未定义行为每一个程序,然后编译它,是pretty无益,反而是符合标准的。值得庆幸的是,没有一个人写编译器,如这些: - )

  • Quality of Implementation: the more accurate and useful the warnings are, the better it is. A compiler that always printed: "This program may or may not invoke undefined behavior" for every program, and then compiled it, is pretty useless, but is standards-compliant. Thankfully, no one writes compilers such as these :-).

确定易于的:编译器可能不容易能够确定不确定的行为,未指定的行为,或实现定义的行为。比方说,你有一个调用堆栈是5层深,以为const char * 参数正在从顶层传递,在链中的最后一个函数,最后函数调用的printf()为const char * 作为第一个参数。你想编译器检查为const char * ,以确保它是正确的? (假设第一个函数使用了该值的文本字符串。)如何当为const char * 从文件中读取,但你知道该文件将始终包含正在打印的值有效的格式说明?

Ease of determination: a compiler may not be easily able to determine undefined behavior, unspecified behavior, or implementation-defined behavior. Let's say you have a call stack that's 5 levels deep, with a const char * argument being passed from the top-level, to the last function in the chain, and the last function calls printf() with that const char * as the first argument. Do you want the compiler to check that const char * to make sure it is correct? (Assuming that the first function uses a literal string for that value.) How about when the const char * is read from a file, but you know that the file will always contain valid format specifier for the values being printed?

成功率的:一个编译器能够检测到许多结构可能会或可能不会是不确定的,不确定的,等等;但具有非常低的成功率。在这种情况下,用户不希望看到很多可能是不确定的消息&MDASH的;太多的虚假警告信息可能掩盖真正的警告信息,或提示用户在低警报设置进行编译。这是坏的。

Success rate: A compiler may be able to detect many constructs that may or may not be undefined, unspecified, etc.; but with a very low "success rate". In that case, the user doesn't want to see a lot of "may be undefined" messages—too many spurious warning messages may hide real warning messages, or prompt a user to compile at "low-warning" setting. That is bad.

有关特定例如, GCC 给出了一个关于可以是不明确的警告。它甚至警告说,为的printf()格式不匹配。

For your particular example, gcc gives a warning about "may be undefined". It even warns for printf() format mismatch.

但如果你希望是一个编译器会发出诊断为所有未定义/未指定的情况下,目前尚不清楚是否应该/可以正常工作。

But if your hope is for a compiler that issues a diagnostic for all undefined/unspecified cases, it is not clear if that should/can work.

假设你有以下内容:

#include <stdio.h>
void add_to(int *a, int *b)
{
    *a = ++*b;
}

int main(void)
{
    int i = 42;
    add_to(&i, &i); /* bad */
    printf("%d\n", i);
    return 0;
}

如果编译器警告你 * A = ++ * B;

由于 GF 在评论中说,一个编译器不能跨翻译单元检查未定义的行为。典型的例子是变量声明在一个文件中的指针,其定义为在另一个阵列,见 comp.lang.c FAQ 6.1

As gf says in the comments, a compiler cannot check across translation units for undefined behavior. Classic example is declaring a variable as a pointer in one file, and defining it as an array in another, see comp.lang.c FAQ 6.1.

这篇关于未定义/未指定/实现定义的行为的警告?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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