CS61C学习记录
# CS61C学习记录
我学的是 Fall20 课程
# 学习资源
# Lab0 (2025.4.9)
这个 Lab 好像说了一些废话,要求我们需要掌握 Git,和 linux 的一些基础指令
# Lab1 (2025.4.10)
# Exercise 1: See what you can C
这个有些 C 语言基础的都很容易做出来
#include <stdio.h>
/* Only change any of these 4 values */
#define V0 3
#define V1 0
#define V2 1
#define V3 3
int main(void) {
int a;
char *s;
/* This is a print statement. Notice the little 'f' at the end!
* It might be worthwhile to look up how printf works for your future
* debugging needs... */
printf("Berkeley eccentrics:\n====================\n");
/* for loop */
for (a = 0; a < V0; a++)
{
printf("Happy ");
}
printf("\n");
/* switch statement */
switch (V1)
{
case 0:
printf("Yoshua\n");
case 1:
printf("Triangle Man\n");
break;
case 2:
printf("Chinese Erhu Guy\n");
case 3:
printf("Yoshua\n");
break;
case 4:
printf("Dr. Jokemon\n");
break;
case 5:
printf("Hat Lady\n");
default:
printf("I don't know these people!\n");
}
/* ternary operator */
s = (V3 == 3) ? "Go" : "Boo";
/* if statement */
if (V2) {
printf("%s BEARS!\n", s);
} else {
printf("%s CARDINAL!\n", s);
}
return 0;
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
# Exercise 2: Catch those bugs!
这个练习主要是要求我们使用 gdb 工具,由于我使用的是 Mac 电脑,所以用 lldb 来代替
While you’re in a gdb session, how do you set the arguments that will be passed to the program when it’s run? 当你在 gdb 会话中时,如何设置程序运行时传递给程序的参数?
g++ [文件名] -o [编译后的程序可执行文件名] -g
1How do you create a breakpoint? 如何创建断点?
(lldb) break set -f [文件名] -l [行号]
1How do you execute the next line of C code in the program after stopping at a breakpoint? 如何在程序停止在断点后执行下一行 C 代码?
next
1If the next line of code is a function call, you’ll execute the whole function call at once if you use your answer to #3. (If not, consider a different command for #3!) How do you tell GDB that you want to debug the code inside the function (i.e. step into the function) instead? (If you changed your answer to #3, then that answer is most likely now applicable here.) 如果下一行代码是一个函数调用,你将使用第 3 题的答案一次性执行整个函数调用。(如果不是,那么第 3 题的答案可能现在不适用了!)你如何告诉 GDB 你想要调试函数内部的代码(即进入函数)?(如果你改变了第 3 题的答案,那么那个答案现在很可能适用于这里。)
step
1How do you continue the program after stopping at a breakpoint? 如何在程序停止在断点后继续执行?
continue
1How can you print the value of a variable (or even an expression like 1+2) in gdb? 你如何在 gdb 中打印变量的值(甚至是一个像 1+2 这样的表达式)?
expr [表达式]
1How do you configure gdb so it displays the value of a variable after every step? 你如何配置 gdb,使其在每一步后显示变量的值?
这个题中 lldb 和 gdb 好像不是很一样
(lldb)target stop-hook add -o "expr [变量名]"
1表示每次停下断点都需要输出变量的值
How do you show a list of all variables and their values in the current function? 如何显示当前函数中所有变量及其值?
frame variable
1How do you quit out of gdb? 如何退出 gdb?
q
1
# Exercise 3: Debugging w/ YOU(ser input)
这个练习要求我们实际通过命令调试一个带有输入输出的程序
# Exercise 4: Valgrind’ing away
这个练习要求我们使用 Valgrind 进行调试
# Exercise 5: Pointers and Structures in C
这个练习他给了我们一个算法要求我们实现来判断一个链表是否有环,直接实现即可
#include <stddef.h>
#include "ll_cycle.h"
typedef struct node *LinkedList;
int ll_has_cycle(node *head) { // 来判断链表是否有环
node *slow = head, *fast = head; // 快慢指针
while (fast != NULL && fast->next != NULL) { // 快指针和快指针的下一个都不为空
slow = slow->next; // 慢指针走一步
fast = fast->next; // 快指针走一步
if (fast != NULL) // 如果快指针不为空
fast = fast->next; // 快指针再走一步
else
break; // 如果快指针为空,说明链表末尾了,直接退出
if (slow == fast) { // 如果相遇了,说明有环
return 1;
}
}
return 0; // 如果快指针走到链表末尾了,说明没有环
}
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21