Placeholder Image

字幕表 動画を再生する

  • DOUG LLOYD: So if you're anything like me, when you write code

  • it's not always going to be perfect.

  • There's usually going to be something wrong with it.

  • And that is known as writing bugs.

  • And part of being a computer programmer is

  • learning how to debug, or remove the bugs, from programs.

  • And there are a lot of different techniques and strategies

  • that one might use to do this.

  • And what I want to do in this video is show you

  • three of the tools that are baked into CS50 IDE

  • that you can use to help with your debugging,

  • or help trying to figure out what's going on in your system.

  • And these three tools, which I'm going to just display the names of here,

  • are known as debug50, eprintf, and help50.

  • We're actually going to cover those in reverse order.

  • So we're going to start with help50.

  • So I have a couple of files pre-configured in my IDE.

  • I'm going to navigate into my help50 directory.

  • And just clear the terminal window here.

  • And what help50 basically does, it is designed

  • to be sort of a virtual teaching fellow of sorts.

  • It basically is trying to give you clues and hints about what might be going on

  • in your program or when you're trying to work with your program

  • and something goes wrong without just giving you the answer outright.

  • So it's trying to nudge you along in the right direction in the hopes

  • that it's an issue that, with a little bit of encountering and having

  • the computers sort of give you a little bit of back and forth,

  • it's an issue that you'll learn to be able to not recreate in the future.

  • So I have a couple of example programs here.

  • All of them are written in C. And let's just try and compile the first one.

  • So let's try and make example one.

  • Error.

  • So this is where help50 is going to come in handy.

  • Generally it's going to be used when you're working.

  • It's not so much a bug in your program that is

  • during runtime, a runtime error.

  • This is going to be a logical bug, or some kind

  • of bug that prevents your program from being compiled

  • is going to be the most common use case of help50.

  • So here I'm getting told I'm implicitly declaring library function

  • printf with blah, blah, blah.

  • There's a whole lot of text there.

  • Now, what help50 tries to do is it looks at that error message for you.

  • And if there are multiple ones it will look

  • at the first error message for you.

  • And it will try and translate that kind of arcane language

  • into something a little more, hopefully, intuitive to use.

  • And the way we use help50 is pretty straightforward.

  • We first type help50, and then we do exactly the command we tried to do.

  • So in this case it's help50, make example1.

  • Let's see if maybe help50 can give me a little more information about what

  • I might be doing wrong in my program.

  • So I run it again.

  • Now, I'm getting this sort of hint from the virtual TF,

  • which is written here in yellow.

  • "Did you forget to #include stdio.h in which printf

  • is declared atop your file?"

  • Well, let's take a look.

  • So in example 1.c, it looks like I did kind of forget to do that.

  • So if I just go ahead and add that, and I save,

  • and I try and make example one again maybe.

  • Hey, it compiled.

  • It worked just fine.

  • So I can now run the example1 program, hello world.

  • Now, of course, I have a logical bug here where I didn't put a backslash n.

  • So it kind of ran into a new line like that.

  • But that's OK.

  • That's something I can go ahead and fix pretty easily.

  • So let's try and compile example two.

  • And we'll go through this again where we won't look at the source code

  • beforehand.

  • We'll just try and compile it, thinking we're pretty close,

  • and see what happens.

  • So we'll try and make example2.

  • Oh.

  • Seven errors generated.

  • That's not good.

  • But, again, help50 can help us here.

  • And what it's going to do in this case is

  • it's going to look at the very first one and give us a hint for that one.

  • Now, oftentimes when you get a lot of errors from compiling

  • it's not necessarily that I've done seven things wrong.

  • In fact, if you look at the program you'll

  • see that it's not that many lines of code when we do.

  • But really it's just one error that sort of cascades and creates

  • another one because it doesn't understand what's happening before.

  • And if you can fix that one error at the beginning,

  • it might resolve one or more, or all, of the remaining errors.

  • So I'll clear my terminal window.

  • And we'll try this again.

  • But we'll preface this by saying help50 first.

  • So help50 make example2.

  • Let's see.

  • Use of undeclared identifier string.

  • Did you mean standard in?

  • Well, I'm assuming probably not.

  • Let's take a look at my code.

  • No.

  • I meant to use string there on line five.

  • So by undeclared identifier, Clang means you've used a name string

  • on line five which hasn't been defined.

  • Did you forget to #include cs50.h in which string is defined atop your file?

  • Well, here, again, I did.

  • I did forget to include cs50.h this time.

  • So it gave me another clue here.

  • But I had seven errors.

  • So hopefully this resolves most of them.

  • Lets clear this and see if it does.

  • We'll try and make example2 again.

  • And it did.

  • So even though it said I had seven errors, I really didn't.

  • I just had the one.

  • And because that one kind of threw everything else off,

  • it just kind of cascaded down and really messed us up.

  • All right.

  • Let's take a look at example three.

  • We'll try and make example three.

  • Seven errors again.

  • Ugh, man.

  • Let's see what I got here.

  • Looks like I have a for loop that's going from 1 to 10.

  • And it's supposed to print out a new integer on each line.

  • That looks OK to me, at least at first glance.

  • I don't really know what the issue could be here.

  • But help50 might be able to help us out again.

  • So we'll try one more time help50 make example3.

  • And, again, you'll see that help50 is commonly

  • going to be used most of the time-- although it's not all it can do.

  • And we'll take a look at a couple of examples of that in a second.

  • You're going to use help50 most of the time when there are compile time

  • errors that prevent your program from being compiled

  • and have to be resolved first.

  • So we'll try and help50 this one.

  • Looks like it says on line five it looks like you're

  • trying to declare a variable that's already been declared elsewhere.

  • I don't see that.

  • But then it tells me a little bit more.

  • If you meant to create a for loop be sure that each part is separated

  • with a semicolon rather than a comma.

  • So here I've got commas, and I'm supposed to have semicolons.

  • So it's, again, little things like this where it's little syntax things where

  • if you were able to get in touch with somebody

  • you could just kind of look at your code really quickly and be like,

  • oh, well, you really just need to put in a semicolon here instead of a comma.

  • That's the kind of thing where it stinks to get stuck in a jam where you're just

  • waiting for somebody to give you that sort of shining moment right

  • where suddenly the error is revealed.

  • You can use help50 to try and help you along and accelerate this

  • so that you don't get stuck waiting, and you can get a little resolution

  • for your errors a little more quickly.

  • Of course, we'll just confirm with trying to make it again.

  • And it compiled no problem.

  • I could run this program.

  • And it will print out the numbers 1 through 10, each on separate lines.

  • We can't see them all here because my terminal window is not big enough

  • to hold them, but they're all there.

  • All right.

  • I got one more example here.

  • And then I'll show you two examples of using help50 not in the make context

  • necessarily, or in the Clang context.

  • So let's try and make example4.

  • I have one error.

  • "Expression result unused, n times 5."

  • Not really sure what that means.

  • So let's just quickly pop open example4 here at the top.

  • Looks like what I want to do is have this print out 50.

  • So I set n as equal to 10.

  • I multiply n by 5 to get 50.

  • And I want to print out 50.

  • So I wonder what the issue could be.

  • So let's help50 make example4.

  • On line six of example 4.c you are performing an operation,

  • but not saving the result. Did you mean to print or store

  • the result in a variable?

  • So remember, when we are doing mathematical manipulations

  • to variables, we always have to assign the result to something.

  • And here what I probably meant to say is either n equals n times 5,

  • to change n from 10 into 50, or the more shorthand version,

  • n times equals 5, which would also work just fine.

  • So I can save this.

  • Hopefully this fixes it.

  • I'll try and recompile it.

  • And I'll try and run it to see if I get 50 printed out.

  • So that's a couple of uses of, in particular

  • this is help50 helping with Clang.

  • Although every time we were doing this we were typing make.

  • Remember that make is actually just a wrapper

  • for Clang, which is the name of the actual compiler

  • that we're using in CS50 IDE.

  • What happens though if I try and do this?

  • There's no file called example5.

  • So make example5.

  • Unlike the previous four, this is actually

  • going to be a help50 that helps with a message

  • from make as opposed to a message from Clang.

  • Now, obviously it's not there.

  • So you can probably figure out what it means.

  • But in case you didn't, I could preface it with help50.

  • "Do you actually have a file called example5.c in the current directory?"

  • Well, I don't.

  • So I'm trying to compile a program that doesn't exist.

  • So this is just help50, again, giving me a clue,

  • like, well, maybe you're making a little bit of a mistake here.

  • And it's true.

  • I am making a bit of a mistake.

  • One more example here.

  • Let's say that I want to change directories into another directory.

  • So I want to ls example6.

  • There is, again, no example6 here.

  • "ls cannot access example6, no such file or directory."

  • Again, help50 might be able to help with this.

  • So I'll preface it one more time.

  • Are you sure example6 exists?

  • Did you misspell example6?

  • It doesn't exist.

  • Maybe I had typed it wrong and I thought it was working.

  • So it's just a reminder.

  • Did you check that?

  • Oh, maybe I had misspelled it.

  • Maybe I meant to do something else, and then I can take a look.

  • Now, help50 is a tool that is still in development.

  • We actually have an open source GitHub repository.

  • So as you go through the class and you become more comfortable, particularly

  • as you start to learn Python, which is the tool that help50 is written in,

  • you can actually head to CS50's public GitHub repositories and take a look

  • and see what types of error messages that

  • are a little more arcane that we're trying to catch and have help50

  • explain in more human understandable terms

  • so that if you want to contribute to that you actually can.

  • And if you encounter an error that you're not

  • getting, that help50 doesn't understand, and you want to report that error

  • and hope that somebody else maybe can help match it,

  • you can do that through that platform as well.

  • So this is definitely a collaborative tool.

  • And we welcome you to continue to contribute either solutions as you

  • become more comfortable, or issues that we can try and resolve

  • to make it a more robust tool.

  • All right.

  • So another tool that we have is one called eprintf.

  • Now, it's really common when you are starting to debug your programs to kind

  • of pepper them with printf statements.

  • It's sort of like a sanity check where you're checking,

  • OK, well, this line printed.

  • And then I have some lines of code that run.

  • And then this line printed.

  • Like you're trying to figure out where things are going wrong.

  • The trouble with printf is it doesn't necessarily tell you

  • where the program is going wrong.

  • You have to kind of go back and scroll through your source code