当前位置:网站首页>C traps and defects Chapter 3 semantic "traps" 3.9 integer overflow
C traps and defects Chapter 3 semantic "traps" 3.9 integer overflow
2022-07-29 03:03:00 【weixin_ Guest time】
Integer overflow
C There are two kinds of integer arithmetic operations in language : Signed and unsigned operations . In unsigned arithmetic operations , It doesn't matter “ overflow ” said : All unsigned operations are based on 2 Of n Power module , here n Is the number of digits in the result . If an operand of an arithmetic operator is a signed integer , The other is unsigned integer , Then signed integers will be converted to unsigned integers ,“ overflow ” It can't happen .
however , When both operands are signed integers ,“ overflow ” It could happen , and “ overflow ” The result of is undefined . When the result of an operation occurs “ overflow ” when , It's not safe to make any assumptions .
Assume a and b It's two nonnegative integer variables , We need to check a+b Whether it will “ overflow ”. One way to take it for granted is this :
//a >= 0, b >= 0
if (a + b < 0) {
complain();
}
This does not work properly . When a+b It does happen “ overflow ” when , All assumptions about the outcome are no longer reliable . for example , On some machines , The addition operation will set an internal register to 4 One of the states in : just 、 negative 、 Zero sum overflow . On this kind of machine ,a And b The result of adding is overflow , Not negative , therefore if The check of the statement will fail .
A correct way is to a and b Are cast to unsigned integers :
if ((unsigned)a + (unsigned)b > INT_MAX) {
complain();
}
Here INT_MAX Is a defined constant , Represents the maximum possible integer value .ANSI C Standard in <limits.h> It defines INT_MAX; If it's in another C Language implementation , Readers may need to redefine themselves .
Another possible way to eliminate the need for unsigned arithmetic operations is :
if (a > INT_MAX - b) {
complain();
}
边栏推荐
猜你喜欢

Chapter 2 VRP command line

SOA(面向服务架构)是什么?

融云实时社区解决方案
![[open the door to the new world] see how the old bird of testing plays API testing between applause](/img/79/1bc836cefef24d23e09d9865ff1fba.png)
[open the door to the new world] see how the old bird of testing plays API testing between applause

Redis configuration cache expiration listening event trigger

第2章 VRP命令行

Feedback function of conference OA

mycat读写分离配置

工科男生:20岁未满,平凡但不平庸

The basic concept of the origin and connotation of maker Education
随机推荐
Add a row to a specific location in the dataframe
解读AI机器人养宠引领时尚潮流
Plato Farm在Elephant Swap上铸造的ePLATO是什么?为何具备高溢价?
Multithreading realizes concurrent reading and execution of multi use case files +selenium grid4 realizes distributed deployment of test framework
OSPF experiment
Analysis of concepts and terms in data warehouse
创客教育的起源和内涵的基本理念
The basic concept of the origin and connotation of maker Education
CentOS install mysql8
C陷阱与缺陷 第3章 语义“陷阱” 3.6 边界计算与不对称边界
Interpreting AI robots' pet raising and leading fashion trends
HTB-Blocky
C和指针 第3章 语义“陷阱” 3.5 空指针并非字符串
第09章_性能分析工具的使用
[QNX Hypervisor 2.2用户手册]9.11 ram(更新中)
shell脚本总结
金山云回港上市:中国TO B云厂商的港股奔袭
R language error: compilation failed for package '****‘
Analysis of Project-based Learning Creativity in steam Education
C traps and defects Chapter 2 syntax "traps" 2.6 problems caused by "hanging" else