YTread Logo
YTread Logo

Understanding Static, Stack, and Heap Memory Regions (Examples in C)

Jun 08, 2021
Well, welcome back my fellow engineers, so in this topic we will talk about

memory

regions

. This is one of these really essential fundamental topics that provides an

understanding

of how to use different forms of different

memory

regions

to solve different types of problems. Well, one thing we're going to realize is that not all storage is equal and that certain types of information, when we're trying to solve problems and we're trying to schedule certain times, we want to store the information in memory a little bit. differently depending on what we're trying to achieve, so this first video is the goal.
understanding static stack and heap memory regions examples in c
I want to talk about three different memory regions. Talk about some of the differences between them. Show some

examples

of how we can use them correctly. The

examples

are going to be a little simple just to illustrate the point and then we'll continue this later in later videos where we can actually see how we use these memory regions to effectively solve certain types of problems. It makes sense to apply using one region versus another, okay, so the first thing we do is jump in here and I'm not going to introduce it, it's just first like jump in and discard the three different memory regions that we're referring to and now this is A lot of information at once, we'll break it down step by step, we'll look at examples for each of these three regions, okay, but let's talk a little about them at the beginning to get a general mindset. and then that will allow us to go a little deeper into each of these later on.
understanding static stack and heap memory regions examples in c

More Interesting Facts About,

understanding static stack and heap memory regions examples in c...

Well, the first thing is to look at the

static

data. The

static

region is a bit unique. This is information as global variables and one thing we want to keep in mind about this is to think about two different particular attributes: the useful life of the information, basically how long it is retained. We are storing something in this region, how long does it stay there too? like the right size, how big it is, how it grows correctly, so we talk about static data and I think the best way to think about this initially, think about global variables, is that this data will exist and will be persistent for the entire duration. of the entire program, okay, your programs execute those global variables, they are there, okay, they will execute, they will be available for the entire duration of the program, yes, so your programs that execute that information are there, do you think it is The size is fixed, okay, the static region doesn't grow, it doesn't shrink, it doesn't grow, the reason for this is that if you think in the context of global variables, for example, the amount of information that needs to be stored is you know at compile time, right? you're going to compile your slice, your program correctly, the amount of information that should be stored in the static region is known at the time of the first compilation of the program, okay, so it's a fixed size region, it doesn't grow, it doesn't It reduces, okay?
understanding static stack and heap memory regions examples in c
We'll look at some examples of that in a moment, but let's do a high-level compare and contrast with some of the other regions. Well,

stack

the memory, all because it's automatic memory. One unique thing is that this Is this what we've really been used to using up until now and really the examples that we've been doing for pretty much the entire course have been using

heap

memory? It is often known as automatic memory. Well, this is the information that is being used to temporarily store information related to functions, all local variables related to functions are stored in

stack

memory, and this grows as we have more nested function calls, we have a function called function called another function, the stack memory will continue to grow, I will show examples of that time when the stack memory is okay, it grows if we think about how things are addressed in memory, the stack memory grows from a high address, a high memory address, right, up to a and then we grow from a higher address to a lower address.
understanding static stack and heap memory regions examples in c
So as the amount of chaste storage occurs in the stack memory, it grows from a high address down, okay and we have another memory region, the

heap

memory region that grows in the other address grows from a low direction until today, the reason for this is this way. These can continue to grow and we don't run in a memory until these two regions ended up crossing and hitting each other, it would be a situation where you would run out of memory, so we would grow from opposite sides and More or less go and grow one towards the another, but two regions were maximizing the ability to make the most of the amount of memory available.
Well, for our program, heap memory is different from staff memory, it's also different from static memory, and what's unique about it is that it's often referenced. Just like manual memory, the programmer has control, the program logic has control in terms of saying when this memory should be allocated, when it should grow and also when it should be allocated, when it should shrink, ok, program control, so so the lifetime at this size is completely controllable by the programmer. Okay, sometimes it's really helpful to have that level of control when solving problems. Sometimes it's not necessary. Let's see how someone manages to debuff all of this momentarily.
Good, now that you know. We have a bit of a basic foundation in terms of seeing what these looks are, let's make this a bit more concrete. I think what really makes this sink in is looking at some examples, so let's go ahead and do that so let's look at static memory let's look at this first, as I mentioned earlier, probably the simplest example related to static memory is to think about context , if you have a variable that is global like this, my global integer here, global scope, so the scope of that variable exists. across multiple functions, anything in that file would be accessible and the key is that if we imagine that an integer takes up four bytes, we know that we need to store to store this variable, we know that this will require four bytes in memory.
We know that that will require four bytes to go ahead and store that. Well, that is known at compile time. The more you compile that program determines how much should be stored. Another key thing is that there is a copy of this variable. it is never released, okay, the program will always have it, you will never have multiple copies of a global variable, okay, that's nice with static, there is an underlying copy, there is a location in memory for that variable, it was never allocated during the process. all the time the program runs correctly, allowing us to make two, that's one of the reasons it has a fixed size.
Well, I point this out because heap memory doesn't work that way. Okay, when we look at the stack memory in a moment, we'll see That works differently, but let's do another example here and I just want to use the same example, but I'll show it two different ways because global variables are static by default, it's well, there is a copy by default, it is always static. keyword here, this static keyword that makes it can indicate that a variable is static, it's okay in the context of global variables, that's optional, I don't need to put that there, it's implied, it's okay when we compile this correctly, already which is global.
It's known that you mean it should be static, so you don't actually need to include it correctly, which is why the X that comes with that static keyword is optional. Okay, you can put it there if you want. You don't have to do it. It will be in the static memory region either way. Well, there are times when this is not an example. I would say that's an example here that I just saw. It's not really an example of proper programming practice, but I want to point it out I want to see I think it's a useful example because it illustrates a key point: this is an example of having a static variable B inside a function.
That's all. I'm not saying this is a good example. you really want to get it right, but I'm doing this to illustrate what this keyword means and what static memory means. Okay, so just look at this example here. It's a really silly example where we have a main function that calls this function. f 1 4 x okay and we have this static variable B, this integer B and we're adding 1 every time the function is called. The key thing to keep in mind here is that this is just static. I have one and only one copy of that integer okay, one location in memory, that's known at compile time, okay, so if we initially initialize it to zero every time that function is called, we'll go ahead and keep adding one to this, okay every time it's called. because we are operating on the same memory address, the same memory address, the same underlying copy, a single copy of the variable, okay, so let's look at our console output here, we see 0 1 2 3 every time it is called to that function, let's move on. and increase that value by 1.
Okay, now let's see how this is different from the examples that we were much more used to, for example, the ones we've been used to all semester. Looking at stack memory. Okay, let's look at the stack memory and compare this to the example. we just saw with static memory. Well, this is an example we've seen before, but I think it's worth looking at and thinking about how it works differently. The only difference in terms of code from the last example is that I don't. I don't have the word static here, okay, this is a normal old integer, so the scope of that integer is local to that function f1, okay, because of that, that local variable is created and destroyed on every call of function, functions called, we move on. and allocate memory space to it, it's okay that the functions called memory space allocation function return that D we allocate okay, because of this we don't have an underlying copy of the variable, we are constantly creating and destroying them, so we understand this fact that every time you run it, this will always be zero because this line here effectively does nothing, okay, that line that they are actively doing nothing because it's just adding one, but as soon as the function returns, the variables are destroyed, okay , that's the big difference with stack memory.
Well, we'll expand on this in a moment, okay, but the prompt created during a function call D assigned during a function return is okay and that's done automatically for you, that's one of the reasons why a lot of people use this automatic memory. you for the duration of the program, yes, since functions are called in exchange for being allocated slots, you as a programmer don't have to do it yourself, a couple of advantages, okay, I just mentioned that one Automatic makes it a little easier. for the programmer, right, auto-allocate deallocation, that's useful, it's actually also faster than all the alternatives, another alternative memory region we'll talk about momentarily, the heap memory region, okay, it's actually faster due to the fact that it has all the memory. located sequentially in terms of how it will be stored and released, okay, so it has faster times, like access times, read/write times, but the big disadvantage that appears is that there are times when we need a level of much higher control, right? like a program where we need to be able to have a high level of control to say hey I need to create some memory and I want that memory to be created based on some logic and I basically wanted to stay and be available until I decide not to. we don't need it anymore, so this lack of control here is one of the big drawbacks, and we'll see problems as we go forward, where we really need that level of control to solve problems.
An example that we'll look at later is if you think about an example where you're reading data from a file, you don't know how big that file is going to be and we want to be able to have some memory that actually grows based on the size of the file to be able to store that information. , that would be an example where we want as a program, we want to have that logic and control to be able to grow things as the data comes in or shrink them as the data gets smaller, so we want to have that flexibility that we could do That with a different memory region, heap memory, we will see times when there are limitations.
Well, let's look at another example because I think this gives us a little

understanding

of how staff memory works. Well, for a stack, they are an example. something called a last-in-first-out data structure, you follow the data structure course later, you talk a lot more about the different ways that information is stored, but sometimes this is the LIFO Ferger's last in, first out and what we mean by that is when something is added to the stack, okay, the last thing, the last thing in will be the first thing we can take out, so it's happening in reverse order , let me do an example here, okay, Ifollowing example, I'm sure everyone has experience, leave something open. browser, you leave some application open on your computer and your computer is open all night the next day or something and maybe check the cable or whatever you're using to see how much memory is being used on your system. becoming really very slow, maybe you go and look at how much memory is being used and see that for example some application is using an incredible amount of memory, very common, this is seen a lot in browsers, the reason for this is there.
There is a bug in the code that slowly allocates more and more memory as the program runs and they don't release it, so over time the programs that run it use more and more memory and what that will do is eventually it will just it will carry all the resources on your machine correctly so it becomes super slow or potentially crashes so we've all experienced it and the idea here is that you know you override it when you no longer need it, you release it, yeah you don't anymore you need it. release that memory, okay, let's stop there, let's stop there because I want to, we'll expand on this and look at some more examples.
We need to see a lot more using malloc. We need to see many more problems resolved. that depend on heap memory usage, okay and we'll also compare more of the contrast with stack versus heap, but the idea is that at this point the goal is for you to have a pretty good understanding of what the regions are, the pros and cons. . of using the different regions well and understanding when those D spaces are allocated or when space is allocated and when D is allocated, if you understand it well, we have a good foundation that will allow us to continue with the following concepts or to be able to apply these concepts more thoroughly, It is again the most important thing.
The important thing is not just to know the concept, what we really want is to be able to have them as tools to solve problems. That's what programming is. Programming is solving problems automatically. We are automating problem resolution. What I want to do is make sure. that we are giving you that fundamental knowledge that we can actually solve problems using this knowledge, so that is the most important thing we want to drive towards, thanks for watching, let me know if there is anything confusing, I will be happy to clarify it. I hope you found it useful and I highly recommend that you watch it several times.
It's okay, especially the parts you found were confusing. I know sometimes in videos I go fast intentionally because I know you can stop. I know you can listen to it several times. Have a good day. Thanks for watching. I'll talk to you soon.

If you have any copyright issue, please Contact