C语言中的undefined behavior系列(3) -- trap representation
文章的传送门: C语言中的undefined behavior/unspecified behavior - 序
嗷嗷的话: 本文里只要是大段大段的英文,都来至C99标准。所有的中文注释都是我对标准的理解。在文中使用的编译器是开了C99支持Intel C Compiler 11 for Windows.
— A trap representation isread by an lvalue expression that does not have character type(6.2.6.1).
— A trap representation isproduced by a side effect that modifies any part of the object using an lvalueexpression that does not have character type (6.2.6.1).
什么是traprepresentation?简单来说trap representation就是不合理,没有意义,无效的值。对于这样的值的读写会造成硬件的trap exception.
举个例子,比如说某种硬件使用32bits来表示一个有符号整数,最高位为符号位,0表示正数,1表示负数。并且定义0为0x00000000,那么0x80000000就是一个trap representation。
另外一个例子是,某硬件使用30bits来表示一个整数,额外的2bits作为校验位,这样也存在trap representation。
所以,读一个traprepresentation或者部分写一个trap representation是一个undefined behavior。
[注1]一个automatic variable可以被初始化为一个traprepresentation,比如说被分配在站上的局部变量,其内存中本来有的值可能就是一个trap representation。但这样的初始化并不是一个undefined behavior,除非不对他正确赋值就是用这个值,才是一个trap representation的undefined behavior
[注2] unspecifiedvalues不等同于trap representation. 比如说padding value in structure就是一个unspecifiedvalue但是不是trap representation,比如说,两个structure相互赋值,虽然有padding value,但是这不是trap representation
[注3] value --precise meaning of the contents of an object when interpreted as having aspecific type
implementation-defined value -- unspecified value where eachimplementation documents how the choice is made
indeterminate value -- either an unspecified value or a traprepresentation
unspecified value -- valid value of the relevant type where thisInternational Standard imposes no requirements on which value is chosen in anyinstance. An unspecified value cannot be a trap representation
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 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的设计模式综述