当前位置:网站首页>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.
边栏推荐
- 1374. 生成每种字符都是奇数个的字符串 : 简单构造模拟题
- What is the difference between a utility model patent and an invention patent?Understand in seconds!
- 附录A printf、varargs与stdarg A.3 stdarg.h ANSI版的varargs.h
- WeChat applet cloud development | personal blog applet
- PyTorch笔记 - Attention Is All You Need (2)
- 函数(二)
- 响应式织梦模板美容整形类网站
- 测试的意义并不是能找到全部的缺陷
- Which websites are commonly used for patent searches?
- 微信小程序云开发|个人博客小程序
猜你喜欢

外骨骼机器人(七):标准步态数据库

Imitation cattle forum project

在Cesium中实现与CAD的DWG图叠加显示分析

excel高级绘图技巧100讲(二十二)-如何对不规则数据进行分列

有点奇怪!访问目的网址,主机能容器却不行

【接口测试】JMeter调用JS文件实现RSA加密

Use WeChat official account to send information to designated WeChat users

Fork/Join线程池

C语言之字符串函数二

STAHL touch screen repair all-in-one display screen ET-316-TX-TFT common faults
随机推荐
Common pits in the Go language
通俗解释:什么是临床预测模型
PyTorch笔记 - Attention Is All You Need (2)
C陷阱与缺陷 第7章 可移植性缺陷 7.6 内存位置0
系统收集集
Godaddy domain name resolution is slow and how to use DNSPod resolution to solve it
vant实现Select效果--单选和多选
宝塔搭建PESCMS-Ticket开源客服工单系统源码实测
淘宝获取收货地址列表的 API
R语言进行相关的操作
[Energy Conservation Institute] Application of Intelligent Control Device in High Voltage Switchgear
附录A printf、varargs与stdarg A.2 使用varargs.h来实现可变参数列表
使用百度EasyDL实现厂区工人抽烟行为识别
OSG Notes: Set DO_NOT_COMPUTE_NEAR_FAR to manually calculate far and near planes
封装一个管理 url 状态的 hook
面试突击70:什么是粘包和半包?怎么解决?
【Dart】dart构造函数学习记录(含dart单例模式写法)
仿牛客论坛项目
excel高级绘图技巧100讲(二十二)-如何对不规则数据进行分列
What is the difference between a utility model patent and an invention patent?Understand in seconds!