C语言中的undefined behavior/unspecified behavior - 序

嗷嗷的话:

  这都是一些细枝末节的东西,我想不做编译器的话,大部分都很难碰到。研究学习这些只是出于对C语言一种偏执狂。

     写出来是为了找到和我一样的偏执狂。

     在随后的的文章中,首先我写一写191种undefined behavior。不一定是对的,只是我自己的理解。参考的标准是C99.


undefined behavior简单来说就是,如果你的程序违反了C标准中某些准则,那么具体会发生什么,C标准没有定义,也就说得到任何奇怪的结果,都是有可能的。比如说整数溢出就是一个undefined behavior。
unspecified behavior简单来说就是,C标准提供了多种可选方案,具体选择哪一个并没有定义。比如说,函数计算他参数的顺序是一个unspecified behavior。以任何顺序计算都可以。i = i++ + i++;的值也是一个unspecified behavior。

 

本系列其他文章传送门:

C语言中的undefined behavior系列(1)-- linkage of identifier

 

 

一下摘于C99标准。关于behavior的定义

behavior -- external appearance or action

 

implementation-defined behavior -- unspecified behavior where each implementation documents how the choice is made  
  EXAMPLE An example of implementation-defined behavior is the propagation of the high-order bitwhen a signed integer is shifted right.


locale-specific behavior -- behavior that depends on local conventions of nationality, culture, and language that each implementation documents  

   EXAMPLE An example of locale-specific behavior is whether the islower function returns true for characters other than the 26 lowercase Latin letters


undefined behavior -- behavior, upon use of a nonportable or erroneous program construct or of erroneous data,for which this International Standard imposes no requirements 

   NOTE Possible undefined behavior ranges from ignoring the situation completely with unpredictableresults, 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).  

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


unspecified behavior  behavior where this International Standard provides two or more possibilities and imposes no further requirements on which is chosen in any instance 

     EXAMPLE An example of unspecified behavior is the order in which the arguments to a function are evaluated.

posted @   嗷嗷  阅读(4710)  评论(4编辑  收藏  举报
编辑推荐:
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
阅读排行:
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· .NET周刊【3月第1期 2025-03-02】
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· [AI/GPT/综述] AI Agent的设计模式综述
点击右上角即可分享
微信分享提示