CSE 333 26su
Section 2
Debugging and Structs
Checking In & Logistics
2
Debugging Tools
3
Debugging
4
Debugging Strategies
Many debugging strategies exist but here’s a simple 4 step process!
5
1. Observation: What is the difference between the expected and actual behavior?
e.g., Expected NumElements(llp) to be 3, actually 627147133
2. Hypothesis: What do you think is going wrong?
3. Experiment: Use debuggers and other tools to verify the problem
4. Analyze: Identify and implement a fix to the problem.
Key Debugging Skills to Master
6
333 Debugging Options
7
Basic Functions in GDB
8
Common Errors
9
Debugging Example: reverse.c
10
Trying to Run reverse.c
We have a program reverse.c that accepts a string from the user and reverses it, but it has a few problems… let’s take a look!
11
char * Reverse(char * s) {
char * result = NULL; /* the reversed string */
int left_i, right_i;
char ch;
strcpy(result, s);
left_i = 0;
right_i = strlen(result);
while (left_i < right_i) {
ch = result[left_i];
result[left_i] = result[right_i];
result[right_i] = ch;
++left_i; --right_i;
}
return result;
}
Structs and Typedef Review
12
Defining Structs
13
struct simplestring_st {
char* word;
int length;
};
struct simplestring_st my_word;
Typedef
14
struct simplestring_st {
char* word;
int length;
};
typedef struct simplestring_st SimpleString;
SimpleString my_word;
typedef struct simplestring_st {
char* word;
int length;
} SimpleString;
SimpleString my_word;
Structs and Memory Diagrams
15
typedef struct simplestring_st {
char* word;
int length;
} SimpleString;
SimpleString my_word;
?
my_word
length
word
?
Structs and Pointers
16
char cse333[] = "cse333";
SimpleString cse333_ss;
SimpleString* cse333_ptr = &cse333_ss;
cse333_ss.word = cse333;
cse333_ptr->length = strlen(cse333);
6
cse333_ss
length
word
'c' | 's' | 'e' | '3' | '3' | '3' | '\0' |
cse333
cse333_ptr
typedef struct simplestring_st {
char* word;
int length;
} SimpleString;
Passing Structs as Parameters
17
SimpleString new_ss = cse333_ss;
6
cse333_ss
length
word
'c' | 's' | 'e' | '3' | '3' | '3' | '\0' |
cse333
cse333_ptr
6
new_ss
length
word
Aside: HW1 Diagrams and Connection
18
Debugging Exercise
19
Debugging Exercise – simplestring.c
The code for simplestring.c is on your worksheet!
20
21
Tool | Question it answers | Example |
📝 Logging | What happened? | "User 123 failed login" |
📈 Monitoring | Is the system healthy? | CPU = 90%, Error Rate = 8% |
🔍 Tracing | Why was this request slow? | Request → API → Auth → Database → Payment |
Production Debugging 101: Logging vs. Monitoring vs. Tracing
Think of a production system like a hospital
Extra readings:�
Debugging Strategies
Many debugging strategies exist but here’s a simple 4 step process!
22