<?xml version="1.0" encoding="UTF-8"?><rss version="2.0"
	xmlns:content="http://purl.org/rss/1.0/modules/content/"
	xmlns:wfw="http://wellformedweb.org/CommentAPI/"
	xmlns:dc="http://purl.org/dc/elements/1.1/"
	xmlns:atom="http://www.w3.org/2005/Atom"
	xmlns:sy="http://purl.org/rss/1.0/modules/syndication/"
	xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
	>

<channel>
	<title>call stack Archives - NeuralLantern.com</title>
	<atom:link href="https://www.NeuralLantern.com/tag/call-stack/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.NeuralLantern.com/tag/call-stack/</link>
	<description></description>
	<lastBuildDate>Sat, 07 Mar 2026 22:50:19 +0000</lastBuildDate>
	<language>en-US</language>
	<sy:updatePeriod>
	hourly	</sy:updatePeriod>
	<sy:updateFrequency>
	1	</sy:updateFrequency>
	<generator>https://wordpress.org/?v=7.0</generator>

<image>
	<url>https://www.NeuralLantern.com/wp-content/uploads/2025/04/cropped-2025-04-04-Lantern-03-32x32.png</url>
	<title>call stack Archives - NeuralLantern.com</title>
	<link>https://www.NeuralLantern.com/tag/call-stack/</link>
	<width>32</width>
	<height>32</height>
</image> 
	<item>
		<title>The Call Stack Explained: Call Frames, Return Addresses, Local Variables</title>
		<link>https://www.NeuralLantern.com/the-call-stack-explained-call-frames-return-addresses-local-variables/</link>
					<comments>https://www.NeuralLantern.com/the-call-stack-explained-call-frames-return-addresses-local-variables/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sat, 07 Mar 2026 22:50:18 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[assembly language stack]]></category>
		<category><![CDATA[C++ stack]]></category>
		<category><![CDATA[call frames]]></category>
		<category><![CDATA[call stack]]></category>
		<category><![CDATA[call stack explained]]></category>
		<category><![CDATA[call stack tutorial]]></category>
		<category><![CDATA[computer science fundamentals]]></category>
		<category><![CDATA[function call stack]]></category>
		<category><![CDATA[heap vs stack]]></category>
		<category><![CDATA[how function calls work]]></category>
		<category><![CDATA[local variables stack]]></category>
		<category><![CDATA[memory management]]></category>
		<category><![CDATA[process stack]]></category>
		<category><![CDATA[programming under the hood]]></category>
		<category><![CDATA[recursion stack overflow]]></category>
		<category><![CDATA[return address]]></category>
		<category><![CDATA[stack frames]]></category>
		<category><![CDATA[stack memory]]></category>
		<category><![CDATA[x86-64 call stack]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=314</guid>

					<description><![CDATA[<p>The call stack is a special stack data structure that your computer uses to manage function calls. Every time a function is called, a call frame is pushed onto the stack containing the return address, function arguments (beyond what fits in registers), preserved registers, and space for local variables. When the function returns, its call frame is popped and execution jumps back to the return address - this is how your program knows exactly where to continue after a function finishes.</p>
<p>The post <a href="https://www.NeuralLantern.com/the-call-stack-explained-call-frames-return-addresses-local-variables/">The Call Stack Explained: Call Frames, Return Addresses, Local Variables</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="The Call Stack Explained: Call Frames, Return Addresses, Local Variables" width="1380" height="776" src="https://www.youtube.com/embed/kT_s5XADSgs?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p class="wp-block-paragraph">Ever wonder how your program remembers where to return after calling a function? That&#8217;s the call stack&#8217;s job.</p>



<p class="wp-block-paragraph">In this clear, whiteboard-style explanation we cover:</p>



<ul class="wp-block-list">
<li>What the call stack actually is</li>



<li>How call frames are built and what they contain</li>



<li>Return addresses, function arguments, local variables</li>



<li>Why recursion works (until you overflow the stack)</li>



<li>Stack vs heap memory differences</li>



<li>Why too much recursion = crash</li>
</ul>



<p class="wp-block-paragraph">Great for beginners to intermediate programmers who want to understand what&#8217;s happening under the hood. References Ed Jorgensen&#8217;s excellent (and free) x86-64 assembly book.</p>



<p class="wp-block-paragraph">00:00 Introduction to the Call Stack<br>00:16 Prerequisites &#8211; Understanding Basic Stacks<br>00:41 Simple Example Program with Function Calls<br>01:24 Function Call Chain and Recursion Example<br>02:20 Visualizing the Call Sequence<br>03:06 Local Variables and Return Values<br>03:55 Quick Recap &#8211; How Generic Stacks Work<br>04:34 Stacks Can Hold Any Data Type<br>05:36 Introducing the Call Frame Concept<br>06:22 What Belongs Inside a Call Frame<br>07:00 Return Address Explained<br>07:44 How the CALL Instruction Works<br>08:32 Function Arguments on the Stack<br>09:57 Extra Arguments Beyond Registers<br>10:19 Preserving Registers (Callee-Saved)<br>11:12 Local Variables on the Stack<br>11:40 Multiple Call Frames Example<br>12:54 Tracing Function Calls Step by Step<br>13:21 Starting with Main&#8217;s Call Frame<br>15:00 Pushing Frames for Nested Calls<br>16:30 Returning &#8211; Popping Frames<br>18:10 Why the Stack is Perfect for Returns<br>19:45 Recursion and Multiple Instances<br>21:30 Stack Grows Downward in Memory<br>23:10 Stack vs Heap Memory Comparison<br>25:12 Local Variables vs Dynamically Allocated Memory<br>26:16 Pointers on Stack Pointing to Heap<br>27:32 Automatic Cleanup of Stack Variables<br>28:00 Memory Leaks with Heap Allocations<br>28:56 Recommended Reading &#8211; Ed Jorgensen Book<br>29:56 Call Frame Layout in x86-64 Assembly<br>31:16 Process Memory Layout Overview<br>32:22 Stack Grows Down, Heap Grows Up<br>33:52 Stack Overflow from Deep Recursion<br>35:09 Summary &#8211; Why the Call Stack Matters<br>35:28 Closing Remarks and Call to Subscribe</p>



<p class="wp-block-paragraph">=-=-=-=-=-=-=-=-=</p>



<p class="wp-block-paragraph">Thanks for watching!</p>



<p class="wp-block-paragraph">Find us on other social media here:</p>



<ul class="wp-block-list">
<li>https://www.NeuralLantern.com/social</li>



<li>Twitter / X: https://x.com/NeuralLantern</li>



<li>Rumble: https://rumble.com/c/c-3696939</li>



<li>BitChute: https://www.bitchute.com/channel/pg1Pvv5dN4Gt</li>



<li>Daily Motion: https://www.dailymotion.com/neurallantern</li>



<li>Minds: https://www.minds.com/neurallantern/</li>



<li>Odysee: https://odysee.com/@NeuralLantern:5</li>
</ul>



<p class="wp-block-paragraph">Please show your support!</p>



<ul class="wp-block-list">
<li>Buy me a coffee: https://ko-fi.com/neurallantern</li>



<li>Subscribe + Sharing on Social Media</li>



<li>Leave a comment or suggestion</li>



<li>Subscribe to the Blog: https://www.NeuralLantern.com</li>



<li>Watch the main &#8220;pinned&#8221; video of this channel for offers and extras</li>
</ul>



<p class="wp-block-paragraph">Hello there. In this video we&#8217;re going to talk about the call stack. What is the call stack?</p>



<p class="wp-block-paragraph">It&#8217;s an important data structure inside of your computer that helps your computer call</p>



<p class="wp-block-paragraph">functions inside of code. It&#8217;s pretty cool.</p>



<p class="wp-block-paragraph">So first off, before you watch this video, you should probably already understand</p>



<p class="wp-block-paragraph">how a stack works, what a stack is in terms of its interface and how to manipulate it</p>



<p class="wp-block-paragraph">See my previous video if you don&#8217;t understand how to use stacks yet. In this video, I&#8217;m just going to talk about the call stack, which is just a special use of the generic stack abstract data type data structure.</p>



<p class="wp-block-paragraph">Okay, so let&#8217;s see. First off, let&#8217;s suppose for the sake of argument that I have, you know, a function called main. This is not a C++ video or a coding video, but the call stack is related to coding.</p>



<p class="wp-block-paragraph">mean when I write down some code. This is just a simple C++ program. You can imagine that</p>



<p class="wp-block-paragraph">up above main, actually I don&#8217;t want to use prototypes, but I&#8217;m going to put the other</p>



<p class="wp-block-paragraph">functions below. Please forgive me. You can imagine that main calls on the function f,</p>



<p class="wp-block-paragraph">so I&#8217;m just going to do a function called f here. And so execution, when it gets to that part of</p>



<p class="wp-block-paragraph">the program on line three, execution is going to jump down to line nine to just execute the f</p>



<p class="wp-block-paragraph">And then maybe F calls G and then G it calls H and maybe H sometimes calls itself.</p>



<p class="wp-block-paragraph">Maybe we&#8217;ll say that it&#8217;s a recursive function.</p>



<p class="wp-block-paragraph">We&#8217;ll say if, you know, something happens, then we will return a call or we&#8217;ll just call H.</p>



<p class="wp-block-paragraph">So it calls itself sometimes.</p>



<p class="wp-block-paragraph">Otherwise, we will have it to call on the I function.</p>



<p class="wp-block-paragraph">then you know execution just kind of returns to the caller which would be G in</p>



<p class="wp-block-paragraph">this case and then we have the I function that just you know does whatever</p>



<p class="wp-block-paragraph">right so the code here doesn&#8217;t really matter what I&#8217;m really trying to show</p>



<p class="wp-block-paragraph">you is that one function calls another calls another and we can sort of allow</p>



<p class="wp-block-paragraph">this to happen under the hood with a call stack so I&#8217;m just gonna draw this</p>



<p class="wp-block-paragraph">and G calls on H and H we&#8217;ll say that we&#8217;ll say for the sake of argument one time H calls on H</p>



<p class="wp-block-paragraph">just so it&#8217;s easier to draw later and then H eventually calls I for some other reason</p>



<p class="wp-block-paragraph">maybe the deeper H calls I and then eventually this starts to return right so if you&#8217;re a</p>



<p class="wp-block-paragraph">programmer you kind of take this situation for for granted you think like well I just kind of</p>



<p class="wp-block-paragraph">call a function and then and then when the function&#8217;s done I just return to the place</p>



<p class="wp-block-paragraph">everything is fine. If we had, let&#8217;s say, local variables in here, they would be preserved. Let&#8217;s</p>



<p class="wp-block-paragraph">say, I don&#8217;t know, maybe h is like a number or something like that. Or how about let&#8217;s do g as</p>



<p class="wp-block-paragraph">a number. We&#8217;ll say g returns some kind of a number. And then maybe in the f function, when</p>



<p class="wp-block-paragraph">we call g, we&#8217;ll just print whatever g returned. So g calls h, and then we&#8217;ll just say like return</p>



<p class="wp-block-paragraph">h and then we&#8217;ll just say like return whatever just trying to show you that we might have</p>



<p class="wp-block-paragraph">a local variable so that means um in the g function maybe you&#8217;ll have a integer a is equal</p>



<p class="wp-block-paragraph">to five and integer b is equal to six i&#8217;m just making up stuff here and then when we return</p>



<p class="wp-block-paragraph">our data we might say you know return a plus b for whatever reason this is a nonsense program again</p>



<p class="wp-block-paragraph">don&#8217;t take it too seriously but i&#8217;m just trying to show you that we have functions that can call</p>



<p class="wp-block-paragraph">we have functions that can call each other sometimes call themselves and maybe they have</p>



<p class="wp-block-paragraph">local variables on top of that so if you understand what is a stack then you understand that a stack</p>



<p class="wp-block-paragraph">can hold many different types of data just as a quick recap here um you know a regular</p>



<p class="wp-block-paragraph">generic data structure stack or an abstract data type stack we could say that the stack hold held</p>



<p class="wp-block-paragraph">integers so if i wanted to put like an eight on there and then put like a five on there it would</p>



<p class="wp-block-paragraph">it would just kind of stack up as we&#8217;re adding data.</p>



<p class="wp-block-paragraph">And then when eventually we decided to remove data from the stack,</p>



<p class="wp-block-paragraph">we&#8217;d pop it by just removing the thing from the top.</p>



<p class="wp-block-paragraph">But because stacks are supposed to be abstract data types that hold any type of data,</p>



<p class="wp-block-paragraph">we&#8217;re not limited to putting integers inside of the stack.</p>



<p class="wp-block-paragraph">So we could invent a totally new data structure</p>



<p class="wp-block-paragraph">and put instances of that data structure inside of the stack.</p>



<p class="wp-block-paragraph">For example, if you had a custom class that you wrote yourself</p>



<p class="wp-block-paragraph">and you just called it my class,</p>



<p class="wp-block-paragraph">my class let&#8217;s see uh let me do class my class and i&#8217;ll just put braces here again this is not</p>



<p class="wp-block-paragraph">a c++ video but you know you can imagine that you sort of made a blueprint for a new type of object</p>



<p class="wp-block-paragraph">and you called it my class and then later on you&#8217;re like let&#8217;s make instances of my class</p>



<p class="wp-block-paragraph">let&#8217;s make an instance called mc1 and mc2 and mc3 and so forth right so in the stack you could just</p>



<p class="wp-block-paragraph">instances of my class i&#8217;m just going to put mc1 here if we created mc1 and then pushed it onto</p>



<p class="wp-block-paragraph">the stack and then we can have mc2 and created an instance of that and just kind of pushed it</p>



<p class="wp-block-paragraph">on the stack so we can do whatever we want we can put any type of data on the stack m</p>



<p class="wp-block-paragraph">and this is not exactly the process stack so far i&#8217;m just telling you what a stack</p>



<p class="wp-block-paragraph">in general can do so now instead of imagining that we have a custom class called my class</p>



<p class="wp-block-paragraph">made a new class called call frame I&#8217;ll say we have like an instance let&#8217;s say</p>



<p class="wp-block-paragraph">like a blueprint for a class we&#8217;ll call it a call frame I&#8217;m a penmanship it&#8217;s</p>



<p class="wp-block-paragraph">because this thing this pen and I push it down and it always like keeps drawing</p>



<p class="wp-block-paragraph">after I start lifting it up I don&#8217;t know what I&#8217;m doing wrong I&#8217;ve been trying</p>



<p class="wp-block-paragraph">this for like 50 videos and it&#8217;s always bad and wrong so imagine you have a</p>



<p class="wp-block-paragraph">class called call frame and inside of it you can just put some sort of data in</p>



<p class="wp-block-paragraph">some sort of data inside the call frame.</p>



<p class="wp-block-paragraph">You can imagine defining what an object might look like and then creating instances and</p>



<p class="wp-block-paragraph">filling it up with data and then every instance just like we did with my class we could just</p>



<p class="wp-block-paragraph">put that onto a stack right?</p>



<p class="wp-block-paragraph">So now let&#8217;s try to understand what might be inside a data structure called a call frame</p>



<p class="wp-block-paragraph">because when we have a stack under the hood in our machine that holds instances of call</p>



<p class="wp-block-paragraph">frames then we actually have a process stack or a call stack which allows us to implement</p>



<p class="wp-block-paragraph">which allows us to implement these function calls.</p>



<p class="wp-block-paragraph">So let&#8217;s see, the first thing that we might imagine,</p>



<p class="wp-block-paragraph">I&#8217;m gonna maybe just do like a little box here,</p>



<p class="wp-block-paragraph">just to let you know that we&#8217;re deciding</p>



<p class="wp-block-paragraph">what will go inside of a call frame.</p>



<p class="wp-block-paragraph">So this is one call frame.</p>



<p class="wp-block-paragraph">So you can imagine that the F function here,</p>



<p class="wp-block-paragraph">in order for the F function to exist,</p>



<p class="wp-block-paragraph">the particular call to exist,</p>



<p class="wp-block-paragraph">what you know what it has what it is to be alive you know it needs its return</p>



<p class="wp-block-paragraph">address I&#8217;m gonna put RA here for its return address what is a return address</p>



<p class="wp-block-paragraph">well you know when we were inside of the main function let me draw the execution</p>



<p class="wp-block-paragraph">path real fast when we were inside of the main function then execution was</p>



<p class="wp-block-paragraph">going from top to bottom when we hit this F function call execution actually</p>



<p class="wp-block-paragraph">jumped to the first line of the F function under the hood in assembly</p>



<p class="wp-block-paragraph">there&#8217;s a jump instruction that allows you to just kind of jump to a totally</p>



<p class="wp-block-paragraph">just kind of jump to a totally different memory location and start executing</p>



<p class="wp-block-paragraph">instructions over there somewhere. In assembly also we have a call</p>



<p class="wp-block-paragraph">instruction which means I would like to jump to another location but also I want</p>



<p class="wp-block-paragraph">to remember where I just came from so that it&#8217;s easy for me to return later.</p>



<p class="wp-block-paragraph">Okay so we&#8217;ll call that the return address. The return address in this case</p>



<p class="wp-block-paragraph">would be let&#8217;s just call it line 4 meaning when function f returns then the</p>



<p class="wp-block-paragraph">is this return zero in well I mean this is C++ we don&#8217;t it&#8217;s not really instructions it&#8217;s more</p>



<p class="wp-block-paragraph">like statements but you can just imagine that F gets called and when it returns then the next</p>



<p class="wp-block-paragraph">statement is return zero right so if we say that the memory location of wherever line four starts</p>



<p class="wp-block-paragraph">is the return address of F then you can imagine that in order for F&#8217;s you know call instance to</p>



<p class="wp-block-paragraph">exist it has to remember its return address so therefore we would put the return address inside</p>



<p class="wp-block-paragraph">call frame. So the return address goes in there and function arguments sometimes go in there. If</p>



<p class="wp-block-paragraph">you know assembly, and that&#8217;s kind of what we&#8217;re talking about right now in this video, then</p>



<p class="wp-block-paragraph">you&#8217;ll know that function arguments, at least the integer arguments usually come in certain</p>



<p class="wp-block-paragraph">registers like RDI and RSI, or if you&#8217;re, whoops, or if you&#8217;re passing float arguments,</p>



<p class="wp-block-paragraph">and XMM1 and so forth right so you just kind of like load up registers with</p>



<p class="wp-block-paragraph">arguments but if you have more arguments than you have registers you know</p>



<p class="wp-block-paragraph">there&#8217;s only so many registers you can actually use to pass arguments into a</p>



<p class="wp-block-paragraph">function that you&#8217;re calling so what if you run out of float registers you run</p>



<p class="wp-block-paragraph">out of the general purpose registers but you want to have more arguments than</p>



<p class="wp-block-paragraph">that for example if you look at the ABI then the integer arguments there&#8217;s only</p>



<p class="wp-block-paragraph">integer arguments there&#8217;s only six registers that we can use to pass integer or pointer arguments</p>



<p class="wp-block-paragraph">so if you wanted to have seven arguments or more then you know you should be able to do that but</p>



<p class="wp-block-paragraph">there has to be a different way to accomplish that so the seventh and beyond arguments will</p>



<p class="wp-block-paragraph">actually uh show up in the call frame oh I guess ra should probably be blue because the the call</p>



<p class="wp-block-paragraph">frame I&#8217;m just you know making that green I&#8217;ll say return address so the seventh and uh the</p>



<p class="wp-block-paragraph">the seventh and beyond arguments args we&#8217;ll just say so the call frame contains the return address</p>



<p class="wp-block-paragraph">and any argument that&#8217;s that&#8217;s like the seventh argument and beyond and if you don&#8217;t have seven</p>



<p class="wp-block-paragraph">arguments then there&#8217;s not going to be that&#8217;s not going to be in the in the call frame it&#8217;ll just be</p>



<p class="wp-block-paragraph">blank at that point sometimes when people are messing around with the stack pointer they will</p>



<p class="wp-block-paragraph">Basically, anytime you push a register in order to preserve it,</p>



<p class="wp-block-paragraph">then it will end up on the call frame, at least at the assembly level.</p>



<p class="wp-block-paragraph">So suppose you&#8217;re going to use the base pointer register.</p>



<p class="wp-block-paragraph">You probably want to preserve the base pointer before you start using it.</p>



<p class="wp-block-paragraph">So we would push it onto the stack and it would become part of the call frame.</p>



<p class="wp-block-paragraph">Any registers that are designated as callee saved</p>



<p class="wp-block-paragraph">would also be pushed into the call frame like R12.</p>



<p class="wp-block-paragraph">Oh my God.</p>



<p class="wp-block-paragraph">R13.</p>



<p class="wp-block-paragraph">my penmanship there&#8217;s like it&#8217;s the ones the ones are always l&#8217;s okay and so forth right so</p>



<p class="wp-block-paragraph">anything that&#8217;s designated as a callee saved register that you intend to use would end up</p>



<p class="wp-block-paragraph">getting pushed onto the stack at the assembly level and thus it would end up in the call frame</p>



<p class="wp-block-paragraph">but you know if you aren&#8217;t going to use any callee preserved registers then you don&#8217;t need to</p>



<p class="wp-block-paragraph">preserve them and therefore they would not show up in the call frame and let&#8217;s see and local</p>



<p class="wp-block-paragraph">local variables in assembly, you just basically end up pushing them to the stack.</p>



<p class="wp-block-paragraph">So I&#8217;m going to put local variables here.</p>



<p class="wp-block-paragraph">And in a second, I&#8217;ll show you how that kind of works in higher level languages like on</p>



<p class="wp-block-paragraph">C++.</p>



<p class="wp-block-paragraph">That&#8217;s why I wrote this down right here, line 12.</p>



<p class="wp-block-paragraph">We&#8217;re making local variables.</p>



<p class="wp-block-paragraph">And let&#8217;s see what else.</p>



<p class="wp-block-paragraph">I should emphasize that local variables are very different from heap variables.</p>



<p class="wp-block-paragraph">some point let&#8217;s say that when when g was called let&#8217;s say execution came in here</p>



<p class="wp-block-paragraph">into main and then execution jumped into the f function so it jumped in there and then f wanted</p>



<p class="wp-block-paragraph">to call on g in order to get some information right so we have a call frame just for g and we</p>



<p class="wp-block-paragraph">have a call frame just for f and we have a call frame just for main and so we have like three</p>



<p class="wp-block-paragraph">items on our stack at least just to get to that point of the program and every call frame you</p>



<p class="wp-block-paragraph">you know, basically has this sort of information inside of it.</p>



<p class="wp-block-paragraph">The return address, so the function knows where to jump back to</p>



<p class="wp-block-paragraph">when it&#8217;s time to return from the function.</p>



<p class="wp-block-paragraph">Seventh and beyond arguments, if any exist.</p>



<p class="wp-block-paragraph">The base pointer, if you happen to modify that at the assembly level.</p>



<p class="wp-block-paragraph">And then same thing for Kali saved registers.</p>



<p class="wp-block-paragraph">And then local variables, which you could probably imagine more clearly.</p>



<p class="wp-block-paragraph">So now I&#8217;m just going to erase this call frame thing here.</p>



<p class="wp-block-paragraph">Oh God, this eraser needs to be improved.</p>



<p class="wp-block-paragraph">god this eraser needs to be improved oh my gosh maybe i should make that bigger i need like a</p>



<p class="wp-block-paragraph">little shortcut to make it bigger for a second because i don&#8217;t really want to draw that thing</p>



<p class="wp-block-paragraph">on top again that&#8217;s just annoying i don&#8217;t want to do it i won&#8217;t do it i won&#8217;t do it okay so uh</p>



<p class="wp-block-paragraph">imagine now that we are trying to sort of trace what&#8217;s happening in our uh call graph you know</p>



<p class="wp-block-paragraph">in our in our program while we&#8217;re calling functions so first imagine we have an empty</p>



<p class="wp-block-paragraph">have an empty stack and again if you don&#8217;t understand stacks you better go see my other video</p>



<p class="wp-block-paragraph">imagine we have an empty stack the stack is not ever really going to be empty when you launch</p>



<p class="wp-block-paragraph">your program it&#8217;s going to be uh there&#8217;s going to be stuff on it already but let&#8217;s just pretend that</p>



<p class="wp-block-paragraph">we haven&#8217;t put anything on the stack at this point so there would be a you know a call frame just for</p>



<p class="wp-block-paragraph">the main function i&#8217;m just going to write main here to say that this is the call frame for main</p>



<p class="wp-block-paragraph">so it contains all the information that main needs and um if you have a main function in the first</p>



<p class="wp-block-paragraph">first place then you&#8217;re probably linking against the GCC libraries which means</p>



<p class="wp-block-paragraph">a main definitely has to return somewhere inside of the GCC library so</p>



<p class="wp-block-paragraph">this kind of makes sense for our program so it&#8217;s a call frame for main</p>



<p class="wp-block-paragraph">and then when main calls on F then another call frame gets created for F</p>



<p class="wp-block-paragraph">and then when F calls on G another call frame gets created for G I don&#8217;t know if</p>



<p class="wp-block-paragraph">I need to put these parentheses but I guess I&#8217;ve already started to do that</p>



<p class="wp-block-paragraph">h and then h sometimes calls itself so I&#8217;m gonna put another call frame for h</p>



<p class="wp-block-paragraph">up here oh my god h&#8217;s look like just crazy okay that&#8217;s been uh every time I</p>



<p class="wp-block-paragraph">lift up the pen h calls itself sometimes we&#8217;ll just say that it calls itself</p>



<p class="wp-block-paragraph">once in this case and then h calls i and then that&#8217;s let&#8217;s just say that&#8217;s the</p>



<p class="wp-block-paragraph">end of our little call graph when i is finished doing whatever it&#8217;s gonna do</p>



<p class="wp-block-paragraph">to do then it&#8217;ll just return to the caller right so notice how every single time we made a function</p>



<p class="wp-block-paragraph">call we added a call frame for that particular instance of a call and we called it a call frame</p>



<p class="wp-block-paragraph">something else that i should point out is that i have been describing to you a call frame as if it&#8217;s</p>



<p class="wp-block-paragraph">a a data structure with sort of like a fixed amount of data inside of it and you would sort of</p>



<p class="wp-block-paragraph">blob that whole block of data on at the same time that&#8217;s not really how it works in assembly in</p>



<p class="wp-block-paragraph">you kind of just push, you know, one tiny piece of data at a time.</p>



<p class="wp-block-paragraph">But in the abstract, you imagine that there&#8217;s a call frame.</p>



<p class="wp-block-paragraph">Just keep that in mind.</p>



<p class="wp-block-paragraph">Anyway, so in the F function, you know, we&#8217;ve been making these calls F and then G and then H and then H and then I.</p>



<p class="wp-block-paragraph">Eventually, when we got to the, let&#8217;s say, what did I do wrong?</p>



<p class="wp-block-paragraph">Oh, I did something wrong.</p>



<p class="wp-block-paragraph">I put the local variables in the wrong function.</p>



<p class="wp-block-paragraph">Hang on a second.</p>



<p class="wp-block-paragraph">I want them to be, I mean, it would be okay to have them inside of the G function, but I really want them.</p>



<p class="wp-block-paragraph">but I really want them, I really want them to be inside the H function for a certain reason.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to cut and paste here.</p>



<p class="wp-block-paragraph">And I&#8217;m going to do like local variables inside of the H function and not in the G function.</p>



<p class="wp-block-paragraph">And then G should maybe return an integer result.</p>



<p class="wp-block-paragraph">And then this return statement shouldn&#8217;t even be in G.</p>



<p class="wp-block-paragraph">It&#8217;s just going to be inside of H.</p>



<p class="wp-block-paragraph">the a and the b variables at some point before returning them modify a and b somehow maybe I can</p>



<p class="wp-block-paragraph">even put that comment inside of both of the blocks of the if so like so maybe if something happens</p>



<p class="wp-block-paragraph">we&#8217;ll modify them in one way and if it doesn&#8217;t happen we&#8217;ll modify them in some other way</p>



<p class="wp-block-paragraph">and then at the very end we return them just to kind of feel like we have more of an excuse</p>



<p class="wp-block-paragraph">to have return variables in the first place and then when h returns something now we have an excuse to kind of print out whatever h is</p>



<p class="wp-block-paragraph">doing okay and the reason I moved all that inside of h and not in g is because I just want you to know that</p>



<p class="wp-block-paragraph">There are two instances of the h, you know call instance on the call stack which means the local variables</p>



<p class="wp-block-paragraph">They&#8217;re not linked. They&#8217;re two distinct copies of each local variable inside of the stack think of it this way</p>



<p class="wp-block-paragraph">When we call our h the first time,</p>



<p class="wp-block-paragraph">we have an integer a and an integer b.</p>



<p class="wp-block-paragraph">You could imagine that there&#8217;s an integer a</p>



<p class="wp-block-paragraph">and an integer b sitting inside of the call frame somewhere,</p>



<p class="wp-block-paragraph">you know, being pushed onto the call stack.</p>



<p class="wp-block-paragraph">And, but the other called h also has a variable a</p>



<p class="wp-block-paragraph">and a variable b,</p>



<p class="wp-block-paragraph">but because those are local variables,</p>



<p class="wp-block-paragraph">changing one doesn&#8217;t actually modify the other copy</p>



<p class="wp-block-paragraph">that is in the other call frame.</p>



<p class="wp-block-paragraph">So you could imagine this as A sub one and B sub one,</p>



<p class="wp-block-paragraph">and then A sub two and B sub two.</p>



<p class="wp-block-paragraph">This is kind of important later on when you realize</p>



<p class="wp-block-paragraph">that your operating system is capable of launching multiple threads for your program</p>



<p class="wp-block-paragraph">and local variables won&#8217;t potentially become race conditions,</p>



<p class="wp-block-paragraph">which is just like for a totally different video.</p>



<p class="wp-block-paragraph">But for now, just to understand these are totally separate instances.</p>



<p class="wp-block-paragraph">If I change A sub two, it&#8217;s not changing A sub one and vice versa.</p>



<p class="wp-block-paragraph">And same thing for B.</p>



<p class="wp-block-paragraph">vice versa and same thing for B so anyway we we make all these calls and</p>



<p class="wp-block-paragraph">somewhere along the way you know we&#8217;re creating a bunch of data and a bunch of</p>



<p class="wp-block-paragraph">call frames eventually when we are done with the I function when I wants to</p>



<p class="wp-block-paragraph">return here we don&#8217;t have an explicit return statement but if you if you know</p>



<p class="wp-block-paragraph">C++ or higher level languages if there&#8217;s no return statement in most of</p>



<p class="wp-block-paragraph">the language in most of these languages for a void function it just returns when</p>



<p class="wp-block-paragraph">when it reaches the end of the function we don&#8217;t really need to say return because it doesn&#8217;t</p>



<p class="wp-block-paragraph">return anything it just ends so when i is ready to return what happens is we look inside of its</p>



<p class="wp-block-paragraph">call frame and we look for the return address which is inside of its call frame the return</p>



<p class="wp-block-paragraph">address specifies exactly where we&#8217;re supposed to return for example maybe we&#8217;re supposed to return</p>



<p class="wp-block-paragraph">right here maybe we&#8217;re supposed to return right here maybe to make things a little bit more</p>



<p class="wp-block-paragraph">so maybe if we called h then we increased a and if not then we increased b something like that</p>



<p class="wp-block-paragraph">so that means the the call frame for the first h function let&#8217;s suppose let&#8217;s see if it called</p>



<p class="wp-block-paragraph">if it called itself then that means this block right here is the first part of the if block so</p>



<p class="wp-block-paragraph">the return address for h return address would actually be pointing to</p>



<p class="wp-block-paragraph">would actually be pointing to the next line right so when h called on h</p>



<p class="wp-block-paragraph">it would look at line 21 or whatever the address was for that instruction</p>



<p class="wp-block-paragraph">to begin at you know increasing a and it would just say that&#8217;s the return address so that</p>



<p class="wp-block-paragraph">whenever h returns from itself it ends up executing a plus plus and then the same thing for i so</p>



<p class="wp-block-paragraph">i has a return address so when we&#8217;re inside of the i function it&#8217;s got its own call frame and it</p>



<p class="wp-block-paragraph">knows where it&#8217;s supposed to return which in this case would be that instruction right there or that</p>



<p class="wp-block-paragraph">statement right there so whatever is the first instruction that implements that higher level</p>



<p class="wp-block-paragraph">language statement that&#8217;s going to be the return address inside the the i call frame so we look</p>



<p class="wp-block-paragraph">inside of the call frame for the return address we know where to return it&#8217;s going to return to h at</p>



<p class="wp-block-paragraph">at this point in time.</p>



<p class="wp-block-paragraph">And once we&#8217;re done with that,</p>



<p class="wp-block-paragraph">then we just deallocate the call frame.</p>



<p class="wp-block-paragraph">In assembly language,</p>



<p class="wp-block-paragraph">all we&#8217;re really doing is moving the stack pointer</p>



<p class="wp-block-paragraph">and just sort of ignoring the call frame</p>



<p class="wp-block-paragraph">from that point forward and we&#8217;ll call it junk data.</p>



<p class="wp-block-paragraph">So the data is kind of actually still there,</p>



<p class="wp-block-paragraph">but we&#8217;re just not using it.</p>



<p class="wp-block-paragraph">And then later, if we wanna start adding more stuff</p>



<p class="wp-block-paragraph">on top of the stack,</p>



<p class="wp-block-paragraph">then it will just override that junk data.</p>



<p class="wp-block-paragraph">But for now, just imagine that it&#8217;s gone.</p>



<p class="wp-block-paragraph">Anyway, so then we returned from I back to H</p>



<p class="wp-block-paragraph">from I back to H and so that&#8217;s line 26 right here imagine we perform this</p>



<p class="wp-block-paragraph">instruction the B++ and then execution falls through down to the very end the</p>



<p class="wp-block-paragraph">return statement so then same thing happens this version or I guess this</p>



<p class="wp-block-paragraph">version of H had its own no hang on a second we returned from I yeah yeah this</p>



<p class="wp-block-paragraph">to h so that means execution will will basically jump from here to right here so it kind of jumps</p>



<p class="wp-block-paragraph">you know back to that next line like we talked about before when it returns based off the return</p>



<p class="wp-block-paragraph">address then again you can imagine that that call frame is deallocated or just ignored as junk data</p>



<p class="wp-block-paragraph">and the top of the stack is kind of like moving and then when that call on h uh finishes so you</p>



<p class="wp-block-paragraph">executed and then execution falls through down to the return statement then again h has its own</p>



<p class="wp-block-paragraph">return address in its in its call frame or that instance of h has its own call frame then it knows</p>



<p class="wp-block-paragraph">where to return and this time it knows to return to whoever called h in the very first place so</p>



<p class="wp-block-paragraph">it&#8217;s going to be um i guess it&#8217;s going to be like when it returns it&#8217;s going to go back to g</p>



<p class="wp-block-paragraph">h on g but I guess you could imagine under the hood you know instead of like</p>



<p class="wp-block-paragraph">another statement you can imagine more stuff is happening right like when we</p>



<p class="wp-block-paragraph">called h and then we returned then we had an expression that evaluated to</p>



<p class="wp-block-paragraph">something because h returned to value and then we would send that to the stream</p>



<p class="wp-block-paragraph">operator so in assembly there are many instructions happening after the call to</p>



<p class="wp-block-paragraph">h but you could just imagine at this point you know it&#8217;s just gonna finish up</p>



<p class="wp-block-paragraph">that that existing statement and then move down to finish and return so then</p>



<p class="wp-block-paragraph">so then again you know we we just returned from h to g so we de-allocated that call frame the top</p>



<p class="wp-block-paragraph">of the stack is now somewhere around there in that call frame when g finishes and it returns</p>



<p class="wp-block-paragraph">same thing it&#8217;s got a return address so it knows where to return and then top of the stack kind of</p>



<p class="wp-block-paragraph">goes down there and g knows to jump to whoever called it because of the return address so it&#8217;s</p>



<p class="wp-block-paragraph">going to be at this point in time again there&#8217;s more statements that happen after the return</p>



<p class="wp-block-paragraph">see out in a stream operator uh you know but uh you know basically when f is done then again it&#8217;s</p>



<p class="wp-block-paragraph">going to use the return address and know where to jump back to which is just going to be this next</p>



<p class="wp-block-paragraph">line right here so when f was called originally it like ended up calling h and then h uh you know</p>



<p class="wp-block-paragraph">called itself and h called itself and then it called i and then we returned from i and then we</p>



<p class="wp-block-paragraph">returned from h and returned from h twice and then eventually we returned to g and then we returned</p>



<p class="wp-block-paragraph">and all of its calls were finished then execution returned just a line for and</p>



<p class="wp-block-paragraph">that&#8217;s that&#8217;s basically saying like oh what happens when F is done let&#8217;s look</p>



<p class="wp-block-paragraph">at its return address hop that off the call stack and then the top of the stack</p>



<p class="wp-block-paragraph">is basically just sitting inside of the main function so yeah this is how we</p>



<p class="wp-block-paragraph">implement function calls in the machine you know there&#8217;s there&#8217;s really no so if</p>



<p class="wp-block-paragraph">you know, from a certain point of view, there&#8217;s no such thing as functions inside the machine.</p>



<p class="wp-block-paragraph">In assembly language, we certainly see that we have a label and then we have like a return</p>



<p class="wp-block-paragraph">instruction and we have a call instruction, but you know, there&#8217;s no like function. It&#8217;s all just</p>



<p class="wp-block-paragraph">ones and zeros all over the place, right? So the idea of labels and functions and things,</p>



<p class="wp-block-paragraph">that&#8217;s kind of an abstraction just for the human being with feelings to make it a little bit easier</p>



<p class="wp-block-paragraph">stack we have a stack and we stack things on top of the stack and the things that we stack are</p>



<p class="wp-block-paragraph">called call frames call frames are kind of flexible because sometimes there&#8217;s more data</p>



<p class="wp-block-paragraph">sometimes there&#8217;s less data depending on what the function itself is doing but it allows us to have</p>



<p class="wp-block-paragraph">a function call itself many times it allows us to have many different instances of function calls</p>



<p class="wp-block-paragraph">exist at the same time even if we&#8217;re only looking at the very top of the call stack in terms of</p>



<p class="wp-block-paragraph">conflict with each other.</p>



<p class="wp-block-paragraph">And then, you know, like we have a trail of breadcrumbs when we start returning and</p>



<p class="wp-block-paragraph">returning and returning, the stack makes it really, really easy.</p>



<p class="wp-block-paragraph">And so, you know, in the abstract, we can basically say that there are functions in</p>



<p class="wp-block-paragraph">our machines because this implementation helps make it happen.</p>



<p class="wp-block-paragraph">One last thing that I wanted to say before I go here is just keep in mind that these</p>



<p class="wp-block-paragraph">local variables, they&#8217;re on the stack, but dynamically allocated memory is not on the</p>



<p class="wp-block-paragraph">stack.</p>



<p class="wp-block-paragraph">stack. Let me just show you what I&#8217;m talking about right here. You probably understand the</p>



<p class="wp-block-paragraph">heap already at this point. If you&#8217;re watching this video, then you know a little bit about</p>



<p class="wp-block-paragraph">dynamic memory allocation. So I&#8217;m just going to do, let&#8217;s say we have like an F function void F.</p>



<p class="wp-block-paragraph">And inside of the F function, we have let&#8217;s say a integer pointer P, and we will allocate P to a new</p>



<p class="wp-block-paragraph">So just so you know, the variables A and B and also P, they&#8217;re considered local variables</p>



<p class="wp-block-paragraph">because I just declared them.</p>



<p class="wp-block-paragraph">I declared a pointer on line four.</p>



<p class="wp-block-paragraph">I declared two integers on line seven.</p>



<p class="wp-block-paragraph">Maybe I should put those like at the top, I guess.</p>



<p class="wp-block-paragraph">Okay, so lines four and five, they declare local variables.</p>



<p class="wp-block-paragraph">The pointer is a special type of variable, right?</p>



<p class="wp-block-paragraph">That just points to a memory location.</p>



<p class="wp-block-paragraph">So the thing that it points to might not be a local variable, but the pointer itself is</p>



<p class="wp-block-paragraph">a local variable.</p>



<p class="wp-block-paragraph">The pointer itself is a local variable.</p>



<p class="wp-block-paragraph">So these three variables on lines four and five, A and B and P, those are on the stack.</p>



<p class="wp-block-paragraph">Any local variable gets put on the stack.</p>



<p class="wp-block-paragraph">And then when you allocate a new integer with dynamic memory, maybe you could imagine if</p>



<p class="wp-block-paragraph">you&#8217;re a C programmer, this would be a call to malloc rather than new.</p>



<p class="wp-block-paragraph">Then what&#8217;s happening under the hood is that the system goes and tries to find some free</p>



<p class="wp-block-paragraph">memory in a different area of your process called the heap, not the stack.</p>



<p class="wp-block-paragraph">heap not the stack it tries to find some free memory in the heap it once it finds some then it</p>



<p class="wp-block-paragraph">designates that as you know being in use and then it returns to you the memory location of that new</p>



<p class="wp-block-paragraph">allocation so in this case we&#8217;re allocating a new integer so that&#8217;s going to be 32 bits of data or</p>



<p class="wp-block-paragraph">four bytes and uh so it just you know it just finds four bytes somewhere in the heap and it finds the</p>



<p class="wp-block-paragraph">memory location of the very first byte and it returns that to the caller and so p its value is</p>



<p class="wp-block-paragraph">is now going to be the memory location of the new integer that you allocated.</p>



<p class="wp-block-paragraph">But P itself is on the stack.</p>



<p class="wp-block-paragraph">So P is kind of weird, right?</p>



<p class="wp-block-paragraph">Like P exists on the stack, but it points to something in the heap.</p>



<p class="wp-block-paragraph">Keep in mind the difference.</p>



<p class="wp-block-paragraph">And then if we just let this function go to the very end</p>



<p class="wp-block-paragraph">without cleaning up that memory, we would have a memory leak.</p>



<p class="wp-block-paragraph">I should probably just do like a dedicated video on pointers</p>



<p class="wp-block-paragraph">for people who are interested in pointers in higher level languages later.</p>



<p class="wp-block-paragraph">But keep in mind that when the function ends</p>



<p class="wp-block-paragraph">and you pop the call frame off the call stack,</p>



<p class="wp-block-paragraph">then that automatically cleans up all the local variables.</p>



<p class="wp-block-paragraph">So A and B and P itself will be deallocated</p>



<p class="wp-block-paragraph">and not become memory leaks.</p>



<p class="wp-block-paragraph">However, the thing that P pointed to,</p>



<p class="wp-block-paragraph">that will be a memory leak.</p>



<p class="wp-block-paragraph">I guess that&#8217;s a topic that&#8217;s too advanced</p>



<p class="wp-block-paragraph">for this stack video,</p>



<p class="wp-block-paragraph">but I just want you to know the difference</p>



<p class="wp-block-paragraph">between something that exists on the stack</p>



<p class="wp-block-paragraph">and something that exists on the heap.</p>



<p class="wp-block-paragraph">Okay, so then I think the last thing</p>



<p class="wp-block-paragraph">that I need to show you here</p>



<p class="wp-block-paragraph">here is uh let me uh shoot let&#8217;s see shambler okay so here&#8217;s uh uh my favorite book i love to</p>



<p class="wp-block-paragraph">i love to pump up this book it&#8217;s awesome the author is like a brilliant man very kind man</p>



<p class="wp-block-paragraph">he offers this book for free it&#8217;s got a copy left license you can literally just download it for</p>



<p class="wp-block-paragraph">and it&#8217;s legal because this is an open book basically it&#8217;s called x86 64 assembly language</p>



<p class="wp-block-paragraph">programming with ubuntu by the brilliant dr ed jorgensen phd or professor ed jorgensen</p>



<p class="wp-block-paragraph">this is the latest version that i&#8217;m working with right now i think it&#8217;s the latest version i don&#8217;t</p>



<p class="wp-block-paragraph">know he updates this thing all the time it&#8217;s always getting better but um if we look at this</p>



<p class="wp-block-paragraph">book uh you can get a copy of it on your own or you can just watch this video i&#8217;m going to go to</p>



<p class="wp-block-paragraph">12.8.3 so 12 section 12 12.8 12.8.3 called the call frame and here you can kind of see a</p>



<p class="wp-block-paragraph">description of some of the stuff that I talked about in terms of what sort of data you would</p>



<p class="wp-block-paragraph">expect to see in the call frame but if you just kind of look down a little bit you can see that</p>



<p class="wp-block-paragraph">we are sort of pushing some preserved registers</p>



<p class="wp-block-paragraph">sort of like save the stack pointer as the RBP register.</p>



<p class="wp-block-paragraph">But before we do that, we wanna push the RBP register.</p>



<p class="wp-block-paragraph">And then notice out here,</p>



<p class="wp-block-paragraph">it shows that there&#8217;s a return address</p>



<p class="wp-block-paragraph">and the RIP register.</p>



<p class="wp-block-paragraph">That&#8217;s actually the instruction pointer register.</p>



<p class="wp-block-paragraph">The stack pointer is RSP.</p>



<p class="wp-block-paragraph">And then other arguments that go into it.</p>



<p class="wp-block-paragraph">So just keep in mind that we have a little layout here</p>



<p class="wp-block-paragraph">a little layout here and then somewhere else there was something I wanted to show you here</p>



<p class="wp-block-paragraph">no no no no no no no no no no no no no no no no no no okay I guess I&#8217;m gonna</p>



<p class="wp-block-paragraph">oh I guess I was just supposed to show you the call frame anyway okay so I guess that&#8217;s pretty</p>



<p class="wp-block-paragraph">much it I just wanted to show you some extra stuff just so you understand this is the basic</p>



<p class="wp-block-paragraph">Oh, okay, I found it.</p>



<p class="wp-block-paragraph">I spent a second scrolling through the book to find the other diagram that I really wanted</p>



<p class="wp-block-paragraph">to show you.</p>



<p class="wp-block-paragraph">So same book, X-8664 Assembly with Ubuntu by Ed Jorgensen, Professor Ed Jorgensen, PhD.</p>



<p class="wp-block-paragraph">And if you look at section 9.3, there&#8217;s another picture here that&#8217;s kind of useful.</p>



<p class="wp-block-paragraph">And it basically shows, you know, the general idea of how information is laid out inside</p>



<p class="wp-block-paragraph">information is laid out inside of your process so when you launch a process in</p>



<p class="wp-block-paragraph">order to execute your program then it&#8217;s kind of laid out like this there&#8217;s like</p>



<p class="wp-block-paragraph">a little reserved area and then there is the text section for assembly you know</p>



<p class="wp-block-paragraph">that&#8217;s where you have all your instructions and then there&#8217;s the data</p>



<p class="wp-block-paragraph">section and then there&#8217;s uninitialized data which is the BSS section if we&#8217;re</p>



<p class="wp-block-paragraph">talking about Yasm assembly and then notice how in this big blob of an area</p>



<p class="wp-block-paragraph">and the stack kind of sharing an area inside of available memory it&#8217;s important to understand that</p>



<p class="wp-block-paragraph">the stack when you actually add things onto the process stack like call like call frame data</p>



<p class="wp-block-paragraph">you&#8217;re actually decreasing memory location so the stack grows downward in memory so I think I&#8217;m going</p>



<p class="wp-block-paragraph">to put this in another video but basically right now I just want to show you if we have memory</p>



<p class="wp-block-paragraph">location let me let&#8217;s pretend that we have like a stack here and there&#8217;s like a couple of frames</p>



<p class="wp-block-paragraph">there&#8217;s like a couple of frames you know sitting on top of it suppose you had I don&#8217;t know memory</p>



<p class="wp-block-paragraph">location OX you know 99 or something suppose for the sake of argument we&#8217;re only pushing one byte</p>



<p class="wp-block-paragraph">at a time so it&#8217;s easier for me to write then that means you know the next thing that shows up</p>



<p class="wp-block-paragraph">you know a little higher on the stack you would intuitively think that the memory location is</p>



<p class="wp-block-paragraph">I guess I increased that 9 to a but it actually goes down in memory so I mean</p>



<p class="wp-block-paragraph">look at this diagram right here the stack goes down in memory so every time</p>



<p class="wp-block-paragraph">you push to the stack you&#8217;re pushing to a lower and lower and lower memory</p>



<p class="wp-block-paragraph">location so right here I&#8217;m just gonna write down OX 9 8 and it just keeps</p>



<p class="wp-block-paragraph">going down and down and down let&#8217;s see 1 2 3 4 5 right and the heap is is</p>



<p class="wp-block-paragraph">upward in memory so the heap when you call new allocations like if you say new or malloc or you</p>



<p class="wp-block-paragraph">know some kind of dynamic memory then its memory locations grow up and the stack grows down and if</p>



<p class="wp-block-paragraph">they ever actually meet then you&#8217;ve ran out of memory just keep that in mind but um you know</p>



<p class="wp-block-paragraph">in modern systems the heap can kind of grow almost endlessly i&#8217;m sure i&#8217;m sure you&#8217;ve all</p>



<p class="wp-block-paragraph">been running programs in the past and noticed that the program was consuming gigabytes of data right</p>



<p class="wp-block-paragraph">gigabytes of data right but it&#8217;s not like you had gigabytes of data allocated to that process</p>



<p class="wp-block-paragraph">when you first opened it up like if you have a browser with like 100 million tabs open</p>



<p class="wp-block-paragraph">i know some of you people do that then when you first open your browser it doesn&#8217;t really use</p>



<p class="wp-block-paragraph">that much memory but then as you start opening more tabs maybe some pages are doing some weird</p>



<p class="wp-block-paragraph">stuff and you get memory leaks then the heap starts to grow and grow and grow and then you&#8217;ll</p>



<p class="wp-block-paragraph">notice the process takes way more than did in the beginning so the heap can kind of grow in an</p>



<p class="wp-block-paragraph">of course that is limited to your physical system ram and eventually in the far off future will be</p>



<p class="wp-block-paragraph">limited by whatever 64 bits can get us i don&#8217;t think we&#8217;re ever going to reach that in my lifetime</p>



<p class="wp-block-paragraph">but you know i&#8217;ve been wrong many times before however the stack it grows downward in memory</p>



<p class="wp-block-paragraph">and it&#8217;s kind of limited you can actually overflow your stack pretty easily so when the stack grows</p>



<p class="wp-block-paragraph">downward too much then you just you have a stack overflow and the program crashes but when the heap</p>



<p class="wp-block-paragraph">But when the heap grows, then it can just be resized again and again and again.</p>



<p class="wp-block-paragraph">Or that little block can kind of expand again and again and again.</p>



<p class="wp-block-paragraph">If you don&#8217;t believe me, try writing a program with a recursive loop that just goes on forever.</p>



<p class="wp-block-paragraph">So imagine, actually, I don&#8217;t know, don&#8217;t do this on your boss&#8217;s computer because maybe</p>



<p class="wp-block-paragraph">it&#8217;ll crash things.</p>



<p class="wp-block-paragraph">Do it on your own personal computer.</p>



<p class="wp-block-paragraph">like a function f and uh you know you&#8217;ll probably want to call f inside of main or something so i&#8217;ll</p>



<p class="wp-block-paragraph">just say like int main and we&#8217;ll just call f right away and then return zero when you&#8217;re inside of f</p>



<p class="wp-block-paragraph">you just call f right so this is like a horrible program if you ran this very quickly you&#8217;d crash</p>



<p class="wp-block-paragraph">the program because every single time you call f you&#8217;re adding another call frame on top of the</p>



<p class="wp-block-paragraph">stack your call stack is going to just be filled with f call frames and then you know it runs out</p>



<p class="wp-block-paragraph">you know, it runs out of available memory locations to use, and then you&#8217;re just done.</p>



<p class="wp-block-paragraph">It just crashes the whole program.</p>



<p class="wp-block-paragraph">So let&#8217;s see.</p>



<p class="wp-block-paragraph">I think hopefully now you feel like you&#8217;re an expert on the process stack and call frames.</p>



<p class="wp-block-paragraph">Thank you for watching this video.</p>



<p class="wp-block-paragraph">And I hope you learned a little bit and had some fun.</p>



<p class="wp-block-paragraph">I guess I&#8217;ll see you in the next video.</p>



<p class="wp-block-paragraph">hey everybody thanks for watching this video again from the bottom of my heart I really</p>



<p class="wp-block-paragraph">appreciate it I do hope you did learn something and have some fun if you could do me a please a</p>



<p class="wp-block-paragraph">small little favor could you please subscribe and follow this channel or these videos or whatever it</p>



<p class="wp-block-paragraph">is you do on the current social media website that you&#8217;re looking at right now it would really mean</p>



<p class="wp-block-paragraph">the world to me and it&#8217;ll help make more videos and grow this community so we&#8217;ll be able to do</p>



<p class="wp-block-paragraph">better videos or just I&#8217;ll be able to keep making videos in general so please</p>



<p class="wp-block-paragraph">do do me a kindness and and subscribe you know sometimes I&#8217;m sleeping in the</p>



<p class="wp-block-paragraph">middle of the night and I just wake up because I know somebody subscribed or</p>



<p class="wp-block-paragraph">followed it just wakes me up and I get filled with joy that&#8217;s exactly what</p>



<p class="wp-block-paragraph">happens every single time so you could do it as a nice favor to me or you could</p>



<p class="wp-block-paragraph">you control me if you want to just wake me up in the middle of the night just</p>



<p class="wp-block-paragraph">subscribe and then I&#8217;ll just wake up I promise that&#8217;s what will happen also if</p>



<p class="wp-block-paragraph">if you look at the middle of the screen right now, you should see a QR code, which you can scan</p>



<p class="wp-block-paragraph">in order to go to the website, which I think is also named somewhere at the bottom of this video.</p>



<p class="wp-block-paragraph">And it&#8217;ll take you to my main website where you can just kind of like see</p>



<p class="wp-block-paragraph">all the videos I published and the services and tutorials and things that I offer and all that</p>



<p class="wp-block-paragraph">good stuff. And if you have a suggestion for clarifications or errata or just future videos</p>



<p class="wp-block-paragraph">Or if you just want to say, hey, what&#8217;s up? What&#8217;s going on? You know, just send me a comment, whatever.</p>



<p class="wp-block-paragraph">I also wake up for those in the middle of the night. I get, I wake up in a cold sweat and I&#8217;m like,</p>



<p class="wp-block-paragraph">it would really, it really mean the world to me. I would really appreciate it. So again,</p>



<p class="wp-block-paragraph">thank you so much for watching this video and enjoy the cool music as, as I fade into the</p>



<p class="wp-block-paragraph">darkness, which is coming for us all.</p>



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/the-call-stack-explained-call-frames-return-addresses-local-variables/">The Call Stack Explained: Call Frames, Return Addresses, Local Variables</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/the-call-stack-explained-call-frames-return-addresses-local-variables/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Master Stacks: Fun Guide to Data Structures, LIFO &#038; Real-World Uses</title>
		<link>https://www.NeuralLantern.com/master-stacks-fun-guide-to-data-structures-lifo-real-world-uses/</link>
					<comments>https://www.NeuralLantern.com/master-stacks-fun-guide-to-data-structures-lifo-real-world-uses/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sun, 20 Jul 2025 09:15:59 +0000</pubDate>
				<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[browser history stack]]></category>
		<category><![CDATA[call stack]]></category>
		<category><![CDATA[coding for beginners]]></category>
		<category><![CDATA[computer science basics]]></category>
		<category><![CDATA[data reversing]]></category>
		<category><![CDATA[data structure basics]]></category>
		<category><![CDATA[data structures tutorial]]></category>
		<category><![CDATA[learn coding]]></category>
		<category><![CDATA[LIFO data structure]]></category>
		<category><![CDATA[programming tutorials]]></category>
		<category><![CDATA[push pop stack]]></category>
		<category><![CDATA[stack data structure]]></category>
		<category><![CDATA[stack in C++]]></category>
		<category><![CDATA[stack operations]]></category>
		<category><![CDATA[stack programming]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=186</guid>

					<description><![CDATA[<p>Learn stacks: fun guide to data structures, LIFO, &#038; uses like browser history. Clear diagrams for beginners!</p>
<p>The post <a href="https://www.NeuralLantern.com/master-stacks-fun-guide-to-data-structures-lifo-real-world-uses/">Master Stacks: Fun Guide to Data Structures, LIFO &amp; Real-World Uses</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="Master Stacks: Fun Guide to Data Structures, LIFO &amp; Real-World Uses" width="1380" height="776" src="https://www.youtube.com/embed/mAqB77KUUKY?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p class="wp-block-paragraph">Want to nail down stacks? This beginner-friendly video breaks down the stack data structure with clear diagrams and a laid-back vibe. We’ll walk you through how stacks work, why they reverse data (hello, LIFO &#8211; Last In, First Out), and how they’re used in real-world stuff like browser history, undo features in editors, and even the call stack in your code. Whether you’re just starting out or brushing up on data structures, we keep it simple with hands-on demos of pushing, popping, and checking stack size—no boring jargon here! Subscribe for more coding tutorials to level up your skills, and let us know in the comments what you want to learn next. Check out our site via the QR code in the video for more coding goodness!</p>



<p class="wp-block-paragraph">Introduction to Stack 00:00:00<br>Explaining Stack Concept 00:00:06<br>Drawing Stack Diagram 00:00:30<br>Adding Elements to Stack 00:00:53<br>Stack Rules and Operations 00:01:16<br>Pop Operation and Interface 00:06:09<br>Stack Size and Empty Check 00:07:59<br>Tracing Stack Operations 00:11:46<br>Stack as Data Reverser 00:17:44<br>LIFO/FILO Explanation 00:21:13<br>Stack Use Cases 00:24:10<br>Call Stack Mention 00:26:18<br>Conclusion and Subscribe Request 00:27:21<br>Outro and Website Promotion 00:28:34</p>



<p class="wp-block-paragraph">Thanks for watching!</p>



<p class="wp-block-paragraph">Find us on other social media here:</p>



<ul class="wp-block-list">
<li>https://www.NeuralLantern.com/social</li>
</ul>



<p class="wp-block-paragraph">Please help support us!</p>



<ul class="wp-block-list">
<li>Subscribing + Sharing on Social Media</li>



<li>Leaving a comment or suggestion</li>



<li>Subscribing to our Blog</li>



<li>Watching the main &#8220;pinned&#8221; video of this channel for offers and extras</li>
</ul>



<p class="wp-block-paragraph">Hello there.</p>



<p class="wp-block-paragraph">In this video, I&#8217;m going to show you a data structure called a stack.</p>



<p class="wp-block-paragraph">We&#8217;re going to make a diagram of it.</p>



<p class="wp-block-paragraph">We&#8217;re going to explain how it works.</p>



<p class="wp-block-paragraph">We&#8217;re going to look at its typical interface,</p>



<p class="wp-block-paragraph">and we&#8217;re just going to kind of work through the concept</p>



<p class="wp-block-paragraph">of what is a stack and what does it do and all that stuff.</p>



<p class="wp-block-paragraph">Where&#8217;s your daddy and what does he do?</p>



<p class="wp-block-paragraph">So for starters, what am I talking about?</p>



<p class="wp-block-paragraph">What am I talking about?</p>



<p class="wp-block-paragraph">Let me open up a little notepad here.</p>



<p class="wp-block-paragraph">And I&#8217;m going to start drawing.</p>



<p class="wp-block-paragraph">A stack is pretty simple when you first think about it.</p>



<p class="wp-block-paragraph">It just takes a little bit more work to really get all the details down.</p>



<p class="wp-block-paragraph">Imagine I have a data structure and maybe like at the bottom here,</p>



<p class="wp-block-paragraph">there&#8217;s like some grass.</p>



<p class="wp-block-paragraph">And so we can say that the data structure is empty when we first start.</p>



<p class="wp-block-paragraph">Okay, so there&#8217;s kind of a stack here, but it&#8217;s empty.</p>



<p class="wp-block-paragraph">So there&#8217;s nothing really on screen.</p>



<p class="wp-block-paragraph">Imagine then I wanted to add integers into this stack.</p>



<p class="wp-block-paragraph">So I&#8217;m going to say let&#8217;s add the integers eight and then two and then three and then four for some reason.</p>



<p class="wp-block-paragraph">Okay, so how do we do this?</p>



<p class="wp-block-paragraph">The first thing we do is we take the eight and we just put it on top of the stack.</p>



<p class="wp-block-paragraph">You can imagine this as a stack of boxes or a stack of books or a stack of whatever.</p>



<p class="wp-block-paragraph">When we add items, we&#8217;re always going to add to the top of the stack.</p>



<p class="wp-block-paragraph">When the stack is empty, we&#8217;re obviously just going to add like at the very bottom, you know, at the ground floor.</p>



<p class="wp-block-paragraph">But that&#8217;s sort of the top.</p>



<p class="wp-block-paragraph">when we add the two here the next thing that happens is we just put the two on top of the eight</p>



<p class="wp-block-paragraph">and the next thing that we do is we uh put the three on top of the two and so forth right so</p>



<p class="wp-block-paragraph">it&#8217;s important to understand a few a few rules about stacks let me add that four real fast</p>



<p class="wp-block-paragraph">in stacks you&#8217;re only allowed to add to the top or remove from the top you&#8217;re not allowed to add</p>



<p class="wp-block-paragraph">or remove from anywhere else let me put a t for top right here so basically you&#8217;re allowed to add</p>



<p class="wp-block-paragraph">So basically you&#8217;re allowed to add and remove from the top.</p>



<p class="wp-block-paragraph">So if I wanted to add, let&#8217;s say a 12, the 12 would have to go on top of the four.</p>



<p class="wp-block-paragraph">It couldn&#8217;t go anywhere else in this data structure.</p>



<p class="wp-block-paragraph">And if I wanted to remove data from this stack, I could only remove the four.</p>



<p class="wp-block-paragraph">I could not remove the three or two or the eight or anything.</p>



<p class="wp-block-paragraph">You&#8217;re also not allowed to look at the middle of the stack.</p>



<p class="wp-block-paragraph">You&#8217;re not allowed to look anywhere but the top.</p>



<p class="wp-block-paragraph">So if I wanted to look at the three, the two or the eight, I wouldn&#8217;t be allowed to do it.</p>



<p class="wp-block-paragraph">I can only look at the four.</p>



<p class="wp-block-paragraph">look at the four and if I want to see that three I have to pop the four off</p>



<p class="wp-block-paragraph">first in order to just see what&#8217;s under it and technically unless you have the</p>



<p class="wp-block-paragraph">data you added memorized somewhere else you probably shouldn&#8217;t know what&#8217;s under</p>



<p class="wp-block-paragraph">the four until you actually remove it okay so we can add and remove from the</p>



<p class="wp-block-paragraph">top we can&#8217;t look in the middle we can&#8217;t look at the bottom or anything like that</p>



<p class="wp-block-paragraph">and let&#8217;s do a little let&#8217;s do a little sequence of pops let me show you what</p>



<p class="wp-block-paragraph">So first off, you can imagine a stack is sort of like a vector or a list with less functionality or more restrictions.</p>



<p class="wp-block-paragraph">It&#8217;s useful to add restrictions on top of more robust data structures for the purposes of, I don&#8217;t know, kindling your imagination or allowing yourself to use easier implementations or whatever.</p>



<p class="wp-block-paragraph">If you&#8217;re a musician or artist, probably at some point you&#8217;ve been stumped and you&#8217;ve had writer&#8217;s block and you can&#8217;t figure out what to create.</p>



<p class="wp-block-paragraph">can&#8217;t figure out what to create so uh you know i know sometimes musicians when they get writer&#8217;s</p>



<p class="wp-block-paragraph">block they&#8217;ll they&#8217;ll go to the swap meet and they&#8217;ll buy like an old dusty dirty uh casio</p>



<p class="wp-block-paragraph">keyboard from 1985 and then they&#8217;ll write a full song just with that one piece of equipment and it</p>



<p class="wp-block-paragraph">really stirs their creativity or it sparks their creativity and then when they&#8217;re done then they&#8217;ll</p>



<p class="wp-block-paragraph">probably upgrade to better equipment but anyway so there&#8217;s there&#8217;s a bunch of different reasons to</p>



<p class="wp-block-paragraph">use you know lesser data structures although this is not really lesser it&#8217;s very useful</p>



<p class="wp-block-paragraph">It&#8217;s very useful in its own right.</p>



<p class="wp-block-paragraph">So let&#8217;s see the interface.</p>



<p class="wp-block-paragraph">So what can we do in a stack?</p>



<p class="wp-block-paragraph">I&#8217;m going to put S for stack.</p>



<p class="wp-block-paragraph">Actually, I&#8217;m just going to type this up as code.</p>



<p class="wp-block-paragraph">No need to use the pen the whole entire time.</p>



<p class="wp-block-paragraph">Let&#8217;s see.</p>



<p class="wp-block-paragraph">So I&#8217;m going to put S for stack.</p>



<p class="wp-block-paragraph">Maybe I&#8217;ll say, you know, stack that holds integers.</p>



<p class="wp-block-paragraph">And I&#8217;ll just put an S here.</p>



<p class="wp-block-paragraph">If you don&#8217;t know how to code right now, at this point in your career, just bear with</p>



<p class="wp-block-paragraph">me.</p>



<p class="wp-block-paragraph">variable of type stack and in C++ these stacks you know stacks are a template of data structure</p>



<p class="wp-block-paragraph">which means the integers that we are holding inside of the stack they could be any other data</p>



<p class="wp-block-paragraph">type that we wanted to including custom classes so in C++ we would use these angle brackets to</p>



<p class="wp-block-paragraph">say I want my stack to hold integers and then I&#8217;m going to say s is the variable that I&#8217;ll use for</p>



<p class="wp-block-paragraph">the stack so but this is not really a programming video this is just about stacks so if I wanted to</p>



<p class="wp-block-paragraph">stack to push something like onto the top of the stack i would use the the method push the method</p>



<p class="wp-block-paragraph">push usually has this prototype let&#8217;s see i&#8217;ll do stack push and it usually takes in a t type element</p>



<p class="wp-block-paragraph">and again the t type it&#8217;s a templated data type which means right now if i just declared a stack</p>



<p class="wp-block-paragraph">that was of type integer it holds integers then the t would actually expand under the hood in c</p>



<p class="wp-block-paragraph">hood in C++ anyway at compile time to be an integer. So it would be like typing int e, right?</p>



<p class="wp-block-paragraph">So whenever I type t, just understand that that means one instance of a thing that you could put</p>



<p class="wp-block-paragraph">into the stack. So we&#8217;ve got a push here and usually the return type is void and it takes in</p>



<p class="wp-block-paragraph">one element to push. And that means if I&#8217;m going to actually call, let me put some comments here.</p>



<p class="wp-block-paragraph">If I&#8217;m going to actually call push on the stack, I&#8217;ve got to give it something to push and the</p>



<p class="wp-block-paragraph">type of the thing that I&#8217;m pushing has to match what the stack was declared to hold. So in this</p>



<p class="wp-block-paragraph">case, it&#8217;s integers. So I&#8217;m going to put an eight to match the diagram up above. And then after that,</p>



<p class="wp-block-paragraph">maybe I wanted to push another number. So it&#8217;s going to be a two. And I&#8217;m just going to match</p>



<p class="wp-block-paragraph">what the diagram has up above. So eight, two, three, four. So that&#8217;s the interface for pushing</p>



<p class="wp-block-paragraph">usually in C++. And other languages should be similar. The other thing you can do with stacks</p>



<p class="wp-block-paragraph">We haven&#8217;t done that yet, but let me show it to you first.</p>



<p class="wp-block-paragraph">POP usually comes in two forms,</p>



<p class="wp-block-paragraph">depending on what implementation you&#8217;re looking at.</p>



<p class="wp-block-paragraph">So sometimes POP returns nothing.</p>



<p class="wp-block-paragraph">It simply removes an item from the top of the stack.</p>



<p class="wp-block-paragraph">And sometimes it returns a copy of the item</p>



<p class="wp-block-paragraph">that you&#8217;re actually removing.</p>



<p class="wp-block-paragraph">So I&#8217;m going to put like stack here and then like stack</p>



<p class="wp-block-paragraph">just to show you that it&#8217;s inside of the namespace.</p>



<p class="wp-block-paragraph">Oh, I don&#8217;t know.</p>



<p class="wp-block-paragraph">Maybe that&#8217;s too C++y.</p>



<p class="wp-block-paragraph">I&#8217;ll get rid of that stuff.</p>



<p class="wp-block-paragraph">Hold on.</p>



<p class="wp-block-paragraph">hold on so we&#8217;ll maybe do this just to show you that we have three separate</p>



<p class="wp-block-paragraph">parts to this little code snip so sometimes pop doesn&#8217;t give you a copy of</p>



<p class="wp-block-paragraph">the thing it removes sometimes it gives you a copy of the thing it removes in</p>



<p class="wp-block-paragraph">cases where pop does not give you a copy of the thing it removes then you would</p>



<p class="wp-block-paragraph">probably want to call top right before you popped and top will return a copy of</p>



<p class="wp-block-paragraph">Just to clarify, if I wanted to remove something and get a copy of it and I had this line 13</p>



<p class="wp-block-paragraph">form of pop from whatever implementation I was using, then well, I would just call pop</p>



<p class="wp-block-paragraph">and I would do something like this, you know, auto element equals, you know, the stack dot</p>



<p class="wp-block-paragraph">pop.</p>



<p class="wp-block-paragraph">And what would happen is under the hood, it removes the item and then it also returns</p>



<p class="wp-block-paragraph">a copy to me.</p>



<p class="wp-block-paragraph">Sorry for being too C++ in this video.</p>



<p class="wp-block-paragraph">On the other hand, if I have the form that has void for pop,</p>



<p class="wp-block-paragraph">then I would have to do something like this.</p>



<p class="wp-block-paragraph">I&#8217;d have to say auto element equals stack.top just to get a copy first,</p>



<p class="wp-block-paragraph">get a copy of that for before I remove it.</p>



<p class="wp-block-paragraph">And then when I&#8217;m finished, then I can say s.pop just to grab a copy of it and then remove it.</p>



<p class="wp-block-paragraph">So for now, we&#8217;re just going to use the form that gives you a copy of the data</p>



<p class="wp-block-paragraph">at the same time that you&#8217;re removing it.</p>



<p class="wp-block-paragraph">thing to keep in mind let&#8217;s see there are two other functions i wanted to mention mention real</p>



<p class="wp-block-paragraph">fast so for now i&#8217;m going to say that we only have the t pop type you can also check to see if a stack</p>



<p class="wp-block-paragraph">is uh is empty and you can also check its size uh usually those are functions that have those names</p>



<p class="wp-block-paragraph">but i&#8217;m going to do let&#8217;s see boolean empty and then i&#8217;m going to say size type size again sorry</p>



<p class="wp-block-paragraph">an unsigned 64-bit integer so just like a count or a size something that&#8217;s just</p>



<p class="wp-block-paragraph">going to be a whole number and never negative so we have this little</p>



<p class="wp-block-paragraph">interface that this data structure has and we have been calling push so far so</p>



<p class="wp-block-paragraph">let&#8217;s go ahead and do some pops I&#8217;m gonna say auto auto elements equals s</p>



<p class="wp-block-paragraph">dot pop maybe I&#8217;ll do two of those in a row maybe instead of calling that</p>



<p class="wp-block-paragraph">just so that we can have different variables to put our pops in.</p>



<p class="wp-block-paragraph">And then maybe after that, I&#8217;ll do some more pushes.</p>



<p class="wp-block-paragraph">I&#8217;ll say, let&#8217;s push like a 15 and let&#8217;s push like a 25.</p>



<p class="wp-block-paragraph">And then I think that means it&#8217;s probably time to erase that,</p>



<p class="wp-block-paragraph">the top part of the diagram with the 8, 2, 3, 4</p>



<p class="wp-block-paragraph">in favor of just whatever pushes and pops we&#8217;re doing.</p>



<p class="wp-block-paragraph">So let me just do this real fast.</p>



<p class="wp-block-paragraph">Okay, so we did our pushes.</p>



<p class="wp-block-paragraph">two three four and then I&#8217;m going to try to grab a couple pieces of data with these pops</p>



<p class="wp-block-paragraph">and for pop all you have to do is just to well I should have mentioned this before but first</p>



<p class="wp-block-paragraph">check to make sure that there is something to pop for example if I had a blank stack let&#8217;s say I had</p>



<p class="wp-block-paragraph">a blank stack right here there&#8217;s just like nothing on it and this green line is non-standard I&#8217;m just</p>



<p class="wp-block-paragraph">drawing it for fun but imagine I had a blank stack with no items and I decided to call pop on it</p>



<p class="wp-block-paragraph">well you&#8217;re not allowed to pop them an empty stack there&#8217;s nothing there to pop which means that&#8217;s a</p>



<p class="wp-block-paragraph">stack there&#8217;s nothing there to pop which means that&#8217;s a really really naughty thing in c++ and</p>



<p class="wp-block-paragraph">other languages we would say that something exceptionally bad has happened the user tried</p>



<p class="wp-block-paragraph">to pop something that wasn&#8217;t there and so then we would do something called throwing an exception</p>



<p class="wp-block-paragraph">at the user again this is not really a code video but if you know a little code hopefully you</p>



<p class="wp-block-paragraph">understand try catch blocks and throwing exceptions when bad things happen so we would just throw an</p>



<p class="wp-block-paragraph">exception at the user if you don&#8217;t know how to code at this point then just keep in mind we would</p>



<p class="wp-block-paragraph">mind we would refuse to do it we would say something bad has happened we can&#8217;t do that</p>



<p class="wp-block-paragraph">and then when you&#8217;re doing your pushes sometimes depending on what what you&#8217;re using to implement</p>



<p class="wp-block-paragraph">the stack under the hood inside of your code you might decide that you&#8217;re using let&#8217;s say you&#8217;re</p>



<p class="wp-block-paragraph">using an array under the hood which has a fixed capacity if you run out of capacity and you can&#8217;t</p>



<p class="wp-block-paragraph">then you would throw if the user tried to push something into a full stack we would call that a</p>



<p class="wp-block-paragraph">stack overflow and then when we tried to pop from an empty stack we would call that stack underflow</p>



<p class="wp-block-paragraph">but for the purposes of this video just assume that the stack is just a diagram and it has no</p>



<p class="wp-block-paragraph">capacity so we can just add as many things as we want on it or maybe we&#8217;re using a linked list</p>



<p class="wp-block-paragraph">under the hood which would have an unlimited capacity or limited only by the machine&#8217;s</p>



<p class="wp-block-paragraph">But anyway, so let&#8217;s go ahead and try to do this pop.</p>



<p class="wp-block-paragraph">So let&#8217;s do a pop here.</p>



<p class="wp-block-paragraph">Well, in the pop, we just look at the top only, right?</p>



<p class="wp-block-paragraph">So we say, well, you know what?</p>



<p class="wp-block-paragraph">Let&#8217;s make this more interesting.</p>



<p class="wp-block-paragraph">Hang on a second.</p>



<p class="wp-block-paragraph">Let&#8217;s make this more interesting.</p>



<p class="wp-block-paragraph">Let&#8217;s do this.</p>



<p class="wp-block-paragraph">I&#8217;m going to do auto a equals s dot top.</p>



<p class="wp-block-paragraph">So I&#8217;m going to grab a value without actually removing it.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">So more interesting.</p>



<p class="wp-block-paragraph">So we push the 8 and we push the 2.</p>



<p class="wp-block-paragraph">So at that point in time, actually let&#8217;s trace this from scratch.</p>



<p class="wp-block-paragraph">So we have like an empty stack here.</p>



<p class="wp-block-paragraph">There&#8217;s nothing there.</p>



<p class="wp-block-paragraph">And we&#8217;re going to push the 8.</p>



<p class="wp-block-paragraph">So the 8 goes on the very top, just like we did last time.</p>



<p class="wp-block-paragraph">I&#8217;m going to put an 8 there.</p>



<p class="wp-block-paragraph">And then we&#8217;re going to push the 2 next.</p>



<p class="wp-block-paragraph">So the 2 just kind of goes on top.</p>



<p class="wp-block-paragraph">And then the next line, line 12, we&#8217;re just checking the top.</p>



<p class="wp-block-paragraph">we&#8217;re allowed to actually look at the top if we want to. So since two is at the top, that means</p>



<p class="wp-block-paragraph">a is equal to two. I&#8217;m going to put a two here to remind ourselves that a is now equal to two.</p>



<p class="wp-block-paragraph">Then we&#8217;re going to do a push three. So I&#8217;m just going to stick a three on top of the stack.</p>



<p class="wp-block-paragraph">And then we&#8217;re going to push a four, four is on top of the stack. Now, again, we&#8217;re not allowed</p>



<p class="wp-block-paragraph">to look at anything in the middle, anything, anything, but the top. So for example, when the</p>



<p class="wp-block-paragraph">it and remove it but now that we have a four on top we can no longer even see the three it&#8217;s not</p>



<p class="wp-block-paragraph">available to us anymore so then we&#8217;re going to pop and put that value into b so um you know what&#8217;s</p>



<p class="wp-block-paragraph">at the very top it&#8217;s a four so that means we&#8217;re going to actually remove the four and stick that</p>



<p class="wp-block-paragraph">into b so i&#8217;m going to put four right here to remind ourselves that b is now equal to four</p>



<p class="wp-block-paragraph">and uh for the purposes of this video you can just imagine that this you know the top of the stack is</p>



<p class="wp-block-paragraph">of the stack is just totally gone deallocated depending on what implementation you use you</p>



<p class="wp-block-paragraph">might want to think about crossing it out maybe there&#8217;s like some junk data there now but for the</p>



<p class="wp-block-paragraph">purposes of this video in this diagram we&#8217;re just saying it&#8217;s gone so it&#8217;s just totally gone</p>



<p class="wp-block-paragraph">okay then let&#8217;s do another pop so we&#8217;re going to pop and that&#8217;s going to become c so obviously the</p>



<p class="wp-block-paragraph">top is uh is now the three so that means the three is going to go into the c variable so here&#8217;s a</p>



<p class="wp-block-paragraph">c variable so here&#8217;s a three right there then the three gets deallocated</p>



<p class="wp-block-paragraph">and now the top is the two so then we&#8217;ll push a 15 and we&#8217;ll push a 25 so again we just put those</p>



<p class="wp-block-paragraph">new items on top of the stack even if there was stuff uh there before it&#8217;s gone now so we&#8217;re just</p>



<p class="wp-block-paragraph">be a 15 and a 25 on top of the stack so we got this and then 25 and then we&#8217;re done adding our</p>



<p class="wp-block-paragraph">stuff so uh the other functions that we talked about are empty and size uh at the very beginning</p>



<p class="wp-block-paragraph">when we had just an empty stack like that if we were to call on the empty function it would tell</p>



<p class="wp-block-paragraph">us true the stack is empty and then every step after that where we had some data in the stack</p>



<p class="wp-block-paragraph">data in the stack like as soon as we even added that eight at the very beginning from that point</p>



<p class="wp-block-paragraph">forward then empty would have returned false to us saying no the stack is uh it&#8217;s not empty</p>



<p class="wp-block-paragraph">the size would have changed uh during every step of the way so you know at line 10 the size would</p>



<p class="wp-block-paragraph">have been one after we&#8217;re finished with line 10 after line 11 the size would have been 2. 12</p>



<p class="wp-block-paragraph">wouldn&#8217;t have changed the size so it still would have been 2. 13 would have upgraded the size to</p>



<p class="wp-block-paragraph">have upgraded the size to four once we popped after that pop was finished it would have been</p>



<p class="wp-block-paragraph">three again then the second pop would have brought it back down to two and then those two pushes</p>



<p class="wp-block-paragraph">would have put it back up to three and then four and so then the final size whoops that was horrible</p>



<p class="wp-block-paragraph">the final size of the stack is four and you can tell that there&#8217;s just four items</p>



<p class="wp-block-paragraph">another way to double check yourself if you&#8217;re kind of trying to trace this on your own is just</p>



<p class="wp-block-paragraph">count the number of pushes and subtract the number of pops so how many pushes do we have one two three</p>



<p class="wp-block-paragraph">2, 3, 4, 5, 6.</p>



<p class="wp-block-paragraph">So that&#8217;s like 6 minus however many pops we saw.</p>



<p class="wp-block-paragraph">We see 2 pops.</p>



<p class="wp-block-paragraph">So 6 minus 2 is equal to 4.</p>



<p class="wp-block-paragraph">So, you know, when you&#8217;re writing this sort of thing down on,</p>



<p class="wp-block-paragraph">like as a diagram to practice,</p>



<p class="wp-block-paragraph">then you know it&#8217;s a 4 just by looking.</p>



<p class="wp-block-paragraph">But what if you did something wrong?</p>



<p class="wp-block-paragraph">It&#8217;s a great idea to double check yourself at all times.</p>



<p class="wp-block-paragraph">So 6 minus 2 is 4.</p>



<p class="wp-block-paragraph">So we know that the size of the stack is 4.</p>



<p class="wp-block-paragraph">Looks pretty good.</p>



<p class="wp-block-paragraph">Looks pretty good and we know the values of A, B, and C and you know one of those was</p>



<p class="wp-block-paragraph">top two of those were pops.</p>



<p class="wp-block-paragraph">And yeah, okay.</p>



<p class="wp-block-paragraph">So notice something though that&#8217;s kind of peculiar about the data that came out.</p>



<p class="wp-block-paragraph">If we added the data, like I guess while we were adding the data, we added first an eight</p>



<p class="wp-block-paragraph">and then a two and then a three and then a four.</p>



<p class="wp-block-paragraph">I&#8217;m just looking at the pushes right now and then a 15 and then a 25, right?</p>



<p class="wp-block-paragraph">So we added the data in that order.</p>



<p class="wp-block-paragraph">order. But then when we grab the data out, let me put this in, but maybe like a red bracket</p>



<p class="wp-block-paragraph">or something. When we grab data out of it, the pops and the tops gave us two and then</p>



<p class="wp-block-paragraph">a, okay, nevermind. That was not a pop. It gave us a four and then a three if we&#8217;re just</p>



<p class="wp-block-paragraph">look at the, look at the first two items. Sorry, sorry, sorry. Look at the last two items right</p>



<p class="wp-block-paragraph">before, and I didn&#8217;t draw this very well. Imagine that we had just pushed the eight and the two and</p>



<p class="wp-block-paragraph">the three and the four only forget about these other pieces of data. So at this point, when we</p>



<p class="wp-block-paragraph">start calling our pops, we have only an eight, two, three, four stack, right? So the data that</p>



<p class="wp-block-paragraph">has a four and then a three if we&#8217;re removing data from the stack.</p>



<p class="wp-block-paragraph">So notice how the four and the three are backwards.</p>



<p class="wp-block-paragraph">Notice also like if we kept popping out data, you know, we got the four and then we got</p>



<p class="wp-block-paragraph">the three, the next, what&#8217;s the next thing that would come out?</p>



<p class="wp-block-paragraph">It would be the 25, right?</p>



<p class="wp-block-paragraph">And then it would be the 15.</p>



<p class="wp-block-paragraph">And then it would be the two.</p>



<p class="wp-block-paragraph">And then it would be the eight.</p>



<p class="wp-block-paragraph">So if you really think about it, the stack is reversing our input data.</p>



<p class="wp-block-paragraph">We added a three and then a four, but we received a four and then a three.</p>



<p class="wp-block-paragraph">And of course, it&#8217;s a little bit muddled because we have some pushes in between some tops and pops and stuff.</p>



<p class="wp-block-paragraph">But just looking at what goes into the stack, it&#8217;s backwards, right?</p>



<p class="wp-block-paragraph">Eight, two, 15, 25.</p>



<p class="wp-block-paragraph">Let me finish this push sequence up here.</p>



<p class="wp-block-paragraph">So we added three and four, and then we added a 15.</p>



<p class="wp-block-paragraph">And then, oh, my penmanship, dude.</p>



<p class="wp-block-paragraph">shit dude 15 and then we added a 25 so if you look at the data that comes out this is like a 15 and a</p>



<p class="wp-block-paragraph">25 backwards and then the three and the four are backwards and then the two and the eight are also</p>



<p class="wp-block-paragraph">backwards and so one thing that a stack does is it reverses data let me give you a cleaner example</p>



<p class="wp-block-paragraph">with no pops in between the pushes just to show you what we&#8217;re talking about a little bit more</p>



<p class="wp-block-paragraph">bit more clearly okay so i&#8217;m going to do maybe like another code page here whoops do that and</p>



<p class="wp-block-paragraph">then i&#8217;m going to say got our stack and we&#8217;re just going to push in some data</p>



<p class="wp-block-paragraph">and then we&#8217;ll just say s.pop and maybe i&#8217;ll just do that however many times</p>



<p class="wp-block-paragraph">six two three four five six so now just a very quick trace because this is basically the same</p>



<p class="wp-block-paragraph">intermixed. So I&#8217;m going to do like the bottom of the stack here. And then, well, I mean like an</p>



<p class="wp-block-paragraph">empty stack. That&#8217;s not really a bottom of a stack. So we&#8217;re going to push an eight.</p>



<p class="wp-block-paragraph">And then we&#8217;re going to push a two. And then we&#8217;re going to push a three. And then we&#8217;re</p>



<p class="wp-block-paragraph">going to push a four. Then a 15. Got to work on my fives, dude. Okay. And then 25.</p>



<p class="wp-block-paragraph">Okay, one more time.</p>



<p class="wp-block-paragraph">Oh yeah, I guess I got to slow down.</p>



<p class="wp-block-paragraph">So what&#8217;s going to come out?</p>



<p class="wp-block-paragraph">Remember the top is the only place that will give us data.</p>



<p class="wp-block-paragraph">So if we start popping all these one by one, we&#8217;re going to end up with a 25 for that first</p>



<p class="wp-block-paragraph">pop.</p>



<p class="wp-block-paragraph">Maybe I should write it down over here.</p>



<p class="wp-block-paragraph">Do like a 25 and maybe I&#8217;ll add some line breaks in the code here so that it&#8217;s easier</p>



<p class="wp-block-paragraph">for me to write with a pen.</p>



<p class="wp-block-paragraph">Do this and that.</p>



<p class="wp-block-paragraph">Okay, so the 25, I&#8217;m just going to do a red line through it to designate or to denote</p>



<p class="wp-block-paragraph">deallocation or just like gone-ness.</p>



<p class="wp-block-paragraph">Then the top is at the 15.</p>



<p class="wp-block-paragraph">We do another pop.</p>



<p class="wp-block-paragraph">It&#8217;s going to be 15.</p>



<p class="wp-block-paragraph">Oh, I did my first perfect five today.</p>



<p class="wp-block-paragraph">And then the top of the stack is now at the four.</p>



<p class="wp-block-paragraph">So then the four is going to pop out next.</p>



<p class="wp-block-paragraph">And then the three is going to pop out next.</p>



<p class="wp-block-paragraph">The top is now at the three.</p>



<p class="wp-block-paragraph">the three so the three is coming out for that pop we deallocate the three the top goes down one and</p>



<p class="wp-block-paragraph">then we pop it becomes the two that&#8217;s deallocated now and then we grab the eight because that&#8217;s</p>



<p class="wp-block-paragraph">where the top is so now we&#8217;ve grabbed all of our data out and I just want you to look at this one</p>



<p class="wp-block-paragraph">more time notice the data is eight that the data that went in was eight two three four fifteen twenty</p>



<p class="wp-block-paragraph">The data that came out was backwards 25, 15, 4, 3, 2, 8 or 8, 2, 3, 4, 15, 25.</p>



<p class="wp-block-paragraph">If you kind of like look up.</p>



<p class="wp-block-paragraph">So a stack is a data reverser.</p>



<p class="wp-block-paragraph">A stack is also known as something called a, let&#8217;s see,</p>



<p class="wp-block-paragraph">last in first out data structure, LIFO,</p>



<p class="wp-block-paragraph">or first in last out, phylo data structure.</p>



<p class="wp-block-paragraph">What that basically means is just it&#8217;s reversing the data or priority goes to the item that is the youngest.</p>



<p class="wp-block-paragraph">So if you looked at the stack at the point where we were about to remove the 25,</p>



<p class="wp-block-paragraph">25 was the most recently added or the youngest item or the item with, I guess, the timestamp, the furthest in the future,</p>



<p class="wp-block-paragraph">however you want to look at it.</p>



<p class="wp-block-paragraph">And that&#8217;s the item that came out.</p>



<p class="wp-block-paragraph">So if you think about it, it was the last item in.</p>



<p class="wp-block-paragraph">And therefore, when we did another pop, it was the first item out.</p>



<p class="wp-block-paragraph">So LIFO or PHILO.</p>



<p class="wp-block-paragraph">and uh you know this eight down here that was obviously the last item that we were able to grab</p>



<p class="wp-block-paragraph">out of it so when we said you know hop our final pop before the stack became empty uh the eight was</p>



<p class="wp-block-paragraph">definitely the first item in so first in was the last out if you really want to you can kind of like</p>



<p class="wp-block-paragraph">jumble these uh these acronyms uh i think most like people just use lipo or phylo</p>



<p class="wp-block-paragraph">life if you say something to me that I understand I think it&#8217;s probably fine</p>



<p class="wp-block-paragraph">but it&#8217;s it&#8217;s non-standard I like to say you know foley first out last in that</p>



<p class="wp-block-paragraph">definitely makes sense it&#8217;s just not very common another thing that I like to</p>



<p class="wp-block-paragraph">do for fun is also I&#8217;ll mix up the phylo and I&#8217;ll say lo-fi because last out is</p>



<p class="wp-block-paragraph">first in and now I&#8217;m cooler than other computer people because I use the word</p>



<p class="wp-block-paragraph">lo-fi I&#8217;ve now got like a giant mustache and like a little like swap meet shirt</p>



<p class="wp-block-paragraph">like swap meat shirt and I have craft beers and I have work boots on and I&#8217;d</p>



<p class="wp-block-paragraph">roll up my jeans and my, my, my, my, my sleeves are rolled up and all that stuff.</p>



<p class="wp-block-paragraph">You know what I&#8217;m saying?</p>



<p class="wp-block-paragraph">I&#8217;m cooler than everybody else now because I used loaf pie anyway.</p>



<p class="wp-block-paragraph">So we used integers.</p>



<p class="wp-block-paragraph">It&#8217;s just important to understand that in modern coding,</p>



<p class="wp-block-paragraph">I think I might&#8217;ve mentioned this before with the T&#8217;s,</p>



<p class="wp-block-paragraph">these stacks are templated data structures,</p>



<p class="wp-block-paragraph">we can hold another data type besides integers as long as we declare what the stack will hold in</p>



<p class="wp-block-paragraph">advance. So instead of integers, you could imagine there&#8217;s like a custom class, like my class,</p>



<p class="wp-block-paragraph">every single, you know, item in the stack is actually a full instance of a my class object,</p>



<p class="wp-block-paragraph">or, you know, floats or strings or just, you know, any data structure you want,</p>



<p class="wp-block-paragraph">you could put another data structure inside of a stack. People do that sometimes just for fun,</p>



<p class="wp-block-paragraph">sometimes just for fun you could take a vector and stick it inside of a stack a list put it inside</p>



<p class="wp-block-paragraph">of a stack you know whatever you want to do just to show that you understand everything</p>



<p class="wp-block-paragraph">um so again by the way the stack is empty if we tried to pop from it at this point</p>



<p class="wp-block-paragraph">this would be called a stack underflow we would probably want to throw an exception at the user</p>



<p class="wp-block-paragraph">or just say we&#8217;re not allowed to do it let&#8217;s see um so i guess now i should tell you some</p>



<p class="wp-block-paragraph">common uses for stacks i&#8217;m looking at my notes right here uh obviously our stack has reversed</p>



<p class="wp-block-paragraph">has reversed the data. So stacks are kind of good for data reversing. Imagine if you had like a word</p>



<p class="wp-block-paragraph">and you wanted to detect if the word was a palindrome or not, you would just like put all</p>



<p class="wp-block-paragraph">the letters of the word into your stack and then grab them back out and see if the word was still</p>



<p class="wp-block-paragraph">in the same order. If it was, then your word was probably a palindrome. If you don&#8217;t remember what</p>



<p class="wp-block-paragraph">a palindrome is, I&#8217;m going to write down the word radar in reverse from backwards, from back to</p>



<p class="wp-block-paragraph">front, right? It&#8217;s the same word. So that&#8217;s a palindrome. You could also use stacks kind of</p>



<p class="wp-block-paragraph">as a trail of breadcrumbs. Like you could imagine that early implementations of browsers</p>



<p class="wp-block-paragraph">had your browsing history in a stack somewhere so that every time you visited a webpage, then</p>



<p class="wp-block-paragraph">some kind of block of information, maybe like a custom class instance was put onto a stack</p>



<p class="wp-block-paragraph">somewhere and it contained information about like when you visited the webpage, what the URL was,</p>



<p class="wp-block-paragraph">it is that the browser wants to remember and then later when you hit the back button then you can</p>



<p class="wp-block-paragraph">imagine the browser is popping from the stack in order to go backwards in your history or your</p>



<p class="wp-block-paragraph">spotify playlist or whatever it is you&#8217;re doing honestly you know these systems are much more</p>



<p class="wp-block-paragraph">advanced than just using you know vanilla stacks now but you could imagine doing something on your</p>



<p class="wp-block-paragraph">own for fun or just what it might have been like in the very beginning i&#8217;m going to erase fully</p>



<p class="wp-block-paragraph">because that&#8217;s just that&#8217;s just that&#8217;s just cringe lo-fi i think is way cooler um so trail of bread</p>



<p class="wp-block-paragraph">So a trail of breadcrumbs, like browser history, you could also imagine undo history, like</p>



<p class="wp-block-paragraph">if you had a, I don&#8217;t know, like a text editor of some sort or like a image editor of some</p>



<p class="wp-block-paragraph">sort, every major change that you did to it, maybe the program under the hood is adding</p>



<p class="wp-block-paragraph">your change to the stack.</p>



<p class="wp-block-paragraph">And that makes it easier to sort of reverse your changes as those changes are popped.</p>



<p class="wp-block-paragraph">And there&#8217;s like a sort of mathematical uses like balancing parentheses and things, but</p>



<p class="wp-block-paragraph">balancing parentheses and things, but I&#8217;m not going to talk about that too much.</p>



<p class="wp-block-paragraph">And one of the most important uses for stacks inside of your computer is actually the call</p>



<p class="wp-block-paragraph">stack. I&#8217;m going to talk about that in a future video. But again, remember each, you know, little</p>



<p class="wp-block-paragraph">item on the stack, it could be a different data structure on its own. It doesn&#8217;t have to be an</p>



<p class="wp-block-paragraph">integer. So imagine that I bundled up a bunch of information about a function call, and I called it</p>



<p class="wp-block-paragraph">I just stuck a call frame on the stack.</p>



<p class="wp-block-paragraph">That&#8217;s basically the call stack.</p>



<p class="wp-block-paragraph">But that&#8217;s for another video.</p>



<p class="wp-block-paragraph">Anyway, so let&#8217;s see.</p>



<p class="wp-block-paragraph">Is there anything else that I wanted to show you?</p>



<p class="wp-block-paragraph">I think we&#8217;re actually done just talking about the basics of stacks.</p>



<p class="wp-block-paragraph">I&#8217;m not talking about time complexities or anything like that in this video.</p>



<p class="wp-block-paragraph">Check a future video if you&#8217;re interested in time complexities per certain implementations</p>



<p class="wp-block-paragraph">and so forth.</p>



<p class="wp-block-paragraph">But for now, this is how a stack works.</p>



<p class="wp-block-paragraph">or LIFO or LOFI data structure.</p>



<p class="wp-block-paragraph">Nobody&#8217;s going to say LOFI but me, just FYI.</p>



<p class="wp-block-paragraph">But maybe everybody will start doing it now.</p>



<p class="wp-block-paragraph">I don&#8217;t know.</p>



<p class="wp-block-paragraph">If it becomes a trend, okay?</p>



<p class="wp-block-paragraph">You heard it here first.</p>



<p class="wp-block-paragraph">Give everybody this link.</p>



<p class="wp-block-paragraph">All right, I hope you feel like an expert on stacks</p>



<p class="wp-block-paragraph">at this point.</p>



<p class="wp-block-paragraph">I hope you had a little bit of fun</p>



<p class="wp-block-paragraph">and learned a little bit of something, some stuffs.</p>



<p class="wp-block-paragraph">I&#8217;ll see you in the next video.</p>



<p class="wp-block-paragraph">hey everybody thanks for watching this video again from the bottom of my heart I really</p>



<p class="wp-block-paragraph">appreciate it I do hope you did learn something and have some fun if you could do me a please a</p>



<p class="wp-block-paragraph">small little favor could you please subscribe and follow this channel or these videos or whatever it</p>



<p class="wp-block-paragraph">is you do on the current social media website that you&#8217;re looking at right now it would really mean</p>



<p class="wp-block-paragraph">the world to me and it&#8217;ll help make more videos and grow this community so we&#8217;ll be able to do</p>



<p class="wp-block-paragraph">videos, longer videos, better videos, or just I&#8217;ll be able to keep making videos in general. So please</p>



<p class="wp-block-paragraph">do me a kindness and subscribe. You know, sometimes I&#8217;m sleeping in the middle of the night</p>



<p class="wp-block-paragraph">and I just wake up because I know somebody subscribed or followed. It just wakes me up</p>



<p class="wp-block-paragraph">and I get filled with joy. That&#8217;s exactly what happens every single time. So you could do it as</p>



<p class="wp-block-paragraph">a nice favor to me or you could you could troll me if you want to just wake me up in the middle</p>



<p class="wp-block-paragraph">of the night, just subscribe and then I&#8217;ll just wake up. I promise that&#8217;s what will happen.</p>



<p class="wp-block-paragraph">Also, 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</p>



<p class="wp-block-paragraph">which I think is also named somewhere at the bottom of this video and</p>



<p class="wp-block-paragraph">It&#8217;ll take you to my main website where you can just kind of like see all the videos</p>



<p class="wp-block-paragraph">I published and the services and tutorials and things that I offer and all that good stuff and</p>



<p class="wp-block-paragraph">If you have a suggestion for</p>



<p class="wp-block-paragraph">Clarifications or errata or just future videos that you want to see</p>



<p class="wp-block-paragraph">please leave a comment or if you just want to say hey what&#8217;s up what&#8217;s going on you know just send</p>



<p class="wp-block-paragraph">me a comment whatever i also wake up for those in the middle of the night i get i wake up in a cold</p>



<p class="wp-block-paragraph">sweat and i&#8217;m like it would really it really mean the world to me i would really appreciate it so</p>



<p class="wp-block-paragraph">again thank you so much for watching this video and um enjoy the cool music</p>



<p class="wp-block-paragraph">as as i fade into the darkness which is coming for us all</p>



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/master-stacks-fun-guide-to-data-structures-lifo-real-world-uses/">Master Stacks: Fun Guide to Data Structures, LIFO &amp; Real-World Uses</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/master-stacks-fun-guide-to-data-structures-lifo-real-world-uses/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
		<item>
		<title>Master GNU Debugger: Debug C++ &#038; Assembly Programs with GDB Like a Pro</title>
		<link>https://www.NeuralLantern.com/master-gnu-debugger-debug-c-assembly-programs-with-gdb-like-a-pro/</link>
					<comments>https://www.NeuralLantern.com/master-gnu-debugger-debug-c-assembly-programs-with-gdb-like-a-pro/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Sat, 14 Jun 2025 23:53:13 +0000</pubDate>
				<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Debugging]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[assembly debugging]]></category>
		<category><![CDATA[assembly programming]]></category>
		<category><![CDATA[breakpoints]]></category>
		<category><![CDATA[c++ debugging]]></category>
		<category><![CDATA[C++ programming]]></category>
		<category><![CDATA[call stack]]></category>
		<category><![CDATA[coding for beginners]]></category>
		<category><![CDATA[coding tutorial]]></category>
		<category><![CDATA[debug symbols]]></category>
		<category><![CDATA[debugging tools]]></category>
		<category><![CDATA[GDB]]></category>
		<category><![CDATA[GNU Debugger]]></category>
		<category><![CDATA[null pointer]]></category>
		<category><![CDATA[programming]]></category>
		<category><![CDATA[software development]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=155</guid>

					<description><![CDATA[<p>Master GDB to debug C++ &#038; assembly! Learn breakpoints, call stacks, &#038; crash fixes in this fun guide for coders.</p>
<p>The post <a href="https://www.NeuralLantern.com/master-gnu-debugger-debug-c-assembly-programs-with-gdb-like-a-pro/">Master GNU Debugger: Debug C++ &amp; Assembly Programs with GDB Like a Pro</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></description>
										<content:encoded><![CDATA[
<figure class="wp-block-embed is-type-video is-provider-youtube wp-block-embed-youtube wp-embed-aspect-16-9 wp-has-aspect-ratio"><div class="wp-block-embed__wrapper">
<iframe title="Master GNU Debugger: Debug C++ &amp; Assembly Programs with GDB Like a Pro" width="1380" height="776" src="https://www.youtube.com/embed/lPcwsvSWak0?feature=oembed" frameborder="0" allow="accelerometer; autoplay; clipboard-write; encrypted-media; gyroscope; picture-in-picture; web-share" referrerpolicy="strict-origin-when-cross-origin" allowfullscreen></iframe>
</div></figure>



<p class="wp-block-paragraph">Hey everybody! Ready to crush those pesky bugs in your C++ and assembly programs? In this video, we’re diving deep into the GNU Debugger (GDB) – the ultimate tool for tracking down crashes, inspecting variables, and mastering your code. From setting up debug symbols to navigating call stacks, setting breakpoints, and even debugging assembly registers, this fun and relatable guide has you covered. Whether you’re a beginner coder or a seasoned dev, you’ll learn practical tips to level up your debugging game. We’ll also tackle null pointers, create GDB scripts, and explore why your program might be misbehaving. Stick around for a few laughs and some serious skills! Don’t forget to subscribe, hit that like button, and check out my website for more coding tutorials. Let’s debug like pros together!</p>



<p class="wp-block-paragraph">Introduction to GNU Debugger 00:00:00<br>What is a Debugger 00:00:20<br>Debug Symbols Explanation 00:02:08<br>Compiling with Debug Symbols 00:03:13<br>File Size Comparison 00:05:30<br>Installing and Launching GDB 00:06:21<br>GDB Console Basics 00:06:55<br>Attaching GDB to Program 00:08:12<br>Running Program in GDB 00:09:09<br>Handling Program Crash 00:13:02<br>Understanding Call Stack 00:14:05<br>Navigating Call Stack 00:18:13<br>Inspecting Variables 00:19:08<br>Null Pointer Issues 00:21:25<br>Using GDB Script 00:25:10<br>Setting Breakpoints 00:27:04<br>Managing Breakpoints 00:28:37<br>Continuing Execution 00:30:41<br>Debugging Assembly 00:33:24<br>Global Variables in Assembly 00:35:43<br>Advanced GDB Commands 00:38:31<br>Conclusion and Call to Action 00:40:22</p>



<p class="wp-block-paragraph">Thanks for watching!</p>



<p class="wp-block-paragraph">Find us on other social media here:</p>



<ul class="wp-block-list">
<li>https://www.NeuralLantern.com/social</li>
</ul>



<p class="wp-block-paragraph">Please help support us!</p>



<ul class="wp-block-list">
<li>Subscribing + Sharing on Social Media</li>



<li>Leaving a comment or suggestion</li>



<li>Subscribing to our Blog</li>



<li>Watching the main &#8220;pinned&#8221; video of this channel for offers and extras</li>
</ul>



<p class="wp-block-paragraph">Hey everybody! In this video I&#8217;m going to show you how to use the GNU Debugger to</p>



<p class="wp-block-paragraph">debug your C++ and assembly programs.</p>



<p class="wp-block-paragraph">You can also use it for C and some other stuff but we&#8217;re just going to focus on</p>



<p class="wp-block-paragraph">C++ and assembly. What is the GNU Debugger and what is a debugger in the</p>



<p class="wp-block-paragraph">first place? So for starters the GNU Debugger it&#8217;s like it&#8217;s called GDB and</p>



<p class="wp-block-paragraph">the db you can imagine that stands for debugger something like that and the g stands for gnu i</p>



<p class="wp-block-paragraph">personally don&#8217;t know how to uh pronounce gnu so i just say gnu because it it feels more fun to do</p>



<p class="wp-block-paragraph">it that way it&#8217;s more interesting i think probably some people just say new but i don&#8217;t like that</p>



<p class="wp-block-paragraph">because the gdb debugger is really old and robust and powerful it&#8217;s not a new tool it&#8217;s it&#8217;s an old</p>



<p class="wp-block-paragraph">new debugger someone&#8217;s always going to ask what was the old debugger and then I say this is the</p>



<p class="wp-block-paragraph">old debugger the new debugger is the old debugger anyway so we&#8217;re going to use the GNU debugger</p>



<p class="wp-block-paragraph">if you haven&#8217;t yet watched my video on caveman debugging you probably want to watch that first</p>



<p class="wp-block-paragraph">just for like some basics of you know why you might want to look at information in your program</p>



<p class="wp-block-paragraph">but for now we&#8217;ll just assume that you already know what&#8217;s going on with just like why you would</p>



<p class="wp-block-paragraph">know how to make make files see my other videos but here i just have a basic make file all it&#8217;s</p>



<p class="wp-block-paragraph">really going to do is just sort of compile a very simple program and add debug symbols into it so</p>



<p class="wp-block-paragraph">let&#8217;s double check that i&#8217;m actually doing that here yeah there we go okay so the first thing</p>



<p class="wp-block-paragraph">that i should probably show you is uh we are compiling a c plus plus source module so i&#8217;ve</p>



<p class="wp-block-paragraph">actually using c but i i have that left over in there anyway and then we&#8217;re assembling something</p>



<p class="wp-block-paragraph">in yasm we&#8217;re linking with g plus plus because we want to have a hybrid program and we&#8217;ve got</p>



<p class="wp-block-paragraph">all these flags here those are just variables and so uh so now before i can debug with gdb</p>



<p class="wp-block-paragraph">i&#8217;ve got to talk about something called debug symbols what are debug symbols imagine just a</p>



<p class="wp-block-paragraph">bunch of extra information explaining all of the design and variables and everything inside of your</p>



<p class="wp-block-paragraph">about your code stuffed directly into the executable.</p>



<p class="wp-block-paragraph">So imagine, you know, when we compile a C++ source module</p>



<p class="wp-block-paragraph">into an object file, usually that&#8217;s just, you know,</p>



<p class="wp-block-paragraph">assembly at that point.</p>



<p class="wp-block-paragraph">But when we add debug symbols, we&#8217;ll have a bunch of information</p>



<p class="wp-block-paragraph">that can help the debugger figure out where we are in the program,</p>



<p class="wp-block-paragraph">what the variables look like, you know,</p>



<p class="wp-block-paragraph">a bunch of extra information to help the debugger pretty much.</p>



<p class="wp-block-paragraph">The assembler will put debug symbols into the object code</p>



<p class="wp-block-paragraph">symbols into the object code that you generate the compilers will do that the linker will do that</p>



<p class="wp-block-paragraph">and so it&#8217;s important to understand if you don&#8217;t generate debug symbols when you&#8217;re compiling and</p>



<p class="wp-block-paragraph">linking your code then your debugger won&#8217;t actually be able to do anything it won&#8217;t understand what&#8217;s</p>



<p class="wp-block-paragraph">going on you can debug it but like you&#8217;re not going to see anything that&#8217;s very uh that&#8217;s useful</p>



<p class="wp-block-paragraph">so let me show you real fast i&#8217;m going to do make clean and then i&#8217;m going to say make build</p>



<p class="wp-block-paragraph">that I do not have debug symbols.</p>



<p class="wp-block-paragraph">All right, okay, if you don&#8217;t know how to compile and link from the command line, see</p>



<p class="wp-block-paragraph">my other videos, but for now it looks like we&#8217;re not generating debug symbols.</p>



<p class="wp-block-paragraph">So if I list the contents of my directory here, just note the size is real fast.</p>



<p class="wp-block-paragraph">I have an assembly module here that I&#8217;m calling ASI.asm, ASI for assembly, and then we&#8217;re</p>



<p class="wp-block-paragraph">assembling it down to an object file.</p>



<p class="wp-block-paragraph">Notice how it&#8217;s just 656 bytes, very small.</p>



<p class="wp-block-paragraph">plus plus module that I&#8217;m creating here is compiling to an object file that is</p>



<p class="wp-block-paragraph">about four and a half kilobytes so you know not too big the final executable is</p>



<p class="wp-block-paragraph">called main and it&#8217;s about 17 kilobytes so this is without debug symbols when</p>



<p class="wp-block-paragraph">you&#8217;re compiling something on your own you need to add extra switches to enable</p>



<p class="wp-block-paragraph">debug symbols so you can see here in this make file that I&#8217;ve made and again if</p>



<p class="wp-block-paragraph">you don&#8217;t know how to make a make file or compile or link or anything see my</p>



<p class="wp-block-paragraph">But for now, I&#8217;m just going to assume you kind of know your way around to makefile,</p>



<p class="wp-block-paragraph">or at least you can kind of infer what&#8217;s going on.</p>



<p class="wp-block-paragraph">So I&#8217;ve commented out a different version of this variable for the flags of my C++ compiler.</p>



<p class="wp-block-paragraph">If I uncomment this and then comment out the other one,</p>



<p class="wp-block-paragraph">you&#8217;ll notice the only real difference is this part right here, dash G dwarf 2.</p>



<p class="wp-block-paragraph">Usually in C++ compilation, it&#8217;ll just be dash G to generate debug symbols,</p>



<p class="wp-block-paragraph">and then the symbol format will default to a format called dwarf2 so I don&#8217;t know for these</p>



<p class="wp-block-paragraph">tutorials I like to just specify the full format but you can just say dash g in your c++ compiler</p>



<p class="wp-block-paragraph">and linker we do the same thing for the linker down here so I&#8217;m going to uncomment this</p>



<p class="wp-block-paragraph">and comment that and the only difference is just g dwarf2 so I&#8217;m just telling my linker that I also</p>



<p class="wp-block-paragraph">want debug symbols in there too then we do the same thing for the assembler so I&#8217;m going to</p>



<p class="wp-block-paragraph">So I&#8217;m going to uncomment that and comment this other one.</p>



<p class="wp-block-paragraph">And the only difference is that in the assembler,</p>



<p class="wp-block-paragraph">we kind of have to specify it a little bit differently.</p>



<p class="wp-block-paragraph">Instead of G dwarf dash two,</p>



<p class="wp-block-paragraph">it&#8217;s just G dwarf two as just one string.</p>



<p class="wp-block-paragraph">And I think with Yasm, you have to specify the debug symbol format.</p>



<p class="wp-block-paragraph">So just keep that in mind.</p>



<p class="wp-block-paragraph">So now I&#8217;ve turned on all the debug symbols.</p>



<p class="wp-block-paragraph">I&#8217;m going to do real fast, make clean and make build one more time.</p>



<p class="wp-block-paragraph">And then if I list everything,</p>



<p class="wp-block-paragraph">list everything let me just double check that it&#8217;s building the symbols now right okay good</p>



<p class="wp-block-paragraph">notice that the files are bigger so this asio um file is now 1.8 kilobytes instead of you know</p>



<p class="wp-block-paragraph">half a kilobyte the main.o object file is now 36 kilobytes instead of only 4.5 and the executable</p>



<p class="wp-block-paragraph">main is 36 kilobytes instead of just 17. so you can feel it already right like there&#8217;s a bunch</p>



<p class="wp-block-paragraph">extra information sitting inside the object files and the binary which allows us to debug our program</p>



<p class="wp-block-paragraph">okay so now that we&#8217;ve generated debug symbols in our program we can actually think about starting</p>



<p class="wp-block-paragraph">to use gdb so gdb is a program just gdb by the command line if you don&#8217;t have it in your system</p>



<p class="wp-block-paragraph">already you can go sudo apt install gdb or depending on what system you&#8217;re on maybe a or</p>



<p class="wp-block-paragraph">or whatever your package management program is,</p>



<p class="wp-block-paragraph">but we just need to install GDB.</p>



<p class="wp-block-paragraph">On Ubuntu and Debian, I think,</p>



<p class="wp-block-paragraph">there is a meta package called build essential,</p>



<p class="wp-block-paragraph">which will give you a bunch of compilation tools</p>



<p class="wp-block-paragraph">and make file stuff and whatever.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to assume at this point</p>



<p class="wp-block-paragraph">you already have GDB on your system.</p>



<p class="wp-block-paragraph">To launch GDB and go into its console,</p>



<p class="wp-block-paragraph">just type GDB by itself.</p>



<p class="wp-block-paragraph">So now we&#8217;re inside of the GDB console or terminal.</p>



<p class="wp-block-paragraph">console or terminal. We&#8217;re no longer inside of a bash terminal. We can&#8217;t type normal commands.</p>



<p class="wp-block-paragraph">Let me show you real fast. Here&#8217;s the first command you want to learn on GDB. It&#8217;s called</p>



<p class="wp-block-paragraph">quit. Never quit. Never surrender. So, you know, normally if you&#8217;re at the command line,</p>



<p class="wp-block-paragraph">you can do something like cat, etc. OS release just to see what operating system you&#8217;re using.</p>



<p class="wp-block-paragraph">So you can see for this virtual machine that I&#8217;m using right now, I&#8217;ve got like Ubuntu 22 on there.</p>



<p class="wp-block-paragraph">If we go inside of GDB, we can&#8217;t use regular bash commands anymore or regular operating</p>



<p class="wp-block-paragraph">system commands.</p>



<p class="wp-block-paragraph">We can&#8217;t say cat, et cetera, OS release because we can now only use GDB commands.</p>



<p class="wp-block-paragraph">We&#8217;re inside of GDB already.</p>



<p class="wp-block-paragraph">Notice how it says undefined catch command.</p>



<p class="wp-block-paragraph">I don&#8217;t know what you&#8217;re talking about, dude.</p>



<p class="wp-block-paragraph">Okay.</p>



<p class="wp-block-paragraph">So remember we do a Q or quit to get out.</p>



<p class="wp-block-paragraph">You can actually just type Q.</p>



<p class="wp-block-paragraph">A lot of commands that I&#8217;m going to be talking about.</p>



<p class="wp-block-paragraph">going to be talking about you can abbreviate them with one or two or three letters and it totally</p>



<p class="wp-block-paragraph">works just for speed so now that we know how to enter gdb and now that we also know that we have</p>



<p class="wp-block-paragraph">already compiled our program with debug symbols we can attach gdb to our program i mean what&#8217;s the</p>



<p class="wp-block-paragraph">whole point of a debugger we most of the time we will just have our debugger attached to our program</p>



<p class="wp-block-paragraph">and launch it or i guess launch it while it&#8217;s attached you can also attach to a running program</p>



<p class="wp-block-paragraph">but we&#8217;re going to launch a program and stay attached to it.</p>



<p class="wp-block-paragraph">And then we can sort of catch crashes and see what&#8217;s going on.</p>



<p class="wp-block-paragraph">Or we could also tell the debugger to stop the program at certain points in time,</p>



<p class="wp-block-paragraph">like when we hit a certain line number or when a variable changes or something like that.</p>



<p class="wp-block-paragraph">And then we can look around.</p>



<p class="wp-block-paragraph">We can look at all the variables, look at the state of the program,</p>



<p class="wp-block-paragraph">even change things if we wanted to.</p>



<p class="wp-block-paragraph">We can look at all the registers if you&#8217;re coding in assembly.</p>



<p class="wp-block-paragraph">So it&#8217;s pretty useful.</p>



<p class="wp-block-paragraph">Anyway, I&#8217;m going to go gdb and the next command we&#8217;re going to learn is file, which just tells</p>



<p class="wp-block-paragraph">gdb to attach to a program so that we can run it.</p>



<p class="wp-block-paragraph">So, you know, the name of our program here that we compiled is just called main.</p>



<p class="wp-block-paragraph">And so if I type file main, then gdb should load up that file and try to parse its debug</p>



<p class="wp-block-paragraph">symbols so it can begin debugging for you.</p>



<p class="wp-block-paragraph">Notice how it says reading symbols from main.</p>



<p class="wp-block-paragraph">you know if we did a typo or we had something with no debug symbols it will give us some sort</p>



<p class="wp-block-paragraph">of an error message so for now i&#8217;m just going to quit and then go back in just to make sure that</p>



<p class="wp-block-paragraph">it&#8217;s all clean and we&#8217;re going to do file main now it attaches once it&#8217;s attached that&#8217;s not i guess</p>



<p class="wp-block-paragraph">it&#8217;s not technically attached because the program&#8217;s not running but once it has loaded the debug</p>



<p class="wp-block-paragraph">symbols for that program we can run the program with the run command in gdb also kind of interesting</p>



<p class="wp-block-paragraph">If you have debug symbols that are generated separately from your executable,</p>



<p class="wp-block-paragraph">you could tell GDB to load the symbols file and then run your program.</p>



<p class="wp-block-paragraph">That happens a lot in Linux where people release the non-debug friendly version of a program</p>



<p class="wp-block-paragraph">and then they&#8217;ll release the debug symbols separately if you&#8217;re interested.</p>



<p class="wp-block-paragraph">Anyway, so I&#8217;m going to type run and it&#8217;ll run the program.</p>



<p class="wp-block-paragraph">Notice how GDB kind of tells us that it&#8217;s starting up now.</p>



<p class="wp-block-paragraph">here&#8217;s like a thread debugger that&#8217;s being enabled and it says we&#8217;re using this library to do it and</p>



<p class="wp-block-paragraph">then it says hello you know here&#8217;s the hello message so the program is actually running that&#8217;s</p>



<p class="wp-block-paragraph">not my name i wish it was and then it prints the final result and then gdb says your program exited</p>



<p class="wp-block-paragraph">normally on newer versions of gdb there&#8217;s a setting i think by default where it&#8217;ll ask you</p>



<p class="wp-block-paragraph">if you want to start a debug daemon a debug info daemon or something yes or no you can usually just</p>



<p class="wp-block-paragraph">without actually having to type n.</p>



<p class="wp-block-paragraph">It&#8217;s a little bit faster.</p>



<p class="wp-block-paragraph">So the whole program ran and finished.</p>



<p class="wp-block-paragraph">If I type run again, it does the same thing.</p>



<p class="wp-block-paragraph">In GDB, if you hit enter without typing anything,</p>



<p class="wp-block-paragraph">it ends up repeating the previous command.</p>



<p class="wp-block-paragraph">So I&#8217;m not going to type run.</p>



<p class="wp-block-paragraph">If I just hit enter, are you serious?</p>



<p class="wp-block-paragraph">GDB made me into a liar.</p>



<p class="wp-block-paragraph">Well, most of the commands will repeat if you hit enter.</p>



<p class="wp-block-paragraph">I guess not the run command.</p>



<p class="wp-block-paragraph">So I&#8217;ll type run again.</p>



<p class="wp-block-paragraph">And okay, I mean, the program is pretty good, right?</p>



<p class="wp-block-paragraph">So if you watched my…</p>



<p class="wp-block-paragraph">It&#8217;s not pretty good.</p>



<p class="wp-block-paragraph">watched my it&#8217;s not pretty good it&#8217;s a nonsense program I mean it it doesn&#8217;t crash is what I&#8217;m</p>



<p class="wp-block-paragraph">saying if you watched my previous video which you should on caveman debugging I had this uh you know</p>



<p class="wp-block-paragraph">nonsense portion of code that just sort of updated a variable and we pretended that we were confused</p>



<p class="wp-block-paragraph">and we didn&#8217;t understand what was happening and uh we used gdb to debug it so what I&#8217;m going to do</p>



<p class="wp-block-paragraph">either let&#8217;s say we&#8217;ll start off with throwing an exception and then we&#8217;ll try to use a no pointer</p>



<p class="wp-block-paragraph">and then we&#8217;ll see a crash and then after that I&#8217;m going to start using breakpoints where we can stop</p>



<p class="wp-block-paragraph">automatically inside of our program to print the state of the program print the variables</p>



<p class="wp-block-paragraph">and then I&#8217;m going to do that first in C++ and then hopefully I&#8217;ll remember to do that in</p>



<p class="wp-block-paragraph">assembly right after it&#8217;s basically the same thing only with assembly you don&#8217;t really have</p>



<p class="wp-block-paragraph">you know a bunch of variables everywhere you just kind of have registers and maybe some globals</p>



<p class="wp-block-paragraph">globals but it&#8217;s going to be the same deal okay so let&#8217;s pretend that uh we are gonna have a crash</p>



<p class="wp-block-paragraph">okay so this program just kind of runs later on we&#8217;ll figure out why the result is wrong we&#8217;ll</p>



<p class="wp-block-paragraph">just pretend that it&#8217;s wrong but for now we&#8217;ll just say how about we uncomment this line right</p>



<p class="wp-block-paragraph">here and what will happen is when we&#8217;re running the main loop at some point in the loop we get</p>



<p class="wp-block-paragraph">When F gets called, the input will be looked at here.</p>



<p class="wp-block-paragraph">And if the input is more than one,</p>



<p class="wp-block-paragraph">which is definitely gonna happen like right away,</p>



<p class="wp-block-paragraph">then we&#8217;ll call on the G function.</p>



<p class="wp-block-paragraph">Then the G function will just throw an exception.</p>



<p class="wp-block-paragraph">We&#8217;ll just pretend that something really bad happened.</p>



<p class="wp-block-paragraph">You crashed or you actually did throw</p>



<p class="wp-block-paragraph">and you didn&#8217;t catch your throw,</p>



<p class="wp-block-paragraph">or maybe the system threw something at you</p>



<p class="wp-block-paragraph">or another library threw something at you</p>



<p class="wp-block-paragraph">and you didn&#8217;t catch it.</p>



<p class="wp-block-paragraph">So just basically a program crash.</p>



<p class="wp-block-paragraph">Let me run this real fast.</p>



<p class="wp-block-paragraph">So make run just to show you that the program crashes.</p>



<p class="wp-block-paragraph">Make run at this point is the same thing as just kind of executing the program.</p>



<p class="wp-block-paragraph">But you can see that it says terminate called after throwing an instance of runtime error.</p>



<p class="wp-block-paragraph">And then there&#8217;s the message, oh my gosh, and then aborted core dumped.</p>



<p class="wp-block-paragraph">That&#8217;s not good.</p>



<p class="wp-block-paragraph">So now let&#8217;s try to understand why.</p>



<p class="wp-block-paragraph">Why did that crash?</p>



<p class="wp-block-paragraph">Pretend you don&#8217;t know that it happened in G. You know, you&#8217;re trying to investigate.</p>



<p class="wp-block-paragraph">So we could type GDB and then we could type a file to load the debug symbols, but it&#8217;s a little bit faster just to type GDB main and just sort of name the file that you want to load right away.</p>



<p class="wp-block-paragraph">So if I do that, notice how it automatically tries to load the debug symbols from main.</p>



<p class="wp-block-paragraph">Then I can just type run and then the program tries to run and notice how it actually catches the crash.</p>



<p class="wp-block-paragraph">If you look down here, terminate called after throwing an instance of runtime error.</p>



<p class="wp-block-paragraph">throwing an instance of runtime error so that&#8217;s what the program thinks is going on but then the</p>



<p class="wp-block-paragraph">program is trying to terminate but then down here gdb is like wait wait wait i just caught you know</p>



<p class="wp-block-paragraph">a termination uh getting invoked here and you can see this is sort of system code like we did not</p>



<p class="wp-block-paragraph">create this source file p thread kill we didn&#8217;t write that that&#8217;s the standard library and there&#8217;s</p>



<p class="wp-block-paragraph">a function here called p thread kill implementation with those you know fun c underscore prefixes</p>



<p class="wp-block-paragraph">prefixes everywhere and so this is like some sort of a c source file and we don&#8217;t have that file on</p>



<p class="wp-block-paragraph">the system so we can&#8217;t really debug that file lucky for you most of these standard libraries</p>



<p class="wp-block-paragraph">work all the time so you don&#8217;t really need to debug them you need to debug your code instead</p>



<p class="wp-block-paragraph">of the standard library code so the question really is how did i get here how did i get to</p>



<p class="wp-block-paragraph">this crash so the next command we&#8217;ll learn in gdb is just the where command the where command will</p>



<p class="wp-block-paragraph">I&#8217;m going to release a video in the future that kind of explains call stacks, but basically</p>



<p class="wp-block-paragraph">imagine a abstract data type, a data structure called a stack. And every time you want to add</p>



<p class="wp-block-paragraph">data to the stack, it just sort of stacks up on top of the last piece of data. So like if I add</p>



<p class="wp-block-paragraph">something, let&#8217;s say I want to add the number five, then I would just add the number five on top of</p>



<p class="wp-block-paragraph">the stack, right? If I wanted to add the number 11, then I would just add the number 11 onto the</p>



<p class="wp-block-paragraph">off of the stack and the stack is a really good data structure to uh sort of give you a trail of</p>



<p class="wp-block-paragraph">breadcrumbs to know where you got where did you come from so we call this the call stack because</p>



<p class="wp-block-paragraph">what&#8217;s happening is every single time you call a function then the new scope and then you know the</p>



<p class="wp-block-paragraph">new function that you&#8217;re invoking um it has something called a call frame full of information</p>



<p class="wp-block-paragraph">current address is and all that stuff.</p>



<p class="wp-block-paragraph">And we&#8217;ll just call that chunk of data that belongs to one instance of a function call.</p>



<p class="wp-block-paragraph">We&#8217;ll just call that a call frame.</p>



<p class="wp-block-paragraph">And so if you&#8217;re stacking call frames one on top of the other,</p>



<p class="wp-block-paragraph">then we call that whole entire thing the call stack.</p>



<p class="wp-block-paragraph">And this is what allows us to use recursion and all these complicated call graphs and everything.</p>



<p class="wp-block-paragraph">It&#8217;s really simple, but it&#8217;s really powerful.</p>



<p class="wp-block-paragraph">So you&#8217;re looking at the call stack.</p>



<p class="wp-block-paragraph">Each one of these lines is the call frame or is a call frame.</p>



<p class="wp-block-paragraph">And so for example, if you just look at number zero here, you&#8217;re looking at call frame zero,</p>



<p class="wp-block-paragraph">which is the closest frame to wherever the crash occurred, which is if you look at it,</p>



<p class="wp-block-paragraph">that&#8217;s exactly what we were looking at when we saw that original message, right? Call frame zero.</p>



<p class="wp-block-paragraph">So that&#8217;s not really useful. You want to go down to higher numbered call frames or I guess</p>



<p class="wp-block-paragraph">physically lowered number call frames and just kind of scan it until you find some code that</p>



<p class="wp-block-paragraph">raise no abort no none of this stuff is ours live standard c plus plus no the first frame that</p>



<p class="wp-block-paragraph">contains code that is ours is call frame number nine or i guess like nine away from our current</p>



<p class="wp-block-paragraph">position so i guess find the physically highest call frame that is your code or the lowest numbered</p>



<p class="wp-block-paragraph">call frame that is your code and it&#8217;s telling us right here all right we uh we actually seem to</p>



<p class="wp-block-paragraph">So if I go to 65, right there, it tells you exactly where the crash happened.</p>



<p class="wp-block-paragraph">If you&#8217;re still trying to figure out, well, how did I get to that crash?</p>



<p class="wp-block-paragraph">I don&#8217;t know how that actually happened.</p>



<p class="wp-block-paragraph">Just keep looking down further in the call frames.</p>



<p class="wp-block-paragraph">The next one, call frame 10, says, well, we were inside of function f with input equals</p>



<p class="wp-block-paragraph">three at main.cpb line 46.</p>



<p class="wp-block-paragraph">So if I just go to 46, I can get another clue.</p>



<p class="wp-block-paragraph">I can go, all right, so for some reason we called g.</p>



<p class="wp-block-paragraph">Imagine again, this program is much more complicated.</p>



<p class="wp-block-paragraph">And then if we&#8217;re still confused, we just look down at another one and it&#8217;s like, well, this all this whole mess started at CPP main dot CPP line 28.</p>



<p class="wp-block-paragraph">So if we look at line 28, it&#8217;s like, well, we were inside of this loop and we we called the F function and then the F function called the G function.</p>



<p class="wp-block-paragraph">And then G just kind of did a throw. So that gives us a lot of information.</p>



<p class="wp-block-paragraph">All right. Next thing we can do is we can sort of move up and down the call stack, because right now we&#8217;re sitting at call frame zero, which is just the system code.</p>



<p class="wp-block-paragraph">which is just the system code, but maybe we don&#8217;t understand why the throw actually happened at,</p>



<p class="wp-block-paragraph">let&#8217;s say, line 65, right? So we can use up to go up in the call stack to a different call frame.</p>



<p class="wp-block-paragraph">So if I hit up, notice how it mentions that we&#8217;re now in frame number one instead of zero.</p>



<p class="wp-block-paragraph">And if I do up again, we&#8217;re now in frame two, which is a little bit closer. We can use the</p>



<p class="wp-block-paragraph">trick finally where we hit the enter key instead of repeating the command. So I&#8217;m just going to</p>



<p class="wp-block-paragraph">and hit enter a couple times until we eventually get to our code,</p>



<p class="wp-block-paragraph">which I think was supposed to be at frame nine.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to hit it a bunch of times until we get to frame nine.</p>



<p class="wp-block-paragraph">Now we&#8217;re looking sort of back in time at that point</p>



<p class="wp-block-paragraph">because all these other frames actually did get invoked,</p>



<p class="wp-block-paragraph">but we don&#8217;t really care about what was going on inside of them</p>



<p class="wp-block-paragraph">because they probably did their job correctly.</p>



<p class="wp-block-paragraph">We&#8217;re looking at our code, so we&#8217;re going back in time</p>



<p class="wp-block-paragraph">right when we were doing this throw.</p>



<p class="wp-block-paragraph">Oh no, we threw. Why did we throw?</p>



<p class="wp-block-paragraph">well there&#8217;s another command in gdb called info locals</p>



<p class="wp-block-paragraph">okay the info locals uh there&#8217;s there&#8217;s really not much to this maybe maybe let me do this again</p>



<p class="wp-block-paragraph">a equals five and uh then i&#8217;ll just say a plus plus just so that we have a local variable</p>



<p class="wp-block-paragraph">i forgot that gdb doesn&#8217;t consider the incoming arguments as locals so there was nothing there</p>



<p class="wp-block-paragraph">real fast uh gdp oh let&#8217;s do make build and then gdp main and then run and then we have a throw</p>



<p class="wp-block-paragraph">and then i say where and then i go up up up up to a frame you can also use the keyword down if you</p>



<p class="wp-block-paragraph">go too far if you want to go you know back down in the call stack but i&#8217;m just going to use up</p>



<p class="wp-block-paragraph">okay so now we have it here now finally i think i can do info locals just to see the local variables</p>



<p class="wp-block-paragraph">notice how the a variable is there now because I just added a local variable a a moment ago</p>



<p class="wp-block-paragraph">you can also print things directly you can say print the input incoming variable if you&#8217;re in</p>



<p class="wp-block-paragraph">assembly you can also print registers you can say info registers like that and you&#8217;ll get a nice</p>



<p class="wp-block-paragraph">print out of all the registers you could print individual registers you can say let&#8217;s print</p>



<p class="wp-block-paragraph">register r12 you can print register r12 as binary by saying I think it&#8217;s p slash t</p>



<p class="wp-block-paragraph">or print slash t yeah we can probably just do print slash t also</p>



<p class="wp-block-paragraph">t stands for two because they&#8217;re it&#8217;s a base two number system</p>



<p class="wp-block-paragraph">does that mean there&#8217;s a p slash h or a p slash f for 15 or 16 s for 16 i don&#8217;t know i haven&#8217;t</p>



<p class="wp-block-paragraph">tried that is it going to work hang on p slash s bar 12 it&#8217;s not going to work nope didn&#8217;t work</p>



<p class="wp-block-paragraph">i think it thinks it&#8217;s a character point or something but anyway so we can print a lot of</p>



<p class="wp-block-paragraph">So we can print a lot of stuff.</p>



<p class="wp-block-paragraph">And like I said before, we can go up and down the call stack.</p>



<p class="wp-block-paragraph">So I can go down a couple times and then go back up again.</p>



<p class="wp-block-paragraph">And now we can assume that we&#8217;ve kind of debugged, you know, where the source of the error was.</p>



<p class="wp-block-paragraph">So I&#8217;m going to comment that call out to G and run the program again one more time with a different error.</p>



<p class="wp-block-paragraph">So I&#8217;ve commented this code out from before, but just imagine now that we&#8217;re going to use some pointers.</p>



<p class="wp-block-paragraph">We&#8217;re going to start off with a pointer called P.</p>



<p class="wp-block-paragraph">a pointer called p we&#8217;re going to set it to null at the beginning and then we&#8217;re going to just</p>



<p class="wp-block-paragraph">kind of allocate it and then we&#8217;re going to set you know a value and then we&#8217;re going to dereference</p>



<p class="wp-block-paragraph">it and dereferencing a pointer just kind of sets the first value in an array you can do that and</p>



<p class="wp-block-paragraph">then we&#8217;re going to deallocate the pointer and then how about right after that we do something</p>



<p class="wp-block-paragraph">really really naughty we say p at some index is equal to a five this should be this should be a</p>



<p class="wp-block-paragraph">a program crash because we deallocated p already at that point we would be dealing with junk data</p>



<p class="wp-block-paragraph">if we tried to dereference p but to make it even more clear that it&#8217;s naughty we&#8217;ll set it to a no</p>



<p class="wp-block-paragraph">pointer and then we&#8217;ll try to actually use it so let me let me just restart the program real fast</p>



<p class="wp-block-paragraph">i&#8217;m gonna do quit yes and then we&#8217;ll do make a debug just so you know my make debug is just a</p>



<p class="wp-block-paragraph">Let&#8217;s see, where&#8217;s that? Where&#8217;s that? Where&#8217;s that? Do I not have a debug shortcut yet?</p>



<p class="wp-block-paragraph">Oh dear. I need to add that before this video is over. You know what? I&#8217;ll add it right now.</p>



<p class="wp-block-paragraph">We&#8217;ll do this run and we&#8217;ll say debug, make debug, and it needs to build first. And then I&#8217;ll say</p>



<p class="wp-block-paragraph">debugging the program. And then we will do the GDB binary and give it an argument of</p>



<p class="wp-block-paragraph">the executable and then later we can we can upgrade this to take a little script of commands</p>



<p class="wp-block-paragraph">because the commands are going to start getting out of control so now if i do make debug if i</p>



<p class="wp-block-paragraph">didn&#8217;t screw it up now it at least goes in there and i&#8217;ll do run and then we have a seg fault and</p>



<p class="wp-block-paragraph">notice how it tells us right away hey you have a seg fault right here you&#8217;re accessing invalid</p>



<p class="wp-block-paragraph">memory let&#8217;s do info locals just to see what&#8217;s up oh the p is actually a zero that&#8217;s a null pointer</p>



<p class="wp-block-paragraph">explicitly oh whoops you know oh what have I done but it&#8217;s a really good idea to set your</p>



<p class="wp-block-paragraph">pointers to null after you deallocate because if you don&#8217;t you might end up with undefined</p>



<p class="wp-block-paragraph">behavior where sometimes the program doesn&#8217;t crash while you&#8217;re overriding junk data or reading junk</p>



<p class="wp-block-paragraph">data but sometimes it does and you&#8217;re not really sure like why does it work sometimes and why does</p>



<p class="wp-block-paragraph">it not work sometimes so let me comment this out and see if it&#8217;ll crash uh let&#8217;s just let&#8217;s just</p>



<p class="wp-block-paragraph">make debug and then i&#8217;ll do run and then if we do info locals oh yeah okay so this is uh</p>



<p class="wp-block-paragraph">it did seg fault so that&#8217;s good uh if we were unlucky then the program wouldn&#8217;t have crashed</p>



<p class="wp-block-paragraph">it would have just started behaving strangely and um when we print out the value of p notice how it</p>



<p class="wp-block-paragraph">looks like a pointer still like if we were to debug the program and print the pointer value</p>



<p class="wp-block-paragraph">then it doesn&#8217;t really it looks the same it looks the same after we deallocated it right so that</p>



<p class="wp-block-paragraph">this is called a dangling pointer meaning you deallocated the pointer but you forgot to set</p>



<p class="wp-block-paragraph">it to null afterwards so then later when you&#8217;re trying to debug the program it&#8217;s a little confusing</p>



<p class="wp-block-paragraph">because you&#8217;re like that looks like a regular pointer what did i do wrong so um it&#8217;s really</p>



<p class="wp-block-paragraph">good practice to set your pointers to null right after you deallocate them that way when you&#8217;re</p>



<p class="wp-block-paragraph">debugging later because something crashed instead of seeing a memory location on p you&#8217;ll see</p>



<p class="wp-block-paragraph">On P, you&#8217;ll see something that looks definitely like a null pointer and print P.</p>



<p class="wp-block-paragraph">Then it&#8217;s way easier to realize, oh, whoops, I was trying to use a dead pointer, a pointer that I deallocated.</p>



<p class="wp-block-paragraph">Hopefully that makes sense so far.</p>



<p class="wp-block-paragraph">Next thing I want to do is a small upgrade.</p>



<p class="wp-block-paragraph">I can&#8217;t remember if it&#8217;s dash X or dash EX.</p>



<p class="wp-block-paragraph">I think it&#8217;s dash X.</p>



<p class="wp-block-paragraph">But you can write a little script.</p>



<p class="wp-block-paragraph">I have a little script here called Good Doggo.</p>



<p class="wp-block-paragraph">called good doggo you can just make any file name it anything you want i&#8217;m so i&#8217;m just naming it</p>



<p class="wp-block-paragraph">good doggo.txt because my dog&#8217;s taking a little nap behind me as usual um but you could name this</p>



<p class="wp-block-paragraph">gdb.txt or whatever it is you want so you can put inside of this script file any command that you</p>



<p class="wp-block-paragraph">want gdb to execute when it launches so let&#8217;s just comment this stuff out real fast and i&#8217;ll do</p>



<p class="wp-block-paragraph">I&#8217;ll just do run maybe for starters.</p>



<p class="wp-block-paragraph">Because whenever I&#8217;ve been launching for debug,</p>



<p class="wp-block-paragraph">I launch with GDB and then I type run manually, right?</p>



<p class="wp-block-paragraph">So I want to save a little time.</p>



<p class="wp-block-paragraph">So good doggo is the name of the script.</p>



<p class="wp-block-paragraph">So when I invoke GDB, I am going to,</p>



<p class="wp-block-paragraph">where is it right there?</p>



<p class="wp-block-paragraph">Okay, it&#8217;s GDB.</p>



<p class="wp-block-paragraph">And then I&#8217;m going to go dot, I think it&#8217;s X.</p>



<p class="wp-block-paragraph">And then I&#8217;m going to say good doggo.txt.</p>



<p class="wp-block-paragraph">If it&#8217;s not dash x, then it&#8217;s dash ex.</p>



<p class="wp-block-paragraph">One of those executes the command that comes after,</p>



<p class="wp-block-paragraph">and one of those executes the command script that comes after.</p>



<p class="wp-block-paragraph">Let me just double check that this actually is going to work.</p>



<p class="wp-block-paragraph">So make debug.</p>



<p class="wp-block-paragraph">All right, okay, so it was dash x.</p>



<p class="wp-block-paragraph">Dash x specifies a script that will be run.</p>



<p class="wp-block-paragraph">Dash ex just specifies a command that you can put right in there.</p>



<p class="wp-block-paragraph">Like I could have said dash ex run, and it would have ran right away.</p>



<p class="wp-block-paragraph">But the script is more convenient, right?</p>



<p class="wp-block-paragraph">Because we can save typing.</p>



<p class="wp-block-paragraph">So notice how it ran right away and then there&#8217;s a seg fault.</p>



<p class="wp-block-paragraph">Okay, so now let&#8217;s go a little bit deeper.</p>



<p class="wp-block-paragraph">Let&#8217;s change this to false again now that we understand like null pointers and all that</p>



<p class="wp-block-paragraph">stuff.</p>



<p class="wp-block-paragraph">So we can just basically deal a little bit more with only what the main loop is doing.</p>



<p class="wp-block-paragraph">Suppose we just still don&#8217;t really understand what the loop is doing, why our answer is</p>



<p class="wp-block-paragraph">you know, good or bad.</p>



<p class="wp-block-paragraph">So maybe I want to do a breakpoint at the top of that for loop.</p>



<p class="wp-block-paragraph">loop gets to the top of its iteration, I want the program to pause so I can look around.</p>



<p class="wp-block-paragraph">So I&#8217;m going to set up a breakpoint on line 22. And here&#8217;s how you set up breakpoints</p>



<p class="wp-block-paragraph">in GDB. I&#8217;m going to go ahead and maybe do the terminal for now. And then I&#8217;ll stick that into</p>



<p class="wp-block-paragraph">the script next. I will not remember that it&#8217;s line 22. That&#8217;s okay. I&#8217;m going to do quit.</p>



<p class="wp-block-paragraph">And I&#8217;m going to do make build. Then I&#8217;m going to go GDB main just to jump in there.</p>



<p class="wp-block-paragraph">and then i&#8217;m going to say break or just the letter b just to make a break point you type the name of</p>



<p class="wp-block-paragraph">the source code file that you want to break in and then a colon and then the line number where</p>



<p class="wp-block-paragraph">you want it to break so i think i think it actually was line 22 oh i can see it and then</p>



<p class="wp-block-paragraph">maybe i want to have it break at line i don&#8217;t know maybe right here line 25 right before it increases</p>



<p class="wp-block-paragraph">And then so I&#8217;m going to do an up arrow to just repeat the command so I can edit it real fast.</p>



<p class="wp-block-paragraph">And then I&#8217;ll break again at line 28 and then maybe 30 and then 32.</p>



<p class="wp-block-paragraph">So I&#8217;m going to go 28, 30 and 32.</p>



<p class="wp-block-paragraph">30 and I&#8217;m just making up break points.</p>



<p class="wp-block-paragraph">I just want to inspect the program and then maybe I&#8217;ll break at line 36 so I can see what the final answer is.</p>



<p class="wp-block-paragraph">So I just have a bunch of break points set up.</p>



<p class="wp-block-paragraph">Remember when the program is running now under GDB, every time it hits one of those lines,</p>



<p class="wp-block-paragraph">every time it hits one of those lines the program will pause and i&#8217;ll have a chance to look around</p>



<p class="wp-block-paragraph">at the variables and things so i&#8217;m not sure if i&#8217;ve set up the breakpoints correctly i can say</p>



<p class="wp-block-paragraph">info breakpoints and it&#8217;ll show me all of my breakpoints notice how you can enable them and</p>



<p class="wp-block-paragraph">disable them so let&#8217;s say for the sake of argument i want to disable the breakpoint at line 30 because</p>



<p class="wp-block-paragraph">maybe i don&#8217;t really need that right now but i kind of want to keep it in the system for later</p>



<p class="wp-block-paragraph">disable four because it&#8217;s number four.</p>



<p class="wp-block-paragraph">It&#8217;s breakpoint number four.</p>



<p class="wp-block-paragraph">So I can say disable four.</p>



<p class="wp-block-paragraph">Then if I do info breakpoints again,</p>



<p class="wp-block-paragraph">notice how there&#8217;s a little N on there,</p>



<p class="wp-block-paragraph">meaning breakpoint four is disabled.</p>



<p class="wp-block-paragraph">So I could re-enable it with a four</p>



<p class="wp-block-paragraph">and then go info breakpoints again.</p>



<p class="wp-block-paragraph">So now they&#8217;re all enabled.</p>



<p class="wp-block-paragraph">I am going to maybe copy this into the script file real fast</p>



<p class="wp-block-paragraph">so I don&#8217;t have to keep typing this over and over and over again.</p>



<p class="wp-block-paragraph">Imagine if every single time you changed your program and recompiled it you had to type all of the breakpoints from scratch</p>



<p class="wp-block-paragraph">That&#8217;s why I want you to use this little breakpoint script</p>



<p class="wp-block-paragraph">So we&#8217;re going to do B to break at main.cpp line 22 and 25 and</p>



<p class="wp-block-paragraph">28 and</p>



<p class="wp-block-paragraph">30 and 32. I should have copy pasted the three dang it 36</p>



<p class="wp-block-paragraph">And after we&#8217;re done setting up all the breakpoints</p>



<p class="wp-block-paragraph">show me the breakpoints just so I can have a visual reminder. Keep in mind if you change too</p>



<p class="wp-block-paragraph">much of your source code then you&#8217;re probably going to have to update these lines and that&#8217;s</p>



<p class="wp-block-paragraph">okay but it&#8217;s a good idea to just at least you know kind of look at them a little bit so that&#8217;s</p>



<p class="wp-block-paragraph">why I&#8217;m saying info breakpoints. And so then remember again the makefile all it&#8217;s doing when</p>



<p class="wp-block-paragraph">I type make debug is it&#8217;s just launching this command right here it&#8217;s just saying gdb with the</p>



<p class="wp-block-paragraph">with my gdb script so let me quit to get out of this and then i&#8217;m going to say clear and make</p>



<p class="wp-block-paragraph">debug and notice how it did all that stuff for me it added the breakpoints it showed me the break</p>



<p class="wp-block-paragraph">points and then it ran the program like i told it to and then it already broke on a breakpoint it</p>



<p class="wp-block-paragraph">already break it break breakened it&#8217;s broken i don&#8217;t know i don&#8217;t think it&#8217;s broken uh so we&#8217;re</p>



<p class="wp-block-paragraph">So we&#8217;re on breakpoint number one on line 22.</p>



<p class="wp-block-paragraph">And here&#8217;s the for loop.</p>



<p class="wp-block-paragraph">I can type all of my inspection stuff.</p>



<p class="wp-block-paragraph">I can say info locals.</p>



<p class="wp-block-paragraph">I can say print I, I can say print A, I can print whatever I want.</p>



<p class="wp-block-paragraph">And then when I&#8217;m done kind of looking around and inspecting things,</p>



<p class="wp-block-paragraph">I want to continue the program.</p>



<p class="wp-block-paragraph">I don&#8217;t want to just stop it here.</p>



<p class="wp-block-paragraph">So I&#8217;m just going to use the command continue.</p>



<p class="wp-block-paragraph">And it goes right to the next breakpoint.</p>



<p class="wp-block-paragraph">If I want to, you know, continue again, I can hit C.</p>



<p class="wp-block-paragraph">continue again i can hit c or i can just hit enter you know to just sort of keep continuing as i</p>



<p class="wp-block-paragraph">inspect and print things let&#8217;s see when can i get to break point number one is that what&#8217;s going on</p>



<p class="wp-block-paragraph">here hang on a second what is break point number one oh i know what to do info break points</p>



<p class="wp-block-paragraph">break point number one is that main.cpp line 22 is that actually going to get hit oh it&#8217;s</p>



<p class="wp-block-paragraph">having issues here with my GUI. Okay. Line 22. Okay. So it&#8217;s not really hitting that anymore.</p>



<p class="wp-block-paragraph">I guess it&#8217;s not going to think that it hits the top of the forelip. Okay. So I guess breakpoint</p>



<p class="wp-block-paragraph">two is where we&#8217;re always going to be hitting. So let&#8217;s just suppose that I&#8217;m continuing until</p>



<p class="wp-block-paragraph">I hit breakpoint number two. Suppose that the next 10 times I see breakpoint two, I don&#8217;t</p>



<p class="wp-block-paragraph">actually want to stop and break on it. I just want to skip the next 10 iterations for some reason.</p>



<p class="wp-block-paragraph">some reason you can say continue 10 and it&#8217;ll skip the next 10 times that it hits the number two</p>



<p class="wp-block-paragraph">notice how if i hit continue a bunch of times we&#8217;re not really seeing the break point number</p>



<p class="wp-block-paragraph">two anymore we&#8217;re seeing other break points and then eventually if i hit it enough times</p>



<p class="wp-block-paragraph">we&#8217;ll probably see two again oh my god oh my god are we seeing it yet no</p>



<p class="wp-block-paragraph">Okay, wait, are we seeing I&#8217;m freaking myself out.</p>



<p class="wp-block-paragraph">Okay, so let&#8217;s continue 10 times on breakpoint three, then we&#8217;ll only see</p>



<p class="wp-block-paragraph">breakpoint four, I guess.</p>



<p class="wp-block-paragraph">So we can do continue 10 times on breakpoint four.</p>



<p class="wp-block-paragraph">What is going on here?</p>



<p class="wp-block-paragraph">Did I write this program in a weird way?</p>



<p class="wp-block-paragraph">Hmm.</p>



<p class="wp-block-paragraph">Well, trust me on this.</p>



<p class="wp-block-paragraph">Let&#8217;s continue 100 on breakpoint three and then continue 100 on breakpoint four.</p>



<p class="wp-block-paragraph">breakpoint 4. Okay, so now we&#8217;re just done with the whole entire program. I guess there must have</p>



<p class="wp-block-paragraph">been a 2 there that I missed. So now we&#8217;re finally on breakpoint 6, which is when everything is</p>



<p class="wp-block-paragraph">finished. Let me show you real fast that we can add more breakpoints in assembly just to prove</p>



<p class="wp-block-paragraph">that we can debug assembly real fast. So if I want to debug assembly, I just have to type the name of</p>



<p class="wp-block-paragraph">that source code file. If it was nested in a folder, like if you had like a complicated hierarchy of</p>



<p class="wp-block-paragraph">of source code files you would just need to type the relative path but for now I&#8217;m just going to</p>



<p class="wp-block-paragraph">be able to type the name of the file so assy.asm and let&#8217;s suppose that I want to break at line</p>



<p class="wp-block-paragraph">16 just so I can see the registers that I set up so I&#8217;m going to do 16 and then I&#8217;m going to break</p>



<p class="wp-block-paragraph">at assy.asm line 16 and then I&#8217;m going to run and that actually never gets called does it no no it</p>



<p class="wp-block-paragraph">called does it no no it gets called I think I have a call up here somewhere</p>



<p class="wp-block-paragraph">nope I don&#8217;t have a call okay let&#8217;s make a call this is gonna throw off all of my</p>



<p class="wp-block-paragraph">C++ breakpoints so I&#8217;ll just comment them all out right now comment them all</p>



<p class="wp-block-paragraph">out so I don&#8217;t have to redo them and this is a hybrid program so I&#8217;m gonna go</p>



<p class="wp-block-paragraph">extern what is it extern C make a block where I name the function the reason we</p>



<p class="wp-block-paragraph">name mangling so the AC symbol is going to show up as just its regular function</p>



<p class="wp-block-paragraph">name rather than a bunch of extra stuff indicating the prototype so that we can</p>



<p class="wp-block-paragraph">do overloading so we&#8217;re just going to disable that and then at the very top of</p>



<p class="wp-block-paragraph">the program I&#8217;m just going to call it this should hopefully work let me just</p>



<p class="wp-block-paragraph">do a regular make run just to make sure the whole program compiles okay now</p>



<p class="wp-block-paragraph">debug script we&#8217;re gonna break on line 16 which is going to be letting us see</p>



<p class="wp-block-paragraph">the registers okay so I&#8217;m gonna go make debug notice how it hit the breakpoint</p>



<p class="wp-block-paragraph">in the assembly just the same as the C++ and we can print whatever we want now so</p>



<p class="wp-block-paragraph">I&#8217;m just gonna say info registers and if we look carefully we can kind of see</p>



<p class="wp-block-paragraph">that we modified racks REX is 15 so that&#8217;s expected RDI is 20 which is</p>



<p class="wp-block-paragraph">R12 should be a giant number. Where&#8217;s R12? Right there. So you can see the state of your program at any point in time just by breaking on it.</p>



<p class="wp-block-paragraph">I&#8217;m breaking on a certain line. So this is tremendously useful for debugging not just for higher level languages but also assembly.</p>



<p class="wp-block-paragraph">What else can I do? Oh let&#8217;s put a global in the data section. So I&#8217;m gonna do, I don&#8217;t know, my thing or something like that and we&#8217;ll call it a byte array and I&#8217;ll just go hello.</p>



<p class="wp-block-paragraph">I&#8217;ll say we have a thing one and a thing two.</p>



<p class="wp-block-paragraph">Something like that.</p>



<p class="wp-block-paragraph">And maybe thing two is a null terminated string,</p>



<p class="wp-block-paragraph">whereas thing one is a regular thing.</p>



<p class="wp-block-paragraph">We should be able to print those symbols.</p>



<p class="wp-block-paragraph">If that&#8217;s not true, I&#8217;ll come back at a later time</p>



<p class="wp-block-paragraph">and show you how to do it.</p>



<p class="wp-block-paragraph">Anyway, so we&#8217;ll do make debug</p>



<p class="wp-block-paragraph">to just kind of assemble everything again.</p>



<p class="wp-block-paragraph">And we know how to do info registers.</p>



<p class="wp-block-paragraph">We can print, you know, one register in particular,</p>



<p class="wp-block-paragraph">print R12 if we wanted to.</p>



<p class="wp-block-paragraph">History has not reached.</p>



<p class="wp-block-paragraph">History has not reached 12.</p>



<p class="wp-block-paragraph">Oh, I got to put R12.</p>



<p class="wp-block-paragraph">Yeah, then we can get the, wait a minute, wait a minute.</p>



<p class="wp-block-paragraph">Oh, I threw off the line numbers because I added the variables up here.</p>



<p class="wp-block-paragraph">So now we&#8217;re going to break at 21.</p>



<p class="wp-block-paragraph">Let me just fix that.</p>



<p class="wp-block-paragraph">21.</p>



<p class="wp-block-paragraph">We&#8217;ll do a quit.</p>



<p class="wp-block-paragraph">Make debug again.</p>



<p class="wp-block-paragraph">And now we&#8217;re at 21.</p>



<p class="wp-block-paragraph">If I say info registers, we can see R12 is that big value.</p>



<p class="wp-block-paragraph">you, but we can also print R12 directly. And we should be able to print my thing one, hopefully,</p>



<p class="wp-block-paragraph">has an unknown type, oh, we have to cast it. So we can do like basic casting in GDB. So I&#8217;m going</p>



<p class="wp-block-paragraph">to say this is a character pointer. Remember, all of the assembly symbols are basically pointers,</p>



<p class="wp-block-paragraph">unless it&#8217;s an EQU, EQU variable. In that case, it&#8217;s more of a define. So I&#8217;m going to print</p>



<p class="wp-block-paragraph">and then it says error cannot access the memory add address</p>



<p class="wp-block-paragraph">what the heck did i do wrong how about that cannot access that all right i&#8217;ll come back in</p>



<p class="wp-block-paragraph">another video because this wasn&#8217;t something that i prepared for just to print strings in globals</p>



<p class="wp-block-paragraph">i&#8217;ll come back at some later date in the future like five years from now and i&#8217;ll just show you</p>



<p class="wp-block-paragraph">how to print globals but i hope if you&#8217;re in assembly you probably don&#8217;t need to worry about</p>



<p class="wp-block-paragraph">You just have globals, but the real problem for you is going to be what&#8217;s inside of your registers and so forth.</p>



<p class="wp-block-paragraph">You can also deref, I think.</p>



<p class="wp-block-paragraph">Let me see.</p>



<p class="wp-block-paragraph">Okay, I&#8217;m going to move on from that.</p>



<p class="wp-block-paragraph">So anyway, we got a script and we got a lot of basic GDB commands.</p>



<p class="wp-block-paragraph">Is there anything else that I wanted to show you?</p>



<p class="wp-block-paragraph">Let me just look at my notes real fast here to make sure I&#8217;m not forgetting to say anything.</p>



<p class="wp-block-paragraph">make sure I&#8217;m not forgetting to say anything.</p>



<p class="wp-block-paragraph">So we&#8217;re going to do debug symbols, console, quit, attach to binary,</p>



<p class="wp-block-paragraph">launching the program, uncaught exception, breakpoints, info, delete,</p>



<p class="wp-block-paragraph">enable, disable, run, continue, end times, info registers, printing.</p>



<p class="wp-block-paragraph">Oh, we can print a register in binary.</p>



<p class="wp-block-paragraph">Forgot to show that.</p>



<p class="wp-block-paragraph">So we are at a breakpoint, I think.</p>



<p class="wp-block-paragraph">And if we printed R12 with the regular print, we can also do P slash T to just</p>



<p class="wp-block-paragraph">p slash t to just sort of print it in binary if you want to i think i might have said that already</p>



<p class="wp-block-paragraph">either that or i said that like a couple days ago to some and then we can do info locals and</p>



<p class="wp-block-paragraph">then print a variable and then we get the script file and then we can quit okay so i think this is</p>



<p class="wp-block-paragraph">pretty much everything that i wanted to show you you now have a basic idea of what we can do inside</p>



<p class="wp-block-paragraph">of gdb but keep in mind there is so much more you can do in gdb than i can fit in one video</p>



<p class="wp-block-paragraph">can type help and you can see a list of other major you know areas that gdb can handle um you</p>



<p class="wp-block-paragraph">can do uh let&#8217;s see i think it&#8217;s help and then like we&#8217;ve got information on aliases breakpoints</p>



<p class="wp-block-paragraph">data you know whatever we can do i think help breakpoints to see more information on breakpoint</p>



<p class="wp-block-paragraph">commands yeah so all of the things you can do with breakpoints is like all of these commands here</p>



<p class="wp-block-paragraph">one page we have to enter to see one more page or see to see all of the results without using</p>



<p class="wp-block-paragraph">paging anymore so i&#8217;m just hit c and so there&#8217;s like you know we can save trace points we can</p>



<p class="wp-block-paragraph">try to catch exceptions we can enable disable break points there&#8217;s like unwinders which i&#8217;ve</p>



<p class="wp-block-paragraph">never even used we can bookmark things we can you know start tracing memory we can rethrow</p>



<p class="wp-block-paragraph">variables so just keep in mind gdp is incredibly powerful this video barely scratches the surface</p>



<p class="wp-block-paragraph">but for a programmer who is new to debugging i think this will be very useful to you i hope it is</p>



<p class="wp-block-paragraph">anyway thanks for watching this video i hope you learned a little bit of stuff and had a</p>



<p class="wp-block-paragraph">little bit of fun um i will see you in the next video rest yourself and take a nap and have fun</p>



<p class="wp-block-paragraph">and hug your your loved ones okay I&#8217;m out see you later hey everybody thanks</p>



<p class="wp-block-paragraph">for watching this video again from the bottom of my heart I really appreciate</p>



<p class="wp-block-paragraph">it I do hope you did learn something and have some fun if you could do me a</p>



<p class="wp-block-paragraph">please a small little favor could you please subscribe and follow this channel</p>



<p class="wp-block-paragraph">channel or these videos or whatever it is you do on the current social media</p>



<p class="wp-block-paragraph">website that you&#8217;re looking at right now it would really mean the world to me</p>



<p class="wp-block-paragraph">and it&#8217;ll help make more videos and grow this community so we&#8217;ll be able to do</p>



<p class="wp-block-paragraph">more videos longer videos better videos or just I&#8217;ll be able to keep making</p>



<p class="wp-block-paragraph">videos in general so please do do me a kindness and and subscribe you know</p>



<p class="wp-block-paragraph">sometimes I&#8217;m sleeping in the middle of the night and I just wake up because I</p>



<p class="wp-block-paragraph">know somebody subscribed or followed it just wakes me up and I get filled with</p>



<p class="wp-block-paragraph">That&#8217;s exactly what happens every single time.</p>



<p class="wp-block-paragraph">So you could do it as a nice favor to me,</p>



<p class="wp-block-paragraph">or you could troll me if you want to just wake me up in the middle of the night.</p>



<p class="wp-block-paragraph">Just subscribe, and then I&#8217;ll just wake up.</p>



<p class="wp-block-paragraph">I promise that&#8217;s what will happen.</p>



<p class="wp-block-paragraph">Also, if you look at the middle of the screen right now,</p>



<p class="wp-block-paragraph">you should see a QR code, which you can scan in order to go to the website,</p>



<p class="wp-block-paragraph">which I think is also named somewhere at the bottom of this video.</p>



<p class="wp-block-paragraph">And it&#8217;ll take you to my main website,</p>



<p class="wp-block-paragraph">where you can just kind of like see all the videos I published,</p>



<p class="wp-block-paragraph">and the services and tutorials and things that I offer,</p>



<p class="wp-block-paragraph">and all that good stuff.</p>



<p class="wp-block-paragraph">things that I offer and all that good stuff. And, uh, if you have a suggestion for, uh, uh,</p>



<p class="wp-block-paragraph">clarifications or errata, or just future videos that you want to see, please leave a comment.</p>



<p class="wp-block-paragraph">Or if you just want to say, Hey, what&#8217;s up, what&#8217;s going on? You know, just send me a comment,</p>



<p class="wp-block-paragraph">whatever. I also wake up for those in the middle of the night. I get, I wake up in a cold sweat</p>



<p class="wp-block-paragraph">and I&#8217;m like, it would really, it really mean the world to me. I would really appreciate it. So</p>



<p class="wp-block-paragraph">watching this video and enjoy the cool music as I fade into the darkness which is coming for us all.</p>



<p class="wp-block-paragraph">Thank you.</p>
<p>The post <a href="https://www.NeuralLantern.com/master-gnu-debugger-debug-c-assembly-programs-with-gdb-like-a-pro/">Master GNU Debugger: Debug C++ &amp; Assembly Programs with GDB Like a Pro</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/master-gnu-debugger-debug-c-assembly-programs-with-gdb-like-a-pro/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
