Last active
May 4, 2018 08:32
-
-
Save yuantailing/ed39b9f0189f4c71f89982dc312db65b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<!DOCTYPE html> | |
<html> | |
<head> | |
<title>C++ Debuggerの日常</title> | |
<meta charset="utf-8"> | |
<style> | |
@import url(https://fonts.googleapis.com/css?family=Droid+Serif:400,700,400italic); | |
@import url(https://fonts.googleapis.com/css?family=Consolas:400,700,400italic); | |
body { font-family: 'Droid Serif'; } | |
.remark-code, .remark-inline-code { font-family: 'Consolas'; } | |
.font-small { font-size: 70%; } | |
</style> | |
</head> | |
<body> | |
<textarea id="source"> | |
class: center, middle | |
# C++ Debuggerの日常 | |
.right[三行·氰书] | |
.right[献给陪伴我们整个青春的 C++ BUG 们] | |
| |
.right.font-small[修改自:知乎用户 w2014 https://www.zhihu.com/question/267697888/answer/331237351] | |
--- | |
# 被忘却的回忆 | |
- 那些总是被遗忘的点点滴滴 | |
--- | |
# | |
- 少写等号 | |
```c++ | |
bool isequal(int x, int y) { | |
if (x = y) | |
return true; | |
else | |
return false; | |
} | |
/* | |
* 纵然命运注定缠身 | |
* 从此以往如路人 | |
* 此情,此意,恒真 | |
*/ | |
``` | |
--- | |
# | |
- 少写大括号 | |
```c++ | |
if (a > b) | |
c = a; | |
a = b; | |
b = a; | |
/* | |
* 这是一个悲伤的故事 | |
* 他的心理满是她的影子 | |
* 她的眸中却没有映出他的情意 | |
*/ | |
``` | |
--- | |
# | |
- case 少写 break | |
```c++ | |
switch (x) { | |
case 1: printf("One"); | |
case 2: printf("Two"); | |
case 3: printf("Three"); | |
} | |
/* | |
* 既然命运使我们在此相遇 | |
* 我便要陪你走过一路荆棘 | |
* 不离,不弃。 | |
*/ | |
``` | |
--- | |
# 逃不出的往事 | |
- 循环的 100 种死法 | |
--- | |
# | |
- 写错循环变量 | |
```c++ | |
for (j = 1; j < 10; i++) { | |
printf("%d ", a[j]); | |
} | |
/* | |
* 一次又一次地徘徊 | |
* 跳不出命运的安排 | |
* 终究还是不能将你忘怀 | |
*/ | |
``` | |
--- | |
# | |
- for 循环中对循环变量赋值 | |
```c++ | |
for (i = 0; i < 5; i++) { | |
printf("%d ", a[i--]; | |
} | |
/* | |
* 多希望一切能重演 | |
* 让我能回到一切的起点 | |
* 留在遇到你的那一天 | |
*/ | |
``` | |
--- | |
# | |
- while 条件无法满足(精度问题?) | |
```c++ | |
while (sqrt(i - 1) * sqrt(i + 1) != 2 * sqrt(6)) { | |
i++; | |
} | |
/* | |
* 我一直都在等你着你回来 | |
* 结果与你擦肩而过时我才明白 | |
* 我的悲哀 | |
*/ | |
``` | |
--- | |
# | |
- 递归没有退出条件 | |
```c++ | |
int fibonacci(int x) { | |
return fibonacci(x - 1) + fibonacci(x - 2); | |
} | |
/* | |
* 昨天的牵挂 | |
* 今天的思念 | |
* 无止无尽,不休不眠,是溢出的爱恋 | |
*/ | |
``` | |
--- | |
# | |
- 多次初始化 | |
```c++ | |
while (s < 100) { | |
s = 0; | |
i++; | |
s += a[i]; | |
} | |
/* | |
* 如果有一天 | |
* 你忘记了我们的从前 | |
* 我会一遍遍讲给你听,直到永远 | |
*/ | |
``` | |
--- | |
# 终究还是柴米油盐 | |
- 琐琐碎碎,零零散散,酸甜苦辣,嬉笑怒骂,才是生活应有的样子 | |
--- | |
# | |
- 超出数据类型范围 | |
```c++ | |
int i; | |
i = 2147483648; | |
/* | |
* 他的心很小 | |
* 记不住她的光环和荣耀 | |
* 只放得下,她的一个拥抱 | |
*/ | |
``` | |
--- | |
# | |
- 变量使用前没有初始化 | |
```c++ | |
int s, i; | |
for (i = 1; i < 100; i++) | |
s += i; | |
/* | |
* 不能忘记的曾经 | |
* 在现在仍留着残影 | |
* 让我们的未来难以确定 | |
*/ | |
``` | |
--- | |
# | |
- 交换变量的错误示范 | |
```c++ | |
a = b; | |
b = a; | |
/* | |
* 他把一切给了她 | |
* 她把一切还给了他 | |
* 却忘记了自己,只记得他 | |
*/ | |
``` | |
--- | |
# | |
- 移动数组中变量的错误示范 | |
```c++ | |
for (i = 0; i < n; i++) | |
a[i + 1] = a[i]; | |
/* | |
* 每一天,都是一样的牵挂 | |
* 每一刻,都是同样的思念 | |
* 日复一日,直到永远 | |
*/ | |
``` | |
--- | |
# | |
- 关于 define | |
```c++ | |
#include <stdio.h> | |
#define x 2 + 258 | |
int main() { | |
printf("%d\n", x * 2); | |
return 0; | |
} | |
/* | |
* 真的真的写不出文字来表达我的情意 | |
* 可是我真的真的好像告诉你 | |
* 我,爱你 | |
*/ | |
``` | |
--- | |
name: solution | |
# 怕是要正经起来了 | |
说了好多问题 刚发现少了解决方案......... | |
--- | |
template: solution | |
不确定计算顺序时永远不要吝啬括号 | |
Debug 时多输出中间变量 | |
适当利用步进调试 | |
变量记得赋初始值 | |
不要用浮点数判等 | |
对于循环,特别要留意退出前的最后几步,能否让循环正常退出 | |
减少递归。必须递归时要确定出口条件无误 | |
减少使用自己不确定的写法 | |
--- | |
# The End | |
感谢这些年来我们写出的种种 bug | |
一路陪我们走来 | |
使我们单调乏味的生活多了几分趣味 | |
又始终提醒着我们自己的弱点 | |
激励着我们不断进步 | |
| |
| |
.right[via 从没给别人写过三行情书的某人] | |
</textarea> | |
<script src="https://remarkjs.com/downloads/remark-latest.min.js"> | |
</script> | |
<script> | |
var slideshow = remark.create(); | |
</script> | |
</body> | |
</html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment