Caveman Debugging: Simple Trick to Debug C++ Code Like a Pro!

Caveman Debugging: Simple Trick to Debug C++ Code Like a Pro!

Hey coders! Ever hit a wall with a program that just won’t work? In this video, I’m spilling the beans on caveman debugging—a super simple, no-fuss way to hunt down bugs in your C++ code (or any language, from assembly to Python)! No need to wrestle with complex debuggers; this method is all about using print statements to trace what your code is doing, step by step.

I’ll show you a real C++ program where things go wrong (think integer overflows and mystery crashes) and walk you through how to sprinkle in cout statements to spot the problem fast. You’ll learn how to label decision points like loops and if-statements, break down complex expressions with temporary variables, and format your debug output so it’s easy to read. Plus, I share a neat trick called short circuiting to toggle your debug prints on or off without deleting them—saving you tons of time!

Whether you’re a beginner just starting out or a pro debugging a massive codebase, caveman debugging is a lifesaver. It’s quick, it’s intuitive, and it fits my “let’s figure this out” vibe perfectly. Stick around to see how I catch a sneaky bug in a for-loop and fix it in minutes.

Why watch?

  • Learn a beginner-friendly debugging technique that works in any language.
  • See real code examples with clear, relatable explanations.
  • Get tips to make your debug output readable and avoid hours of frustration.
  • Discover how to short circuit print statements for reusable debugging.

If you’re ready to level up your coding game and squash bugs like a champ, smash that play button! Don’t forget to subscribe and hit the bell for more coding tutorials, from beginner hacks to advanced tricks. Drop a comment if you’ve ever used caveman debugging or have a bug you need help with—I read every one! Check out my next video on using a proper debugger for even more debugging goodness.

Scan the QR code on-screen or visit my website (linked below) for more tutorials, services, and coding resources. Let’s keep the coding community growing—your support means the world! #CavemanDebugging #CodingTips #DebuggingHacks

Hey everybody! In this video I’m gonna try to familiarize you with a concept

called caveman debugging. It’s just as bad as it sounds but it’s really really

really useful. So suppose for the sake of argument you’re writing a program I’m

gonna show a C++ program here but this this concept is not just for C++

debugging it’s also for assembly and any other kind of language that you can come

other kind of language that you can come up with but suppose I have a program and I’m trying to

figure out why it’s not working so if you look at this program right here that I’ve kind of written

up already we have a main function here and we have like a little hello message that’s not my

name I love that name though and we have a variable a and then we just sort of do some nonsense to it

to try and compute a value for a when I say nonsense I mean literally I’m just I just made

I’m just I just made up instructions the point of this video is not to show you some advanced program

It’s just to show you that you know

This is how you can possibly debug a program that has gone awry

So you can imagine at home that this code you’re seeing right here might be your larger project your larger

Code base whatever it is that you’re doing that is not working for some reason and you’re not sure why

So, um, let me run the program real fast

It basically is just like a little for loop and then it just sort of

of you know not randomly but it just kind of increases the value of a for no reason

a bunch of times and then it calls a function that will crash later

so let me show you what happens when we try to run this program okay do do do

okay i want to go clear and make run if you don’t know how to do a make file or compile

or link or anything like that uh then go see my other videos so clear and make run

What did I do wrong?

TempResult was not declared in this scope.

Okay, well, this is now a video on reading compiler errors.

Line 59, I probably left something out.

Oh, because I just changed it.

I forgot to put it back in there.

Return tempResult, we should just return input times two.

And I’m just making this up.

It’s not really like a valid algorithm.

Okay, so we run our program.

We print the final result and we realize for some reason the final result is wrong.

result is wrong. So I don’t, I mean, I just made this up. So I don’t really know what the right

answer is. But just imagine that you are looking at some sort of an output, some sort of a final

state, maybe a crash. And you realize, oh, my program doesn’t work, but it’s too complicated

to figure out. Caveman debugging to the rescue. Caveman debugging kind of is as bad as it sounds.

It’s just really, really simple. But it’s also really useful, especially if you just want to do

on you don’t want to dive deep into a full-on debugger or do something more advanced it literally

is just printing a lot of things while your program is running i do it all the time i usually

don’t even go to a debugger unless i really get in trouble with like some huge mess um okay so

the first thing i’m going to say is in this for loop we uh you know we’re kind of like iterating

And so probably this is a good idea to announce that we’re iterating in the for loop.

So I’m just going to go Cout.

In C++, you know, we have the Cout object that we can use to stream texts to standard output,

which usually goes to the terminal.

So use whatever construct you’re using for your particular language.

So we’re going to do Cout.

And I should say also, if you’re programming in assembly,

if you are programming in assembly,

it’s a little bit more complicated than just writing a Cout.

little bit more complicated than just writing a C out you probably have to have some pre-computed

messages and then you can use a library to print a number to the screen or use printf to print stuff

to the screen but you can do it just takes a little bit longer so anyway conceptually I’m

just going to say this is the first iteration of my loop so I’m going to sort of announce it I’m

going to say main and then I’m going to call it main four or how about primary four or first four

but I want to try to label the for loop.

And maybe instead of leaving those parentheses blank,

I could say iteration number i right there,

or maybe I could do iteration i.

That would be pretty good.

Then I just have to replace the i with the actual variable i

so that the i keeps increasing.

So I’m just going to like do another stream operator here

and I’m going to say i.

Then I’ll print a new line.

we’ve got, you know, one iteration of our loop.

Maybe I should also print what is the current value of A

because the point of this, you know, weird nonsense code

is we’re coming up with a value for A, right?

So I should just print what A is at the top of each iteration of the for loop.

So I’m going to say A is equal to, and then do another A at the end of it.

And then maybe at the bottom of the for loop,

when the for loop is actually ending one of its iterations,

I can just say we’re at the bottom of the iteration so maybe I’ll put to end

iteration and then print the final value of a and then at the top I’ll say begin

iteration at this point you might start to feel like hey aren’t you using too

many words here like why can’t I just print a bunch of numbers I mean you

could but when you’re coding you need all the brainpower you can get to not

just code but to debug coding is already hard enough without making it harder

harder for yourself than you need it to be. So, you know, I see people all the time, they’re

trying to debug their code and they just like start printing numbers. And after not very long

at all, they’re looking at the debug output and they’re just like, what did that number mean?

Where is the number I’m looking for? It’s just a bunch of numbers. It’s harder to debug when you

do it that way. Do it the nice and pretty way. It just costs you a couple of extra keystrokes to do,

you know, nice letters and new lines and things, nice words, labels. Isn’t that still a lot better

Isn’t that still a lot better than debugging for an extra six hours?

Because you can’t really understand what you’re seeing.

Anyway, let me show you what this looks like so far with just those two lines added.

Now we can kind of see, alright, alright, on iteration number 61, where I was 61, then

this was the first value of A and then after that this was the second value of A, you know,

when we were finished with the iteration.

It seems to have gone up by a certain amount.

you know it was like a negative blah blah blah 836 and then negative blah blah blah 708 so it

kind of increased in value all right that’s a little bit helpful now it’s a really good uh

place to uh print our decision points so anytime in your code where you have an if or an else or

a function call or anything where you are kind of like you know making a decision or processing your

data in some way it’s probably a good idea to announce your decisions so i could say up here i

say up here i can go maybe i’ll say uh main first four and then i’ll call it uh maybe i’ll do a

double colon there and i’ll say top if just to make it easy to read that i’m kind of like at

the first if statement um maybe i’ll even put parentheses i’ll say a is more than 20 just to

kind of like name what i’m actually doing and then i could replace the a with the actual value of a

actual value of a so i could say a like that and maybe uh when i’m reading this later i might want

to kind of actually see the variable name so i could say uh you could just like put a as a string

and then maybe its value in parentheses or something like that do a new line um and then

maybe after that top thing i’ll say uh true to indicate that we’re inside of the the if statement

the if statement and then maybe at the bottom of that if statement we’ll just say

a is now true and we’ll say like a is now you know whatever the new value of a was so just

basically we’re announcing that we decided to go into this if block and then we announce what we

ended up changing a to and also you probably anytime you see a compound expression like this

You know B time something or a plus something or just you know a big part of multiple parts of the expression any expression

That’s more than just like one variable or one number. It’s probably a good idea if you break that up into temporary variables, too

and then

Print out each variable and I know that’s a lot of stuff to read but again it beats debugging for an extra six hours

I’m gonna leave that be for now though

leave that be and

I’m just gonna continue adding comments into the rest of the code. So I’m gonna do the second if block here

So I’m going to do the second if block here.

I’m going to say we’re in the first floor.

We’re in the bottom if more than 20 true.

And then we’ll print out what is the value of a after we’re finished.

We’ll say a is now is now whatever value for a.

And then I’ll do the same thing for this function call.

Because, you know, when I call this function, it’s going to change the value somehow.

So we’re going to do cout about to a plus equals f, you know, call to f.

And then inside the arguments, I’m obviously putting i plus 3.

But because i is a variable, we could just print that.

So I’m going to do quote, quote, and then just stick the actual i in there.

And then another new line.

a finished a plus equals f and then I’ll just say you know a is now you know a you know print the

new value of a you could also say a equal equal a and then print the value whatever you want but

the point is I’m just printing everything here so now let’s go down a little bit more because

there is a function call if you look at this function call down here here’s like some nonsense

being used here’s some other like you know disabled code really what i’m doing at the

very end is i’m just taking the input that the function received and i’m just multiplying it by

two and returning it so again imagine that your code is a lot more complicated than this

maybe input times two is actually input times two divided by three and then some other function

call and then some other this and some other that and whatever so a lot of people will just kind of

stuff like a really complicated expression in one line or one assignment or one return statement

And that’s super confusing because you can’t really be sure that every part of the expression is as you thought.

So it’s a really good idea to break it all up into parts and print each part.

So you could imagine that we have like a C out here and we’ll say C out, you know, begin F.

I’ll say like begin for F and then F receives an input of input.

Do an end L.

And then I’ll make a temporary variable here.

variable here I’ll say int temp results equals input times two and then I’ll

print that f and maybe I’ll do the input again

temp result is now and then say temp result so I’m just gonna print what the

temp result actually was and I could print something about I just multiplied

thing about I just multiplied the input by two and then you know the more

complicated your expression is the more temporary variables you want to use and

just kind of print every single one of them and print you know this new

variable is the result of dividing by two this new variable is the result of

calling some other function and so forth

so then we have like a basically you know basically a pretty good idea maybe

at the end though instead of multiplying by two again you want to make it more

want to make it more consistent with your debug output so like here temp result is obviously going

to be the result that i return why would you do the expression all over again for the return part

when you could easily get it wrong why not instead just return the temp result that you made right so

then that way your output completely matches what you’re actually returning and for now i think

that’s all we need to do let me run it one more time and then you’ll see a bunch of stuff now uh

Now, we have a lot of information that we can use to trace how our program was thinking and hopefully find the problem.

But isn’t this like a little bit starting to get a little bit hard to read, right?

You can see that there are kind of blocks happening whenever we iterate.

So, you know, for me, I always try to keep in mind you need all the brainpower you can get.

Why not just format it a little bit better so it’s really easy to just quickly look at one block and see, you know,

the couts kind of like belong together so i’m just going to add one more cout in the main for loop

here just at the very bottom i’m going to go couts and l just so that i get a new line and if i run

the program again notice how it’s easier to see the blocks like oh that’s clearly one iteration

right here you know your mind just grabs onto it faster and that increases your brain power and

makes it easier to debug and read the debug output so i can go all right all right okay so

all right okay so we’re right up here oh around uh iteration um number 83 uh a was this number

and then it got increased by uh this other number and the reason that that happened is because the

function returned 172 so it basically increased it by 172 and then it ended up being that number

oh okay i think i see what went wrong oh no right in this particular program if we scroll up just a

little bit we can probably see that the integer is overflowing right the integer was getting bigger

right the integer was getting bigger and bigger and bigger here and then eventually

it jumped into the negative at some point so right here we can see oh okay when did this

number become negative suppose we didn’t want it to be negative um what was this this is like

a million this is a billion no wait wait wait this is uh no that’s a million right there so it’s like

299 million and then it overflowed oh no no it didn’t it didn’t overflow it started at 299

it started at 299 then at the bottom it was sitting at 899 million that’s kind of close to a billion

and it did jump up from 300 million to 900 million so that’s a jump up of like 600 million

oh you know it’s like kind of jumping up faster with each iteration oh and i’m using a 32-bit

signed integer which has a maximum value of around 2 billion so now i could possibly realize at this

the number is just like it became too big and I need to change my data type or I need to change

my algorithm for some reason right so you can kind of get to the bottom of things pretty quickly

so here’s another problem that people encounter when they’re using caveman debugging

basically you you add all these cout statements or print statements or whatever you’re doing in

whatever language you have and then when you’re finished you’re kind of like well I guess uh

I need to delete all of these Couts because I don’t want all this you know

Junk being printed in my program after I fix the problem or maybe you’re trying to debug a different part of your program

So then you start commenting out all the print statements or you start

You know deleting them or whatever and then whoops later on you realize maybe that part of my program

Was not actually fixed and I have to add all the Couts all over again from scratch that costs time

So that’s not fun. So here’s a trick that I like to use. It’s called a short circuit

it’s called a short circuit it’s a sort of like a beginning C++ thing or just

when you’re first learning how to use logical operators for boolean

expressions so I’m gonna do a boolean and I’m just gonna name it after what

problem I’m trying to solve so we could just call this you know main C outs or

something like that let’s I don’t know primary problem I’ll call it primary

the boolean as true because then what will happen is uh we can then short circuit all of the cout

statements we can say primary problem couts and cout like that and the way the short circuit works

works is if you have a logical and expression you can see right here we have two sides

of that statement now one’s on the left which is just the boolean and one’s on the right which is

have a logical and then you know one and zero is equal to zero true and false is equal to false so

both things have to be true for the expression to evaluate to true which means if the first thing

is false there’s no point in even looking at the second thing at all because if i if i set that to

false then the second part the c out is it doesn’t matter the whole expression is going to evaluate

to false anyway so uh the logical operator itself will just you know block out the rest of the

you know, block out the rest of the statement.

That’s called a short circuit.

So I’m going to copy the short circuiting to all of my C out statements like this.

Okay.

And do that.

And then the C out there.

And I guess I’ll leave the final result there.

And for the moment, you know, this is just a regular function.

apply the short circuiting to this other f unless I do a separate boolean but

keep in mind if this was a class and you had two different methods that you

wanted to short circuit see outs inside of at the same time you could just use a

member variable and name it after the problem you’re currently trying to solve

and then just short circuit each see out in the appropriate method with that

boolean so I guess in this regular program that doesn’t have a class I

could move the boolean into the global if I wanted to but I’m not going to right

if I wanted to, but I’m not going to right now.

And if you’re writing assembly or some simpler language

and you’re thinking, hey, I can’t short circuit an assembly

or some other language, well, you could write a function

that just takes a couple arguments.

You could write a function that takes a string to print

and maybe a number to print or just, you know,

something simple like that.

And then in the function itself that prints,

you could call it debug print if you want.

Have it look at a global variable or define

that you’ve set up at the top of the assembly module.

set up at the top of the assembly module, which you can just turn on and off,

like just do a data byte and set it to a zero or set it to a one.

And then the function debug print will just look at that variable to decide

whether or not it’s going to actually print or not.

So, you know, it’s a little more complicated in assembly,

but you can do it basically the same concept.

Let’s run this again just to make sure it still works.

Okay, so it still works.

So now suppose I want to turn off all the Cout statements.

If I just change the true to false,

the true to false notice how it’s all gone except for the stuff inside of f which i told you i

wasn’t going to touch but notice how easy it was to mute it and then if i want to bring it back

later i just go true to unmute it of course if you want to do a little bit more typing you can

also do if statements like if this condition is true and that condition is true then print the

debug statement so then you can have a more complicated way of muting and not muting but

I’m going to leave it as is right now.

Let’s see, what else do we got here?

I think actually at this point, this is the basic idea of caveman debugging.

Just basically print everything.

Print all of your decision points.

Use pretty labels that are really, really easy for you to understand.

Use formatting so that your brain doesn’t have to struggle to understand what you’re seeing.

If you just print a bunch of numbers, you’re going to be shooting yourself in the foot.

you’re going to be shooting yourself in the foot and use short circuiting or if statements to

selectively mute them so you can do less typing and and debugging the debugging and so forth but

yeah i think i think this is all i really wanted to show you in this particular video

i hope you feel like an expert in caveman debugging i certainly am and it you know it

kind of fits my personality a little bit i’m like what’s going on um and like i said before

debugger it’s probably a better idea but I usually don’t unless I’m in big trouble

and I have a huge mess on my hands I usually just go directly to caveman

debugging we’ll look at the same piece of code in my next video that I’m going

to publish which is going to talk about using a proper debugger so watch the

next one also but for now I hope you’ve enjoyed this video thank you for

watching I hope you learned a little bit and had a little bit of fun see you in

video

whoops what the heck

hey everybody thanks for watching this video again from the bottom of my heart

i really appreciate it i do hope you did learn something and have

some fun uh if you could do me a please a small little favor could you please

subscribe and follow this channel or these videos

or whatever it is you do on the current social media website that you’re looking

at right now it would really mean the world to me and

more videos and grow this community so we’ll be able to do more videos longer

videos better videos or just I’ll be able to keep making videos in general so

please do do me a kindness and and subscribe you know sometimes I’m

sleeping in the middle of the night and I just wake up because I know somebody

subscribed or followed it just wakes me up and I get filled with joy that’s

exactly what happens every single time so you could do it as a nice favor to me

or you could you control me if you want to just wake me up in the middle of the

and then i’ll i’ll just wake up i promise that’s what will happen also uh if you look at the middle

of the screen right now you should see a qr code which you can scan in order to go to the website

which i think is also named somewhere at the bottom of this video and it’ll take you to my

main website where you can just kind of like see all the videos i published and the services and

tutorials and things that i offer and all that good stuff and uh if you have a suggestion for

clarifications or errata or just future videos that you want to see please leave a comment or

if you just want to say hey what’s up what’s going on you know just send me a comment whatever i also

wake up for those in the middle of the night i get i wake up in a cold sweat and i’m like

it would really it really mean the world to me i would really appreciate it so again thank you so

much for watching this video and um enjoy the cool music as as i fade into the darkness which is

us all.

Thank you.

Comments

No comments yet. Why don’t you start the discussion?

Leave a Reply