<?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>memory management Archives - NeuralLantern.com</title>
	<atom:link href="https://www.NeuralLantern.com/tag/memory-management/feed/" rel="self" type="application/rss+xml" />
	<link>https://www.NeuralLantern.com/tag/memory-management/</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.1.2</generator>

<image>
	<url>https://www.NeuralLantern.com/wp-content/uploads/2025/04/cropped-2025-04-04-Lantern-03-32x32.png</url>
	<title>memory management Archives - NeuralLantern.com</title>
	<link>https://www.NeuralLantern.com/tag/memory-management/</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>Demystifying x86-64 Program Segments: Stack, Heap, and More</title>
		<link>https://www.NeuralLantern.com/demystifying-x86-64-program-segments-stack-heap-and-more/</link>
					<comments>https://www.NeuralLantern.com/demystifying-x86-64-program-segments-stack-heap-and-more/#respond</comments>
		
		<dc:creator><![CDATA[mike]]></dc:creator>
		<pubDate>Mon, 04 Aug 2025 01:16:47 +0000</pubDate>
				<category><![CDATA[Assembly Language]]></category>
		<category><![CDATA[Coding]]></category>
		<category><![CDATA[Computer Science]]></category>
		<category><![CDATA[Videos]]></category>
		<category><![CDATA[assembly programming]]></category>
		<category><![CDATA[BSS section]]></category>
		<category><![CDATA[C++ programming]]></category>
		<category><![CDATA[coding tutorial]]></category>
		<category><![CDATA[computer architecture]]></category>
		<category><![CDATA[data section]]></category>
		<category><![CDATA[heap memory]]></category>
		<category><![CDATA[low-level programming]]></category>
		<category><![CDATA[memory management]]></category>
		<category><![CDATA[program segments]]></category>
		<category><![CDATA[segmentation fault]]></category>
		<category><![CDATA[stack overflow]]></category>
		<category><![CDATA[virtual memory]]></category>
		<category><![CDATA[x86-64]]></category>
		<category><![CDATA[Yasm assembly]]></category>
		<guid isPermaLink="false">https://www.NeuralLantern.com/?p=195</guid>

					<description><![CDATA[<p>Understand x86-64 program segments like stack, heap, data, and BSS. Learn to avoid segmentation faults and stack overflows in this beginner-friendly coding tutorial.</p>
<p>The post <a href="https://www.NeuralLantern.com/demystifying-x86-64-program-segments-stack-heap-and-more/">Demystifying x86-64 Program Segments: Stack, Heap, and More</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="Demystifying x86-64 Program Segments: Stack, Heap, and More" width="1380" height="776" src="https://www.youtube.com/embed/GUicAuRPgPA?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">Join us for a clear, engaging look at x86-64 program segments! We break down the text, data, and BSS sections, explore stack vs. heap memory, and show a real stack overflow in C++. Learn why segmentation faults happen, how virtual memory works, and tips to avoid crashes. Whether you&#8217;re new to assembly or leveling up your coding skills, this video is packed with insights to boost your understanding of low-level programming. Subscribe for more coding deep dives!</p>



<p class="wp-block-paragraph">Introduction to x86-64 program segments 00:00:01<br>Understanding segmentation faults 00:00:12<br>Importance of segments in assembly 00:00:32<br>Overview of assembly program structure 00:01:09<br>Defining segments in Yasm assembly 00:01:49<br>Data section: Initialized global variables 00:02:20<br>BSS section: Uninitialized variables 00:03:23<br>Text section: Code and functions 00:03:59<br>Global and extern function declarations 00:04:41<br>Stack: Local variables and return addresses 00:07:11<br>Stack overflow example in C++ 00:08:48<br>Heap: Dynamic memory allocation 00:11:31<br>Stack vs heap memory growth 00:11:56<br>Virtual memory and memory allocation 00:14:00<br>Demonstrating memory overflow with heap allocation 00:16:59<br>Summary of segments and their purposes 00:17:59<br>Closing remarks and call to subscribe 00:18:47</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">Hi there!</p>



<p class="wp-block-paragraph">Let&#8217;s talk about x86-64 programs and their segments and the purpose of the segments.</p>



<p class="wp-block-paragraph">Probably when you&#8217;ve been programming in the past you&#8217;ve seen segmentation fault errors.</p>



<p class="wp-block-paragraph">If you&#8217;ve accidentally crossed the bounds of some appropriate memory location, if you</p>



<p class="wp-block-paragraph">have an array and you try to index it way outside of bounds or something, you might</p>



<p class="wp-block-paragraph">have seen a segfault.</p>



<p class="wp-block-paragraph">SegFaults basically come from the idea that you have crossed a segment boundary.</p>



<p class="wp-block-paragraph">Segments used to be really, really important.</p>



<p class="wp-block-paragraph">They&#8217;re a little bit less important now that we have virtual memory.</p>



<p class="wp-block-paragraph">I&#8217;ll just talk a little bit about them.</p>



<p class="wp-block-paragraph">I&#8217;m going to be discussing this from the perspective of an assembly program,</p>



<p class="wp-block-paragraph">just because even in higher level languages,</p>



<p class="wp-block-paragraph">your languages will typically be compiled down to assembly</p>



<p class="wp-block-paragraph">first before they actually hit machine code and your actual executable binary.</p>



<p class="wp-block-paragraph">So if you&#8217;re not interested in Yasm Assembly,</p>



<p class="wp-block-paragraph">Yasm assembly I think this could still be worth it for you to watch because</p>



<p class="wp-block-paragraph">you&#8217;ll be able to see the different segments and just kind of get a better</p>



<p class="wp-block-paragraph">understanding of what they are okay so I&#8217;m gonna actually write a very very</p>



<p class="wp-block-paragraph">very quick program in assembly it&#8217;s not gonna really do anything I just want you</p>



<p class="wp-block-paragraph">to show I just want you to see the segments that are involved so for</p>



<p class="wp-block-paragraph">starters let&#8217;s just pretend that I&#8217;m writing a an assembly program here we&#8217;ll</p>



<p class="wp-block-paragraph">This is a comment just so you know what I&#8217;m about to type.</p>



<p class="wp-block-paragraph">Perhaps actually this is not the best editor.</p>



<p class="wp-block-paragraph">I don&#8217;t know why I pulled this up.</p>



<p class="wp-block-paragraph">Let me get Gene here.</p>



<p class="wp-block-paragraph">It&#8217;s a little bit better.</p>



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



<p class="wp-block-paragraph">There we go.</p>



<p class="wp-block-paragraph">Maybe I&#8217;ll save it somewhere.</p>



<p class="wp-block-paragraph">I&#8217;ll say segments assembly.</p>



<p class="wp-block-paragraph">And then now the comment gets highlighted.</p>



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



<p class="wp-block-paragraph">So in assembly in the ASM assembly,</p>



<p class="wp-block-paragraph">we denote various segments by using the section keywords.</p>



<p class="wp-block-paragraph">keywords so we&#8217;ll say section and then dot something indicating the type of segment that we&#8217;re</p>



<p class="wp-block-paragraph">defining so section text and then up here we&#8217;ll have another one section dot bss and you know if</p>



<p class="wp-block-paragraph">you already program assembly you know that somewhere in here is where the code goes right</p>



<p class="wp-block-paragraph">between these sections so just a quick recap of what the data section is it just is where you</p>



<p class="wp-block-paragraph">can name and initialize at the same time at the very top.</p>



<p class="wp-block-paragraph">Globals are not great, but sometimes you want to do that in assembly, depending.</p>



<p class="wp-block-paragraph">It&#8217;s better if you stick all your variables on the stack.</p>



<p class="wp-block-paragraph">Like if they&#8217;re a local variable, it&#8217;s a little bit better.</p>



<p class="wp-block-paragraph">Think of higher level languages.</p>



<p class="wp-block-paragraph">You typically want to avoid global variables if you can possibly avoid them and make</p>



<p class="wp-block-paragraph">everything else a local variable or a member variable.</p>



<p class="wp-block-paragraph">So that means somewhere else other than data, probably the stack, but or the heap.</p>



<p class="wp-block-paragraph">Anyway, you know, this is like the basic pattern.</p>



<p class="wp-block-paragraph">We&#8217;ll say, you know, my variable and then we&#8217;ll decide to say what data size it is.</p>



<p class="wp-block-paragraph">In this case, it&#8217;s DB for one byte of data.</p>



<p class="wp-block-paragraph">You could put a, I think DD in there or a DW for a word.</p>



<p class="wp-block-paragraph">Sorry, sorry, DW would be for a word.</p>



<p class="wp-block-paragraph">And then I think DD would be a double and then DQ would definitely be a quad word.</p>



<p class="wp-block-paragraph">So here quad word is eight bytes.</p>



<p class="wp-block-paragraph">And then I can sort of initialize it to some number.</p>



<p class="wp-block-paragraph">some number and now I have a global variable so not like a huge deal the BSS section is basically</p>



<p class="wp-block-paragraph">where you have uninitialized variables but there you can make huge amounts of variables you can</p>



<p class="wp-block-paragraph">make an array of variables so if I say my array something like that I could put resource queue</p>



<p class="wp-block-paragraph">to say I want the type of one item to be a quad word so you know eight bytes per item and then I</p>



<p class="wp-block-paragraph">could just say I want you know a hundred thousand items or something and I&#8217;ll</p>



<p class="wp-block-paragraph">double check that after I stop recording this video and correct it if I&#8217;m wrong</p>



<p class="wp-block-paragraph">it&#8217;s been a while since I actually typed BSS here and then in the text section</p>



<p class="wp-block-paragraph">this is where you put code and functions and things you typically want to have at</p>



<p class="wp-block-paragraph">least one entry point somewhere you know if you want to make like if you want to</p>



<p class="wp-block-paragraph">make this the entry point of your entire program you&#8217;ll probably do global main</p>



<p class="wp-block-paragraph">main and then this assumes that you use the GCC libraries and just sort of return from the label</p>



<p class="wp-block-paragraph">sort of makes it a function assuming you didn&#8217;t ruin any of the other data.</p>



<p class="wp-block-paragraph">So those are the three sections that we can start off looking at the data section,</p>



<p class="wp-block-paragraph">the BSS section, and the tech section. It&#8217;s important to note that besides marking functions</p>



<p class="wp-block-paragraph">as global so that they&#8217;re accessible to other segments or other modules, we also mark</p>



<p class="wp-block-paragraph">We also mark functions and labels that we would like to access that are in other modules.</p>



<p class="wp-block-paragraph">So if we&#8217;re using the GCC libraries, we could say something like global printf,</p>



<p class="wp-block-paragraph">and then right away we&#8217;re able to call the printf function.</p>



<p class="wp-block-paragraph">Or say if you have another function in another module,</p>



<p class="wp-block-paragraph">like a different source code file that&#8217;s compiled somewhere else,</p>



<p class="wp-block-paragraph">we could say, you know, my other function.</p>



<p class="wp-block-paragraph">Something like that.</p>



<p class="wp-block-paragraph">Just mark it as global.</p>



<p class="wp-block-paragraph">Or sorry, not global.</p>



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



<p class="wp-block-paragraph">Xtern. Global is when you have the function in the current module and you want it to be available to other modules.</p>



<p class="wp-block-paragraph">Xtern is when the function or label is in another module and it&#8217;s already been made available to you,</p>



<p class="wp-block-paragraph">but you want your current module to know that it can access it for the purposes of assembling the program.</p>



<p class="wp-block-paragraph">So that means inside of here we could do something like call.</p>



<p class="wp-block-paragraph">We could say, let&#8217;s call my other function.</p>



<p class="wp-block-paragraph">my other function that will work if the other function is actually a function</p>



<p class="wp-block-paragraph">with a return statement and it respects the ABI you know it pushes registers that</p>



<p class="wp-block-paragraph">it&#8217;s going to modify if they&#8217;re callie saved if it&#8217;s not another function maybe</p>



<p class="wp-block-paragraph">we&#8217;ll say extern my other jump point so like not a proper function but just</p>



<p class="wp-block-paragraph">something that you might want to jump to then instead of using call we would use</p>



<p class="wp-block-paragraph">jump, you know, and so forth. So those are the basic sections. Let&#8217;s see, for the BSS,</p>



<p class="wp-block-paragraph">I definitely talked about token size and count. And then there&#8217;s resource B, resource W, resource</p>



<p class="wp-block-paragraph">D, resource Q for the different sizes. So basically just take all the different sizes you could put</p>



<p class="wp-block-paragraph">in the regular data section and assume that that could be popped on to the end of RES inside of</p>



<p class="wp-block-paragraph">the BSS section. If you&#8217;re interested, this is a segments video, not an assembly video.</p>



<p class="wp-block-paragraph">assembly video um another segment uh another segment that we&#8217;re not showing here in this</p>



<p class="wp-block-paragraph">source code is the actual stack probably i guess the best way that i could show it is</p>



<p class="wp-block-paragraph">i don&#8217;t know by messing around with the stack pointer increase uh</p>



<p class="wp-block-paragraph">oh rsp why did i think it was the instruction pointer we could increase the stack pointer and</p>



<p class="wp-block-paragraph">stack pointer and then decrease the stack pointer.</p>



<p class="wp-block-paragraph">It doesn&#8217;t really help a whole lot.</p>



<p class="wp-block-paragraph">But in another video, I&#8217;m gonna talk more in depth</p>



<p class="wp-block-paragraph">about what the stack is and how to use it and such.</p>



<p class="wp-block-paragraph">But just keep in mind that the stack</p>



<p class="wp-block-paragraph">is where local variables are stored</p>



<p class="wp-block-paragraph">and you can use it in assembly and C++</p>



<p class="wp-block-paragraph">and other higher level languages.</p>



<p class="wp-block-paragraph">You can even make virtual stacks</p>



<p class="wp-block-paragraph">in any type of program almost</p>



<p class="wp-block-paragraph">to just sort of have a stack like structure.</p>



<p class="wp-block-paragraph">But in C++, if you make any local variable,</p>



<p class="wp-block-paragraph">any local variable, then it&#8217;s usually going on the stack.</p>



<p class="wp-block-paragraph">And the stack tends to have a fixed size.</p>



<p class="wp-block-paragraph">And so if you sort of put too much data on the stack,</p>



<p class="wp-block-paragraph">you risk a stack overflow, which will crash your program.</p>



<p class="wp-block-paragraph">Besides local variables, the stack will hold other information that the current scope needs,</p>



<p class="wp-block-paragraph">like its return address.</p>



<p class="wp-block-paragraph">So when we make a function and then let&#8217;s say we call another function inside of it,</p>



<p class="wp-block-paragraph">actually just up here even when we call another function on line 22 the stack will receive the</p>



<p class="wp-block-paragraph">return address of where we currently are so that later when we return from the other function you</p>



<p class="wp-block-paragraph">know the program knows where to jump back and forth it&#8217;s actually under the hood just jump</p>



<p class="wp-block-paragraph">instructions when you do call it&#8217;s it&#8217;s not it&#8217;s not like a special thing it&#8217;s just jumping to</p>



<p class="wp-block-paragraph">another location and then pushing the return address on the call stack so that&#8217;s other stuff</p>



<p class="wp-block-paragraph">other stuff that the stack holds, which means if you have like an endless or like a infinite</p>



<p class="wp-block-paragraph">recursion happening in your function where just a function calls itself over and over</p>



<p class="wp-block-paragraph">and over again, or you have some complicated call graph and you end up calling too many</p>



<p class="wp-block-paragraph">functions without ever returning, you could overflow the stack.</p>



<p class="wp-block-paragraph">So that&#8217;s not good.</p>



<p class="wp-block-paragraph">Let me show you a quick example that I&#8217;m going to make for you real fast.</p>



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



<p class="wp-block-paragraph">Did I actually make this?</p>



<p class="wp-block-paragraph">Where is the, it&#8217;s on the desktop.</p>



<p class="wp-block-paragraph">Okay, I guess I&#8217;m going to make more on the desktop.</p>



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



<p class="wp-block-paragraph">Let me do desktop here.</p>



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



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



<p class="wp-block-paragraph">I&#8217;m just going to make a quick program.</p>



<p class="wp-block-paragraph">In C++, that doesn&#8217;t really do anything except overflow the stack.</p>



<p class="wp-block-paragraph">So I just want you to know the stack can overflow.</p>



<p class="wp-block-paragraph">I&#8217;ll make a function called F.</p>



<p class="wp-block-paragraph">And F just calls itself.</p>



<p class="wp-block-paragraph">calls itself. So that&#8217;s going to be an infinite recursion.</p>



<p class="wp-block-paragraph">And that means, you know, every single time F calls itself,</p>



<p class="wp-block-paragraph">the return address of like line five is going to get pushed onto the stack again.</p>



<p class="wp-block-paragraph">So it&#8217;s just going to get pushed and pushed and pushed and pushed.</p>



<p class="wp-block-paragraph">The stack will overflow faster if I had local variables because every scope that</p>



<p class="wp-block-paragraph">gets called, even if it&#8217;s the same function name, it&#8217;ll have its own allocation on</p>



<p class="wp-block-paragraph">the stack. So if I just sort of like call this from main and then I&#8217;ll just need</p>



<p class="wp-block-paragraph">and then I&#8217;ll just need to make a little uh a little make file that just kind of compiles</p>



<p class="wp-block-paragraph">I don&#8217;t really even need to make a make file I could do a build script but I&#8217;m going to do it</p>



<p class="wp-block-paragraph">anyway we&#8217;ll say g plus plus standard equals c plus plus 23 show all warnings show pedantic</p>



<p class="wp-block-paragraph">the input file will be stack overflow cpp and then the output file will be main</p>



<p class="wp-block-paragraph">main and then assuming that succeeds I&#8217;ll just run main so this is like not a make</p>



<p class="wp-block-paragraph">file video I have other videos for make files let&#8217;s see let me get to the</p>



<p class="wp-block-paragraph">desktop if I run the make file with just the word make you should see a stack</p>



<p class="wp-block-paragraph">overflow we should also see a warning about the fact that there&#8217;s infinite</p>



<p class="wp-block-paragraph">That&#8217;s funny, I must have done something wrong or different because on a different computer</p>



<p class="wp-block-paragraph">not too long ago I did the same thing and I saw a warning.</p>



<p class="wp-block-paragraph">Okay, well thanks for not warning me.</p>



<p class="wp-block-paragraph">But anyway, you can see that we have a segmentation fault here because the stack overflowed.</p>



<p class="wp-block-paragraph">We just made a stack that was too huge.</p>



<p class="wp-block-paragraph">This is a reminder that the stack is not necessarily as dynamic as the heap.</p>



<p class="wp-block-paragraph">You can definitely overflow it.</p>



<p class="wp-block-paragraph">It&#8217;s not really meant to store gigantic amounts of data.</p>



<p class="wp-block-paragraph">What am I talking about when I say the heap though?</p>



<p class="wp-block-paragraph">So just keep in mind that whenever you allocate dynamic data,</p>



<p class="wp-block-paragraph">well, maybe, well, what do you think?</p>



<p class="wp-block-paragraph">Maybe, maybe inside of, instead of calling F, let&#8217;s call G.</p>



<p class="wp-block-paragraph">The heap is where you allocate dynamic data.</p>



<p class="wp-block-paragraph">So anytime you use the malloc operator in C or the new allocator in C++,</p>



<p class="wp-block-paragraph">you&#8217;re creating like a dynamic allocation of data.</p>



<p class="wp-block-paragraph">I&#8217;ll do a new integer and then I&#8217;ll do like a thousand integers so this is</p>



<p class="wp-block-paragraph">basically going to allocate a thousand new integers in the heap which is a</p>



<p class="wp-block-paragraph">segment that is right up against the stack I should mention also that the</p>



<p class="wp-block-paragraph">stack it&#8217;s important the stack its memory locations go down as the stack</p>



<p class="wp-block-paragraph">grows keep that in mind because typically when you when you imagine a</p>



<p class="wp-block-paragraph">stack I&#8217;ll make more videos in the future so you can visualize the stack better but most of you who</p>



<p class="wp-block-paragraph">know what a stack is already you imagine that the stack grows upward visually right but in your</p>



<p class="wp-block-paragraph">computer the memory locations of the stack grow downward so if I add something to the stack then</p>



<p class="wp-block-paragraph">the new stack head pointer is actually going to be a lesser address and the reason for that is the</p>



<p class="wp-block-paragraph">stack and the heap they grow in the same direction towards the same unallocated space we&#8217;ll call that</p>



<p class="wp-block-paragraph">We&#8217;ll call that space the unallocated memory space or the unmapped memory space.</p>



<p class="wp-block-paragraph">And it&#8217;s just, you know, a segment stack in the heap.</p>



<p class="wp-block-paragraph">And then there&#8217;s the unallocated space that they grow towards each other.</p>



<p class="wp-block-paragraph">If they ever meet, then your program is out of memory and it&#8217;ll crash.</p>



<p class="wp-block-paragraph">But the heap actually can be dynamically allocated to store tons of RAM.</p>



<p class="wp-block-paragraph">I&#8217;m probably about to crash this computer.</p>



<p class="wp-block-paragraph">So maybe this video won&#8217;t finish recording.</p>



<p class="wp-block-paragraph">recording so maybe I&#8217;ll try to crash this at the very end I just wanted you to</p>



<p class="wp-block-paragraph">see let me let me do this I&#8217;ll say while true we&#8217;ll just allocate a new integer</p>



<p class="wp-block-paragraph">this is kind of bad form because when you allocate you know a new variable</p>



<p class="wp-block-paragraph">somewhere you want to have access to it after the allocation you would usually</p>



<p class="wp-block-paragraph">send that into a pointer you&#8217;d say like you know integer pointer P equals new</p>



<p class="wp-block-paragraph">in something like that right but in this case I don&#8217;t really care I&#8217;m not gonna</p>



<p class="wp-block-paragraph">that memory i just want you to see how fast the memory can grow when we did the stack overflow</p>



<p class="wp-block-paragraph">seemed like the program died pretty quickly but here we&#8217;re going to be able to see the memory</p>



<p class="wp-block-paragraph">boom pretty pretty big before the whole computer just starts to you know fall to its knees</p>



<p class="wp-block-paragraph">so i&#8217;m going to do a call to g here and then i won&#8217;t actually run this until</p>



<p class="wp-block-paragraph">i&#8217;m ready to crash this whole video let&#8217;s see what else can i say</p>



<p class="wp-block-paragraph">Oh, the reason that the heap can grow so much larger than the stack is our programs now in the modern era, they use virtual memory.</p>



<p class="wp-block-paragraph">So it&#8217;s not like when we allocate, let&#8217;s say we&#8217;re accessing a memory location via a pointer inside of our program.</p>



<p class="wp-block-paragraph">It&#8217;s not that the pointer has the absolute memory location through the whole system.</p>



<p class="wp-block-paragraph">Like it doesn&#8217;t, it&#8217;s not able to name locations outside of the current program.</p>



<p class="wp-block-paragraph">Every program sees its own start memory location as just zero.</p>



<p class="wp-block-paragraph">And then under the hood, there are some extra tricks so that when you try to access a memory location,</p>



<p class="wp-block-paragraph">let&#8217;s say memory location 100, because you&#8217;ve made, I don&#8217;t know, this many integers or whatever.</p>



<p class="wp-block-paragraph">Then under the hood, the computer will know what is the start offset of the program&#8217;s memory.</p>



<p class="wp-block-paragraph">So maybe the start offset is 1000.</p>



<p class="wp-block-paragraph">It&#8217;s like totally unrealistic, but just pretend.</p>



<p class="wp-block-paragraph">The start offset is 1000.</p>



<p class="wp-block-paragraph">1000 then that means if it&#8217;s trying to access a pointer with memory location 100 it just gets added to the offset the virtual offset</p>



<p class="wp-block-paragraph">So it&#8217;ll be a thousand and 100 is the real you know physical memory location or at least at that point</p>



<p class="wp-block-paragraph">It&#8217;ll probably go to the paging system, but that&#8217;s that&#8217;s another video</p>



<p class="wp-block-paragraph">So just know that it&#8217;s virtual which means the program thinks it has the full range of of the 64 bit memory address space</p>



<p class="wp-block-paragraph">Even if you don&#8217;t even have that much memory on your computer, which there&#8217;s no way there&#8217;s no way you have so much memory</p>



<p class="wp-block-paragraph">there&#8217;s no way you have so much memory that it&#8217;s going to overflow or even meet</p>



<p class="wp-block-paragraph">64 bits at all but the program thinks it can so it&#8217;s just going to grow and grow</p>



<p class="wp-block-paragraph">and grow until you actually run out of physical memory and then eventually</p>



<p class="wp-block-paragraph">probably your program is going to start going to the page file or to the swap</p>



<p class="wp-block-paragraph">file and then when you run out of that then your whole computer probably is</p>



<p class="wp-block-paragraph">going to start crashing to be honest it&#8217;ll probably start crashing as soon as</p>



<p class="wp-block-paragraph">it hits the swap file because at that point none of the other programs really</p>



<p class="wp-block-paragraph">None of the other programs really have enough leftover memory to to breathe to do extra</p>



<p class="wp-block-paragraph">allocations and things so everything&#8217;s going to start slowing down it&#8217;s going to be pretty bad</p>



<p class="wp-block-paragraph">and well i think that&#8217;s all i really need to say this is not supposed to be a really long video</p>



<p class="wp-block-paragraph">let me uh let me show you this crasher i&#8217;m going to say goodbye now thank you so much for watching</p>



<p class="wp-block-paragraph">the video i hope you learned a little bit and had some fun too and and that way i&#8217;ve already said</p>



<p class="wp-block-paragraph">if it actually crashes the computer. So I&#8217;m going to open up this and then I&#8217;m going to do maybe</p>



<p class="wp-block-paragraph">HTOP so that I can see memory. So if you kind of like look at HTOP real fast,</p>



<p class="wp-block-paragraph">this is just a Linux command line tool that you can install. It&#8217;s pretty cool.</p>



<p class="wp-block-paragraph">What the heck&#8217;s going on here? Oh, that&#8217;s my recorder. You can see here&#8217;s the memory allocation.</p>



<p class="wp-block-paragraph">So I&#8217;m using two gigs of eight gigs on this virtual machine and then I&#8217;m not using any swap</p>



<p class="wp-block-paragraph">space which means I have not exhausted all my memory and and I have not been forced to go to</p>



<p class="wp-block-paragraph">the disk for like additional memory but in a second we&#8217;ll see that will probably change</p>



<p class="wp-block-paragraph">so I&#8217;m going to do make just to run that it looks like it&#8217;s stuck but if you keep looking at the</p>



<p class="wp-block-paragraph">memory allocation oh it already hit eight gigs now it&#8217;s into the swap already probably</p>



<p class="wp-block-paragraph">The out of memory killer, the OOM, successfully killed the program.</p>



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



<p class="wp-block-paragraph">Or the whole computer would have been down.</p>



<p class="wp-block-paragraph">I always forget that that&#8217;s going to happen lately.</p>



<p class="wp-block-paragraph">But you can see it spiked, right?</p>



<p class="wp-block-paragraph">It went right up to the full physical memory allocation of 7.75 gigs.</p>



<p class="wp-block-paragraph">And then the swap file went all the way up to 2 gigs.</p>



<p class="wp-block-paragraph">You can see that the swap file still has about 700 megs inside of it.</p>



<p class="wp-block-paragraph">That just means that there&#8217;s memory that isn&#8217;t really being accessed.</p>



<p class="wp-block-paragraph">the operating system doesn&#8217;t feel like it&#8217;s necessary to pull it back out of swap and put</p>



<p class="wp-block-paragraph">it into RAM but I wonder if the video skipped when I did that or stuttered or anything I&#8217;m</p>



<p class="wp-block-paragraph">interested to find out now okay so that&#8217;s the end of this video I hope you have a decent basic</p>



<p class="wp-block-paragraph">understanding of segments and then you know like the segments and their purpose you know the heap</p>



<p class="wp-block-paragraph">and the stack they grow together the heap can dynamically allocate to grow bigger notice how it</p>



<p class="wp-block-paragraph">Whereas the stack will crash a lot faster.</p>



<p class="wp-block-paragraph">If the stack and the heap meet, then you&#8217;ve ran out of memory.</p>



<p class="wp-block-paragraph">At that point, you know, you probably want to like resize the heap or the stack just crashes.</p>



<p class="wp-block-paragraph">And then we got the text section of the program and the data section and the BSS section of the program.</p>



<p class="wp-block-paragraph">And these sections are a little bit less relevant in the modern era because we have, you know, virtual memory and stuff.</p>



<p class="wp-block-paragraph">That definitely means you have tried to access outside of your segment and it&#8217;s a no go.</p>



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



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



<p class="wp-block-paragraph">I will see you at a later date and time.</p>



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



<p class="wp-block-paragraph">Thanks for watching this video again from the bottom of my heart.</p>



<p class="wp-block-paragraph">I really appreciate it.</p>



<p class="wp-block-paragraph">I do hope you did learn something and have some fun.</p>



<p class="wp-block-paragraph">If you could do me a please, a small little favor, could you please subscribe and follow</p>



<p class="wp-block-paragraph">as you do on the current social media website</p>



<p class="wp-block-paragraph">that you&#8217;re looking at right now,</p>



<p class="wp-block-paragraph">it would really mean the world to me</p>



<p class="wp-block-paragraph">and it&#8217;ll help make more videos</p>



<p class="wp-block-paragraph">and grow this community.</p>



<p class="wp-block-paragraph">So we&#8217;ll be able to do more videos,</p>



<p class="wp-block-paragraph">longer videos, better videos,</p>



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



<p class="wp-block-paragraph">So please do me a kindness and subscribe.</p>



<p class="wp-block-paragraph">You know, sometimes I&#8217;m sleeping</p>



<p class="wp-block-paragraph">in the middle of the night</p>



<p class="wp-block-paragraph">and I just wake up</p>



<p class="wp-block-paragraph">because I know somebody subscribed or followed.</p>



<p class="wp-block-paragraph">It just wakes me up and I get filled with joy.</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 or you could you could troll me if you want to just wake me up in the middle</p>



<p class="wp-block-paragraph">And I 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</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</p>



<p class="wp-block-paragraph">and all that good stuff and</p>



<p class="wp-block-paragraph">and uh if you have a suggestion for uh uh clarifications or errata or just future videos</p>



<p class="wp-block-paragraph">that you want to see please leave a comment or if you just want to say hey what&#8217;s up what&#8217;s going on</p>



<p class="wp-block-paragraph">you know just send me a comment whatever i also wake up for those in the middle of the night i</p>



<p class="wp-block-paragraph">get i wake up in a cold sweat and i&#8217;m like it would really it really mean the world to me i</p>



<p class="wp-block-paragraph">would really appreciate it so again thank you so much for watching this video and um enjoy the cool</p>



<p class="wp-block-paragraph">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/demystifying-x86-64-program-segments-stack-heap-and-more/">Demystifying x86-64 Program Segments: Stack, Heap, and More</a> appeared first on <a href="https://www.NeuralLantern.com">NeuralLantern.com</a>.</p>
]]></content:encoded>
					
					<wfw:commentRss>https://www.NeuralLantern.com/demystifying-x86-64-program-segments-stack-heap-and-more/feed/</wfw:commentRss>
			<slash:comments>0</slash:comments>
		
		
			</item>
	</channel>
</rss>
