Comp 2560 Lab No.2 - GDB Debugging in C Program (Copy.c)

preview_player
Показать описание
This video will show how to step by step debug a C file using GDB.
It shows how you can set up a breakpoint at main and specific lines(break main, 17, 22)
It demonstrates how to print values and display variables (print fd1, fd2, n1 and display fd1, fd2)
And how to logically reason your way into finding the errors within the code you have and make adjustments (priority errors, bracket error, array size error).
Hopefully this helps your understanding of GDB and how it can be used to improve your coding skills to debug.

In the file provided (copy.c) we notice that the file does not behave as intended.
When we use gdb, we notice there is an error in the first if loop.
When you explore the values, you notice that fd1 and fd2 are not in sequence - something must be wrong.
Therefore when you explore it further and look back at the code - you notice that the predidence of operation in the fd2, goes to "==" which alters how our condition behaves.
When we add additional brackets to fd2 - it will allow for the file to be stored correctly.
But we still have errors still with the code.
When you run gdb again, you notice that there is another issue with the while loop.
Lets exit and explore that further - back to the code.
We notice that the buffer is not set to match the contents being passed to the buffer. We can either change the buffer size to match what is being passed; or what is being passed to match the buffer size. [I altered the buffer size].
When you run it again, there is still an issue; gdb identifies it is still at the while loop.
Back to the code.
Again the brackets are incorrect, causing the condition not to match what our intended condition was.
Lets move the brackets around; to reflect what we wanted the code to achieve.
Run the file again. It works!
Рекомендации по теме
welcome to shbcf.ru