什么是 C 中的未定义行为? [英] What is Undefined Behaviour in C?

查看:44
本文介绍了什么是 C 中的未定义行为?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

什么是 C 中的未定义行为?

What is undefined behaviour in C?

我正在使用 GCC 编译器.在某些情况下,虽然程序的输出应该是未定义的,但我得到了正确的值.我多次运行这些程序.但结果是一致的.对于其他一些程序,结果是不确定的.那么,在哪些情况下我应该考虑程序行为真的是未定义的?这有什么规则吗?

I am using GCC compiler. In some cases I get correct value though the program's output was supposed to be undefined. I ran those programs several times. But the result was consistent. And for some other programs the result was undefined. So, in which cases I should consider that the program behaviour is really undefined? Is there any kind of RULES for this?

推荐答案

未定义行为的定义:

1 个未定义的行为

行为,在使用不可移植或错误的程序结构或错误数据时,本国际标准对此没有要求

behavior, upon use of a non portable or erroneous program construct or of erroneous data, for which this International Standard imposes no requirements

2 注意 可能的未定义行为范围从完全忽略情况并产生不可预测的结果,在翻译或程序执行期间以环境特征的文件化方式(有或没有发布诊断消息),终止翻译或执行(发出诊断消息).

2 NOTE Possible undefined behavior ranges from ignoring the situation completely with unpredictable results, to behaving during translation or program execution in a documented manner characteristic of the environment (with or without the issuance of a diagnostic message), to terminating a translation or execution (with the issuance of a diagnostic message).

3 示例 未定义行为的一个例子是整数溢出的行为.

3 EXAMPLE An example of undefined behavior is the behavior on integer overflow.

C11 §J.2 中还有一个未定义行为列表未定义行为

There is also a list of undefined behaviors in C11 §J.2 Undefined behavior

这篇关于什么是 C 中的未定义行为?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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