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

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

问题描述

编译器在发现未定义/未指定/实现定义的行为的语句时,不能警告(如果它抛出错误,则更好)?

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

一个语句作为错误,标准应该这么说,但它可以至少警告编码器。在实施这种选择方面是否有任何技术困难?

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] = ++ i; 不会知道代码试图引用一个变量,并在到达一个序列点之前在同一语句中修改它。

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.

推荐答案

这一切都归结于


  • 执行质量:更准确和有用的警告是,越好。一个编译器总是打印:这个程序可能或可能不会调用未定义的行为为每个程序,然后编译它,是相当无用,但符合标准。幸运的是,没有人写这样的编译器: - )。

  • 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?

成功率:编译器可能能够检测到许多结构,未定义,未指定等;但是具有非常低的成功率。在这种情况下,用户不想看到大量的可能未定义消息,太多的虚假警告消息可能会隐藏真实的警告消息,或提示用户在低警告设置下编译。

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常见问题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天全站免登陆