WorksheetsDebuging Arena - C Language
Total questions: 5
Worksheet time: 25mins
What will be the execution order?
10 + 3 + 20 = 33
10 + 3 + 30 = 43
10 + 2 + 30 = 42
Undefined behavior
Scenario: A developer needs to implement a simple "queue" from an array. When an item is "dequeued" (removed from the front), they try to shift all other elements one position to the left.
Bug: The program runs, but sometimes it crashes with a "Segmentation Fault." When it doesn't crash, the output is 20 30 40, but the last element of data_queue is now a huge, random garbage value.
Question: What is the root cause of the garbage data and the occasional crash?
The for loop should have been i <= size to make sure it copies the last element.
When i is 4 (the last iteration), the code tries to read data_queue[5], which is outside the array's bounds. This is Undefined Behavior.
You must use memmove() to shift an array, as assigning data_queue[i] = data_queue[i+1] is not allowed when the memory overlaps.
The size variable is incorrect; it should be sizeof(data_queue) / sizeof(int).
Scenario: A junior developer is writing a utility. They malloc a pointer, use it, and free it. To save memory, they try to reuse the variable name p to hold an integer status code. Then, they accidentally call free on the status code.
Bug: The developer is trying to debug the free(p) runtime error, but the code won't even compile. The compiler fails on the line int p = 5;.
Question: Why is the compiler throwing an error at int p = 5;?
It's a Runtime Error, not a compile error. The free(5) line is the real bug.
The compiler is smart and sees that free(p) will be called on an int, so it stops the code from compiling.
The variable p is being rede-clared (as int) in the same scope where it already exists as int *. This is a "conflicting types" error.
p is a const (constant) after being freed, so it cannot be reassigned.
Scenario: A developer is writing a logging function. To prevent sensitive data from being written to disk, they try to "censor" a password in a log message in-place to save memory.
Bug: The program prints "Original: Pass: 123" and then immediately crashes with a Segmentation Fault (or "Access Violation").
Question: What is the fundamental reason for this crash?
msg[9] is out of bounds; the string "Pass: 123" only goes up to index 8.
The log_message function was passed char msg, but it should have been const char msg. This type mismatch is causing the crash.
The msg pointer points to a string literal (like "Pass: 123"), which is stored in a read-only section of memory. Attempting to write to it (msg[7] = 'X') is Undefined Behavior and causes a hardware exception.
Strings must be modified with strcpy(), not [] assignment.
Scenario: A developer thinks malloc is too slow. To "optimize" their code, they write a helper function that creates a small, 4-element lookup table as a local array and returns a pointer to it.
Bug: The developer is shocked by the output. The program prints: Helper: local_table[0] is 10 Main: p_table[0] is 1345234 (some random number) Main: p_table[1] is -9487271 (some other random number)
Question: Why is p_table in main() full of random garbage data?
The local_table array was created on the get_lookup_table function's stack. When that function returned, its stack was deallocated. p_table is now a dangling pointer to invalid memory.
The printf call in main() is running before get_lookup_table can finish, so the data isn't ready yet (a race condition).
The global arr is interfering with the local_table memory, as they are both arrays.
A compiler error should have occurred. A function cannot return an int* if the array was declared as int[].
