当前位置:网站首页>Suggestions and answer 8.1 C traps and defect chapter 8
Suggestions and answer 8.1 C traps and defect chapter 8
2022-08-01 21:03:00 【weixin_Guest time】
C language is a powerful and flexible tool.
How can we avoid these problems in C language?
The most important avoidance technique is knowing what you are doing.
Most of the mistakes are in the final opinion work the producer does before going to bed.So, before you prepare
to do something at the end, you'd better rest early.
Thinking about how to combine programs before actually combining them is crucial to getting a reliable result.
Advice:
Don't convince yourself of "The Emperor's New Clothes".
while (c == '\t' || c = ' ' || c == '\n')
c = getc(f);
This is illegal in C.
is equivalent to the following code:
while ((c == '\t' || c) = (' ' || c == '\n'))
c = getc(f);
(c == '\t' || c) cannot appear on the left side of an assignment operator.
while (c == '\t' || c == ' ' || c == '\n')
c = getc(f);
直接了当的表name intent.
Please use parentheses or other means to make your intent as clear as possible.
Sometimes we should also anticipate that those errors may appear, and prevent them in the way of writing the code.
while ('\t' == c || ' ' == c || '\n' == c)
c = getc(f);
考察最简单的特例.
Suitable for designing programs, conceiving how programs work, and testing how programs work.
使用不对称边界。
Watch out for bugs lurking in the dark.
Stick to only the well-known parts of the C language.
防御性编程
对程序用户和编译器事先的假设不要过多。
No matter how impossible it is, it can still happen at some point.To achieve a robust program, you should anticipate this exception.
In order for a C compiler to strictly detect various errors in a program, it must strictly detect the parts of the program that are intended to be portable, and at the same time leave out those parts of the program that are used to complete the specific machine-related parts..
Another reason, certain types of errors are inherently difficult to detect.
void set(int *p, int n) {
*p = n;
}
Context is required to say whether a program is legal or not.
//2.
//legal
int a[10];
set(a + 5, 37);
//illegal
int a[10];
set(a + 10, 37);
The ANSI C standard allows a program to obtain the address of the first position out of bounds at the end of an array.
边栏推荐
- 淘宝获取收货地址列表的 API
- C陷阱与缺陷 第8章 建议与答案 8.1 建议
- Based on FPGA in any number of bytes (single-byte or multibyte) serial port (UART) to send (including source engineering)
- 【中文树库标记---CTB】
- 网络安全与基础设施安全局(CISA):两国将在网络安全方面扩大合作
- [译] 容器和 Kubernetes 中的退出码完整指南
- JSD-2204-Knife4j框架-处理响应结果-Day07
- Godaddy域名解析速度慢问题以及如何使用DNSPod解析解决
- AQS原理和介绍
- Pytorch框架学习记录12——完整的模型训练套路
猜你喜欢
Hiking, cured my mental internal friction
Questions I don't know in database kernel interview(1)
Fork/Join线程池
Excel advanced drawing techniques, 100 (22) - how to respectively the irregular data
扣减库存方案
Imitation cattle forum project
网红驼背矫正产品真的管用吗?如何预防驼背?医生说要这样做
C专家编程 序
乐观锁批量跟新 纯SQL
Interview Blitz 70: What are sticky packs and half packs?How to deal with it?
随机推荐
那些关于DOM的常见Hook封装(一)
【Dart】dart之mixin探究
在Cesium中实现与CAD的DWG图叠加显示分析
仿牛客论坛项目
Pytorch框架学习记录8——最大池化的使用
SkiaSharp 之 WPF 自绘 五环弹动球(案例版)
Goroutine Leaks - The Forgotten Sender
Use WeChat official account to send information to designated WeChat users
Digital twin Beijing the imperial palace, yuan universe is the process of tourism
【微信小程序】【AR】threejs-miniprogram 安装(76/100)
职场如象棋,测试/开发程序员如何突破成长瓶颈期?
密码学的基础:X.690和对应的BER CER DER编码
案例:MySQL主从复制与读写分离
宝塔搭建PESCMS-Ticket开源客服工单系统源码实测
Postman 批量测试接口详细教程
Go Atomic
Pytorch框架学校记录11——搭建小实战完整细节
Where should I prepare for the PMP exam in September?
网络安全与基础设施安全局(CISA):两国将在网络安全方面扩大合作
OSG笔记:设置DO_NOT_COMPUTE_NEAR_FAR,手动计算远近平面