I never use GDB before though I’ve heard of it long times ago. Well, I never use it simply because I never really debug program. And maybe I never write programs complicated enough that needs to debug. Though I’ve found time spent on debugging code is quite a lot. I never think of how to improve this. And I never care about the potential bugs left in the code as long as the code begins to work.
My previous experience with debugging is awful. Inserting printfs here and there and finally mess up the code. Changing here and there and later forget where have been changed.
I’m doing the labs of the Berkerley CS61C today and was required to used GDB to complete a exercise exploring the code of K&R’s memory allocator. This power tool get along well with Emacs editor which I’ve been using for studying this opencourse. To start gdb, you just fire it off from Emacs using the command M-x gdb.
The feature I love most is the print command. It make you feel like you are using a python shell. You can print out the value of a variable: ’p a’, a expression `p (int) A->array[5]' , assign values: `p a = 5', execute function: `p f(4, a)'. And In my very exercise, I use ((Header*)0x93db008)[-1].s.size to find out the size of allocated block. And there’s lots of other features I would like to explore later.
GNU has provided lots of powerful tools that make programmers’ life happier. But unfortunately most CS students in China are not get introduced to these powerful tools in their classroom.