当前位置:网站首页>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.
边栏推荐
猜你喜欢
随机推荐
4.1 配置Mysql与注册登录模块
在Cesium中实现与CAD的DWG图叠加显示分析
R语言 数据的关系探索
pytest:开始使用
C陷阱与缺陷 附录B Koenig和Moo夫妇访谈
Fork/Join线程池
C专家编程 第1章 C:穿越时空的迷雾 1.4 K&R C
98.嵌入式控制器EC实战 EC开发板开发完成
Where should I prepare for the PMP exam in September?
2022年秋招,软件测试开发最全面试攻略,吃透16个技术栈
MySQL Syntax Basics
Interview assault 70: what is the glue bag and a bag?How to solve?
网络安全与基础设施安全局(CISA):两国将在网络安全方面扩大合作
进行交互或动画时如何选择Visibility, Display, and Opacity
列表页常见的 hook 封装
Convolutional Neural Network (CNN) mnist Digit Recognition - Tensorflow
C陷阱与缺陷 第8章 建议与答案 8.2 答案
JS提升:如何中断Promise的链式调用
Pytorch框架学习记录10——线性层
【Kaggle】Classify Leaves









