Stack vs Heap in C# - How They Work, What Goes Where, and the Exceptions
Read full transcript 29 segments
-
Every time you see a variable in C, it Every time you see a variable in C, it either goes onto the stack or the heap. either goes onto the stack or the heap. either goes onto the stack or the heap. But do you know which one and why? It But do you know which one and why? It But do you know which one and why? It isn't as simple as value types versus isn't as simple as value types versus isn't as simple as value types versus reference types. Sometimes value types reference types. Sometimes value types reference types. Sometimes value types are stored in the heap. And then strings are stored in the heap. And then strings are stored in the heap. And then strings come in and make things even more come in and make things even more come in and make things even more confusing. So let's clear things up and confusing. So let's clear things up and confusing. So let's clear things up and understand what the stack is, what the understand what the stack is, what the understand what the stack is, what the heap is, and how they affect our heap is, and how they affect our heap is, and how they affect our application so we can make our application so we can make our application so we can make our applications more efficient. Now, as you applications more efficient. Now, as you applications more efficient. Now, as you probably know, the proceeds from my paid probably know, the proceeds from my paid probably know, the proceeds from my paid courses pay for this free content that I courses pay for this free content that I courses pay for this free content that I produce here on YouTube. With over a produce here on YouTube. With over a produce here on YouTube. With over a thousand free videos on YouTube, that's thousand free videos on YouTube, that's thousand free videos on YouTube, that's a lot of content. So, thank you to all a lot of content. So, thank you to all a lot of content. So, thank you to all the people and businesses that have gone the people and businesses that have gone the people and businesses that have gone to imtcore.com to purchase courses. You to imtcore.com to purchase courses. You to imtcore.com to purchase courses. You help ensure that everyone can have a help ensure that everyone can have a help ensure that everyone can have a great education in C, not just those who great education in C, not just those who great education in C, not just those who can afford it. And if you would like to can afford it. And if you would like to can afford it. And if you would like to help, head over and check out all the help, head over and check out all the help, head over and check out all the content that I have to offer from content that I have to offer from content that I have to offer from beginner courses through more advanced beginner courses through more advanced beginner courses through more advanced topics. I'm sure you'll find something topics. I'm sure you'll find something topics. I'm sure you'll find something that fits your needs. All right, let's that fits your needs. All right, let's that fits your needs. All right, let's jump right in. We're gonna first start jump right in. We're gonna first start jump right in. We're gonna first start here. I'm using Excal to kind of here. I'm using Excal to kind of here. I'm using Excal to kind of illustrate what we need to understand illustrate what we need to understand illustrate what we need to understand about stack versus heap. Now, if you've about stack versus heap. Now, if you've about stack versus heap. Now, if you've never heard these term before, they're never heard these term before, they're never heard these term before, they're two different ways to store our items in two different ways to store our items in two different ways to store our items in memory because every variable has to memory because every variable has to memory because every variable has to live somewhere. And sometimes we hear live somewhere. And sometimes we hear live somewhere. And sometimes we hear things like, "Oh, we have a memory leak things like, "Oh, we have a memory leak things like, "Oh, we have a memory leak or the the application uses a lot of or the the application uses a lot of or the the application uses a lot of memory." And you don't know what that memory." And you don't know what that memory." And you don't know what that is, how it works, and all the rest.
-
is, how it works, and all the rest. is, how it works, and all the rest. We're going to get into the basics of We're going to get into the basics of We're going to get into the basics of stack versus heap. Now, in a future stack versus heap. Now, in a future stack versus heap. Now, in a future video, next week, we're going to talk video, next week, we're going to talk video, next week, we're going to talk about the garbage collection and how about the garbage collection and how about the garbage collection and how that works on the heap. But first, we that works on the heap. But first, we that works on the heap. But first, we need to understand stack versus heap. need to understand stack versus heap. need to understand stack versus heap. So, let's start here with what is the So, let's start here with what is the So, let's start here with what is the stack. So the stack is short-lived and stack. So the stack is short-lived and stack. So the stack is short-lived and it's destroyed when the stack frame is it's destroyed when the stack frame is it's destroyed when the stack frame is closed. Now that's kind of jargony, closed. Now that's kind of jargony, closed. Now that's kind of jargony, right? So what is a stack? Well, if you right? So what is a stack? Well, if you right? So what is a stack? Well, if you have a method and you create inside of have a method and you create inside of have a method and you create inside of that a variable and that variable only that a variable and that variable only that a variable and that variable only lives for that method, that method has lives for that method, that method has lives for that method, that method has called a frame on the stack. And so when called a frame on the stack. And so when called a frame on the stack. And so when we call that method, the variables if we call that method, the variables if we call that method, the variables if they're they are value types. So we're they're they are value types. So we're they're they are value types. So we're talking we'll see what value types are talking we'll see what value types are talking we'll see what value types are in a minute, but things like integer and in a minute, but things like integer and in a minute, but things like integer and double etc. So if it's one of those double etc. So if it's one of those double etc. So if it's one of those value types, there is a a frame that value types, there is a a frame that value types, there is a a frame that gets put into in memory and when the gets put into in memory and when the gets put into in memory and when the method is done, it returns its value and method is done, it returns its value and method is done, it returns its value and it's over. Then it gets popped back out it's over. Then it gets popped back out it's over. Then it gets popped back out of memory. The whole frame does and all of memory. The whole frame does and all of memory. The whole frame does and all that data gets destroyed. The cool thing that data gets destroyed. The cool thing that data gets destroyed. The cool thing there is it's very cheap, which means there is it's very cheap, which means there is it's very cheap, which means that we don't have to worry about memory that we don't have to worry about memory that we don't have to worry about memory leaks there. Everything in the frame leaks there. Everything in the frame leaks there. Everything in the frame gets destroyed all at once. So this gets destroyed all at once. So this gets destroyed all at once. So this means it's shortlived. Only lives the means it's shortlived. Only lives the means it's shortlived. Only lives the life life of the method. Hopefully your life life of the method. Hopefully your life life of the method. Hopefully your methods aren't very long, but that whole methods aren't very long, but that whole methods aren't very long, but that whole frame gets destroyed at the same time.
-
frame gets destroyed at the same time. frame gets destroyed at the same time. That's what the stack is. It's a bunch That's what the stack is. It's a bunch That's what the stack is. It's a bunch of frames. Now there is another type of frames. Now there is another type of frames. Now there is another type called the heap. This is for long lived called the heap. This is for long lived called the heap. This is for long lived items. Think like uh an object like a a items. Think like uh an object like a a items. Think like uh an object like a a person model. That person model might person model. That person model might person model. That person model might live might be created in one method but live might be created in one method but live might be created in one method but then passed to another method, another then passed to another method, another then passed to another method, another method. You pass around the reference to method. You pass around the reference to method. You pass around the reference to that because it's cheaper to do that. that because it's cheaper to do that. that because it's cheaper to do that. What that means is it's going to live What that means is it's going to live What that means is it's going to live longer than the original method that longer than the original method that longer than the original method that creates it. So it lives at least until creates it. So it lives at least until creates it. So it lives at least until it's no longer reachable in the code. So it's no longer reachable in the code. So it's no longer reachable in the code. So that means is when you no longer need that means is when you no longer need that means is when you no longer need that reference anywhere in the code then that reference anywhere in the code then that reference anywhere in the code then that's how long it's going to live at that's how long it's going to live at that's how long it's going to live at least. Now I say at least because this least. Now I say at least because this least. Now I say at least because this is where the garbage collection comes is where the garbage collection comes is where the garbage collection comes in. It may actually live in memory in. It may actually live in memory in. It may actually live in memory longer than that because it's not longer than that because it's not longer than that because it's not collected yet. We'll talk more about collected yet. We'll talk more about collected yet. We'll talk more about that in the next video on garbage that in the next video on garbage that in the next video on garbage collection. But the heap is more collection. But the heap is more collection. But the heap is more expensive in the fact that it's going to expensive in the fact that it's going to expensive in the fact that it's going to live longer than one frame potentially.
-
live longer than one frame potentially. live longer than one frame potentially. But the benefit there is we're not But the benefit there is we're not But the benefit there is we're not copying from one to another. We're not copying from one to another. We're not copying from one to another. We're not copying from one frame to another this copying from one frame to another this copying from one frame to another this data. So the data can the one instance data. So the data can the one instance data. So the data can the one instance of the data can live across frames. So of the data can live across frames. So of the data can live across frames. So there is a difference and there is a there is a difference and there is a there is a difference and there is a reason for both the stack and the heap. reason for both the stack and the heap. reason for both the stack and the heap. These are both very optimized places to These are both very optimized places to These are both very optimized places to store in memory our data. It's not like store in memory our data. It's not like store in memory our data. It's not like one is super inefficient. They're both one is super inefficient. They're both one is super inefficient. They're both very very efficient. It's just that they very very efficient. It's just that they very very efficient. It's just that they deal with different types of objects and deal with different types of objects and deal with different types of objects and so different lifespans. So when you so different lifespans. So when you so different lifespans. So when you think heap, don't think bad and the think heap, don't think bad and the think heap, don't think bad and the stack don't think good because each has stack don't think good because each has stack don't think good because each has their purpose for what they do. You just their purpose for what they do. You just their purpose for what they do. You just need to make sure that you put things in need to make sure that you put things in need to make sure that you put things in the proper place. Next up, let's talk the proper place. Next up, let's talk the proper place. Next up, let's talk about what types are in the go on the about what types are in the go on the about what types are in the go on the stack by default. And this would be stack by default. And this would be stack by default. And this would be things like integer, double, decimal, things like integer, double, decimal, things like integer, double, decimal, bool, strrus, enumes, and here's a fun bool, strrus, enumes, and here's a fun bool, strrus, enumes, and here's a fun one, date time, because date time is one, date time, because date time is one, date time, because date time is actually a strruct. So these are things actually a strruct. So these are things actually a strruct. So these are things that would live on the stack. These are that would live on the stack. These are that would live on the stack. These are value types and so they live in the value types and so they live in the value types and so they live in the stack. That means when you put an stack. That means when you put an stack. That means when you put an integer and we're going to see this in integer and we're going to see this in integer and we're going to see this in code in just a minute, but if you put an code in just a minute, but if you put an code in just a minute, but if you put an integer in a method, it's going to be on integer in a method, it's going to be on integer in a method, it's going to be on that method's frame. And then when that that method's frame. And then when that that method's frame. And then when that method gets closed out, that frame gets method gets closed out, that frame gets method gets closed out, that frame gets destroyed along with the integer. And if destroyed along with the integer. And if destroyed along with the integer. And if you pass that integer to another frame, you pass that integer to another frame, you pass that integer to another frame, you're actually making a copy of it. And
-
you're actually making a copy of it. And you're actually making a copy of it. And then you have two different copies and then you have two different copies and then you have two different copies and each copy will live inside of its each copy will live inside of its each copy will live inside of its specific frame. So same with double, specific frame. So same with double, specific frame. So same with double, decimal, bool, etc. decimal, bool, etc. decimal, bool, etc. So that's the stack. So what is what So that's the stack. So what is what So that's the stack. So what is what goes in the heap? Well, objects. If it's goes in the heap? Well, objects. If it's goes in the heap? Well, objects. If it's an object, it goes on the heap. Now you an object, it goes on the heap. Now you an object, it goes on the heap. Now you might think, well, everything derives might think, well, everything derives might think, well, everything derives from object. Yes, but there's a value from object. Yes, but there's a value from object. Yes, but there's a value object, a value type, and that's where object, a value type, and that's where object, a value type, and that's where the the um the value types come from. the the um the value types come from. the the um the value types come from. But if you cast something to an object, But if you cast something to an object, But if you cast something to an object, we're going to see that in a minute, but we're going to see that in a minute, but we're going to see that in a minute, but if you cast something to an object, it if you cast something to an object, it if you cast something to an object, it goes in the heap. Arrays live in the goes in the heap. Arrays live in the goes in the heap. Arrays live in the heap. Class instances live in the heap. heap. Class instances live in the heap. heap. Class instances live in the heap. And here's a fun one. Strings are And here's a fun one. Strings are And here's a fun one. Strings are actually living on the heap. So, we're actually living on the heap. So, we're actually living on the heap. So, we're going to see the differences here and going to see the differences here and going to see the differences here and when to use each, but I want you to kind when to use each, but I want you to kind when to use each, but I want you to kind of have this picture in your mind first of have this picture in your mind first of have this picture in your mind first of the uh the theory behind stack versus of the uh the theory behind stack versus of the uh the theory behind stack versus heap. Let's see how it plays out in C heap. Let's see how it plays out in C heap. Let's see how it plays out in C code. So here I have a very basic code. So here I have a very basic code. So here I have a very basic console application. I will give you the console application. I will give you the console application. I will give you the the source code for this down in the the source code for this down in the the source code for this down in the description. There's a link, but really description. There's a link, but really description. There's a link, but really it's just basic C# code, right? So we're it's just basic C# code, right? So we're it's just basic C# code, right? So we're going to talk through various things going to talk through various things going to talk through various things where they live and why. So let's start where they live and why. So let's start where they live and why. So let's start off with create a couple integers. Int off with create a couple integers. Int off with create a couple integers. Int a= a= a= 3 and int b equals 4. Okay. Now these 3 and int b equals 4. Okay. Now these 3 and int b equals 4. Okay. Now these two objects right here these two are two objects right here these two are two objects right here these two are integers right here live in this frame.
-
integers right here live in this frame. integers right here live in this frame. So a frame was created for main. These So a frame was created for main. These So a frame was created for main. These two items live in that memory space. So two items live in that memory space. So two items live in that memory space. So integer A and integer B live on the main integer A and integer B live on the main integer A and integer B live on the main frame the the first frame uh that gets frame the the first frame uh that gets frame the the first frame uh that gets created. Now if we call add and say a created. Now if we call add and say a created. Now if we call add and say a and b pass those two values in they're and b pass those two values in they're and b pass those two values in they're integers they're value types. Therefore, integers they're value types. Therefore, integers they're value types. Therefore, we make a copy of these and that copy we make a copy of these and that copy we make a copy of these and that copy goes down in here into this method. goes down in here into this method. goes down in here into this method. Which means that now X and Y get the Which means that now X and Y get the Which means that now X and Y get the values of four and three. Which means we values of four and three. Which means we values of four and three. Which means we now have these two items living in the now have these two items living in the now have these two items living in the frame that's for main. We have a copy of frame that's for main. We have a copy of frame that's for main. We have a copy of those a different ones that live in the those a different ones that live in the those a different ones that live in the frame for the add method. And then we frame for the add method. And then we frame for the add method. And then we actually get a third integer that gets actually get a third integer that gets actually get a third integer that gets created in this frame called results. created in this frame called results. created in this frame called results. Now when this method is done, the frame Now when this method is done, the frame Now when this method is done, the frame that was created for this ad method gets that was created for this ad method gets that was created for this ad method gets destroyed. So all of this the stuff that destroyed. So all of this the stuff that destroyed. So all of this the stuff that was stored in memory that were value was stored in memory that were value was stored in memory that were value types for this particular method have types for this particular method have types for this particular method have been then destroyed. It's very cheap been then destroyed. It's very cheap been then destroyed. It's very cheap because you make a copy, you use it, you because you make a copy, you use it, you because you make a copy, you use it, you destroy it. Very cheap. That is the destroy it. Very cheap. That is the destroy it. Very cheap. That is the stack. Now let's look at what might be a stack. Now let's look at what might be a stack. Now let's look at what might be a a value or I'm sorry a a heap item. So a value or I'm sorry a a heap item. So a value or I'm sorry a a heap item. So let's go with uh person model. We'll say
-
let's go with uh person model. We'll say let's go with uh person model. We'll say person model p equals new and then we'll person model p equals new and then we'll person model p equals new and then we'll say age equals say age equals say age equals uh five. And I'm going to create the the uh five. And I'm going to create the the uh five. And I'm going to create the the address as well. So new and we'll say address as well. So new and we'll say address as well. So new and we'll say that that that uh new address model and uh new address model and uh new address model and then we'll say in there um people then we'll say in there um people then we'll say in there um people address equals address equals address equals two and street address equals uh 1 2 3 two and street address equals uh 1 2 3 two and street address equals uh 1 2 3 mockingb bird. mockingb bird. mockingb bird. Okay, so let's unpin this real quick. Okay, so let's unpin this real quick. Okay, so let's unpin this real quick. That's a long declaration. I could That's a long declaration. I could That's a long declaration. I could probably do it in um two, but in fact, probably do it in um two, but in fact, probably do it in um two, but in fact, you know what? Let's just do that. It's you know what? Let's just do that. It's you know what? Let's just do that. It's It's too long. It's too long. It's too long. So, we're going to say um right here So, we're going to say um right here So, we're going to say um right here address model address model address model A equals A equals A equals and then we'll Whoops, I can't say a and then we'll Whoops, I can't say a and then we'll Whoops, I can't say a address. How about that?
-
address. How about that? address. How about that? Okay. and then we'll put address in here Okay. and then we'll put address in here Okay. and then we'll put address in here because this kind of gives us a a clearer picture. Now what I've done a a clearer picture. Now what I've done is created two different things on on is created two different things on on is created two different things on on the heap and so address model gets the heap and so address model gets the heap and so address model gets created on the heap and in it notice we created on the heap and in it notice we created on the heap and in it notice we have an integer type and we have a have an integer type and we have a have an integer type and we have a string type. So where does this integer string type. So where does this integer string type. So where does this integer live? You might say well it's a value live? You might say well it's a value live? You might say well it's a value type therefore gets put into a frame. type therefore gets put into a frame. type therefore gets put into a frame. No, it doesn't because it gets created No, it doesn't because it gets created No, it doesn't because it gets created as part of an instance of address model as part of an instance of address model as part of an instance of address model which is an item that goes on the heap. which is an item that goes on the heap. which is an item that goes on the heap. Therefore, this integer gets created and Therefore, this integer gets created and Therefore, this integer gets created and put in the memory space that holds this put in the memory space that holds this put in the memory space that holds this entire item. So, the address, this entire item. So, the address, this entire item. So, the address, this specific address item gets a memory specific address item gets a memory specific address item gets a memory address that stores then an integer. And address that stores then an integer. And address that stores then an integer. And you might say, well, it stores three you might say, well, it stores three you might say, well, it stores three address too, right? Well, no, because address too, right? Well, no, because address too, right? Well, no, because this is actually going to be a address.
-
this is actually going to be a address. this is actually going to be a address. I'm sorry, a string. So, therefore, I'm sorry, a string. So, therefore, I'm sorry, a string. So, therefore, since it's a string, strings are also since it's a string, strings are also since it's a string, strings are also heap items. Therefore, it gets that heap items. Therefore, it gets that heap items. Therefore, it gets that string gets put in the heap separately. string gets put in the heap separately. string gets put in the heap separately. And then the reference to that string And then the reference to that string And then the reference to that string gets put into stream address. We're gets put into stream address. We're gets put into stream address. We're going to come back to strings in just a going to come back to strings in just a going to come back to strings in just a minute. But so now we have a a value minute. But so now we have a a value minute. But so now we have a a value being stored right in the instance of being stored right in the instance of being stored right in the instance of address and we have a string which is a address and we have a string which is a address and we have a string which is a reference in the address. Okay. Now that reference in the address. Okay. Now that reference in the address. Okay. Now that gets a little confusing here. I gets a little confusing here. I gets a little confusing here. I understand that. It gets even more understand that. It gets even more understand that. It gets even more confusing when you start putting the confusing when you start putting the confusing when you start putting the address into another person model address into another person model address into another person model because now this instance of address because now this instance of address because now this instance of address gets put into the reference of it gets gets put into the reference of it gets gets put into the reference of it gets put into this P instance of person put into this P instance of person put into this P instance of person model. But you can kind of see though model. But you can kind of see though model. But you can kind of see though why we have reference types versus value why we have reference types versus value why we have reference types versus value types because imagine for a minute that types because imagine for a minute that types because imagine for a minute that this address right here actually store this address right here actually store this address right here actually store the value of address. Well, that's the value of address. Well, that's the value of address. Well, that's great. But what if I created another great. But what if I created another great. But what if I created another person? Person person uh P2 equals new person? Person person uh P2 equals new person? Person person uh P2 equals new and I said, you know, age equals 6 and and I said, you know, age equals 6 and and I said, you know, age equals 6 and address equals address.
-
Well, then I would have two places this Well, then I would have two places this is stored. And if I made this a value is stored. And if I made this a value is stored. And if I made this a value type can't, but if I did, if C type can't, but if I did, if C type can't, but if I did, if C developers decide, you know what, or the developers decide, you know what, or the developers decide, you know what, or the designers say, you know what, um, we're designers say, you know what, um, we're designers say, you know what, um, we're gonna put make this a value type and gonna put make this a value type and gonna put make this a value type and make a copy of it like we do with the make a copy of it like we do with the make a copy of it like we do with the integers. Well, then we have a copy here integers. Well, then we have a copy here integers. Well, then we have a copy here and a copy here. But the problem is what and a copy here. But the problem is what and a copy here. But the problem is what if I changed then one, two, three if I changed then one, two, three if I changed then one, two, three mockingb bird to one, two, three mockingb bird to one, two, three mockingb bird to one, two, three mockingb bird street. I have to change mockingb bird street. I have to change mockingb bird street. I have to change it in two different places. I'd have to it in two different places. I'd have to it in two different places. I'd have to know that they're in two different know that they're in two different know that they're in two different places where right now if I were to places where right now if I were to places where right now if I were to change if I were to come down here and change if I were to come down here and change if I were to come down here and say address dot uh street address equals say address dot uh street address equals say address dot uh street address equals 1 2 3 mockingb bird lane. That's the 1 2 3 mockingb bird lane. That's the 1 2 3 mockingb bird lane. That's the that's the actual address. Well, right that's the actual address. Well, right that's the actual address. Well, right now because it's on the heap and these now because it's on the heap and these now because it's on the heap and these are references are references are references to that location, I can make a change to that location, I can make a change to that location, I can make a change right here and it will update P and P2.
-
right here and it will update P and P2. right here and it will update P and P2. So, this is where a a uh stack wouldn't So, this is where a a uh stack wouldn't So, this is where a a uh stack wouldn't really work because we're talking about really work because we're talking about really work because we're talking about one object, one one address object that one object, one one address object that one object, one one address object that I want to modify in multiple places. I I want to modify in multiple places. I I want to modify in multiple places. I can't do that if it's a value type can't do that if it's a value type can't do that if it's a value type that's going to be copied from one place that's going to be copied from one place that's going to be copied from one place to another. For example, if I were to to another. For example, if I were to to another. For example, if I were to say, you know, down here x= say, you know, down here x= say, you know, down here x= x + x + x + 1, that would still work, but it 1, that would still work, but it 1, that would still work, but it wouldn't modify a because a was passed wouldn't modify a because a was passed wouldn't modify a because a was passed into x. It wouldn't modify a because into x. It wouldn't modify a because into x. It wouldn't modify a because it's a copy of a's value. it's a copy of a's value. it's a copy of a's value. So So So this right here is modifying the actual this right here is modifying the actual this right here is modifying the actual object. And this is where object- object. And this is where object- object. And this is where object- oriented programming kind of comes in. oriented programming kind of comes in. oriented programming kind of comes in. When you store in like P or P2 or When you store in like P or P2 or When you store in like P or P2 or address, you're not storing the object address, you're not storing the object address, you're not storing the object itself. You're storing the reference to itself. You're storing the reference to itself. You're storing the reference to the location of the object. The way I the location of the object. The way I the location of the object. The way I explain this to students when in a C# explain this to students when in a C# explain this to students when in a C# master course is I say that you know the master course is I say that you know the master course is I say that you know the the address model this is like a a the address model this is like a a the address model this is like a a blueprint for a house and every time you blueprint for a house and every time you blueprint for a house and every time you create a new uh variable what you're create a new uh variable what you're create a new uh variable what you're doing is saying hey build the house and doing is saying hey build the house and doing is saying hey build the house and then put in this location put in this then put in this location put in this then put in this location put in this the address for that house. That way, if the address for that house. That way, if the address for that house. That way, if you pass that address around to multiple you pass that address around to multiple you pass that address around to multiple places, well, what you're doing is places, well, what you're doing is places, well, what you're doing is saying, "Hey, this house was built on
-
saying, "Hey, this house was built on saying, "Hey, this house was built on this location and then everyone knows this location and then everyone knows this location and then everyone knows what that house is." You're not making a what that house is." You're not making a what that house is." You're not making a copy of the house. You're not building a copy of the house. You're not building a copy of the house. You're not building a second house and a third house. You're second house and a third house. You're second house and a third house. You're just pointing to that same location and just pointing to that same location and just pointing to that same location and then everyone can know where that is then everyone can know where that is then everyone can know where that is without having to rebuild the whole without having to rebuild the whole without having to rebuild the whole house. That's what we're doing when we house. That's what we're doing when we house. That's what we're doing when we instantiate a an item. We're creating a instantiate a an item. We're creating a instantiate a an item. We're creating a reference here and in memory on the reference here and in memory on the reference here and in memory on the heap, we store the actual data that we heap, we store the actual data that we heap, we store the actual data that we need. Okay, need. Okay, need. Okay, that's a lot. And yes, that's kind of that's a lot. And yes, that's kind of that's a lot. And yes, that's kind of confusing. But why is this important? confusing. But why is this important? confusing. But why is this important? Well, I kind of explained a little bit Well, I kind of explained a little bit Well, I kind of explained a little bit already that it's, you know, we're already that it's, you know, we're already that it's, you know, we're passing value types around like passing value types around like passing value types around like integers. It's very easy to make copies. integers. It's very easy to make copies. integers. It's very easy to make copies. Go ahead and make a copy. Whatever it Go ahead and make a copy. Whatever it Go ahead and make a copy. Whatever it they're cheap, they're small. Uh, make a they're cheap, they're small. Uh, make a they're cheap, they're small. Uh, make a copy. have it only live the life of the copy. have it only live the life of the copy. have it only live the life of the frame. Good enough. Done. But we're frame. Good enough. Done. But we're frame. Good enough. Done. But we're creating objects. We're creating creating objects. We're creating creating objects. We're creating something where we need to have this be something where we need to have this be something where we need to have this be one object, not a copy and a copy and a one object, not a copy and a copy and a one object, not a copy and a copy and a copy. There's a lot of data that could copy. There's a lot of data that could copy. There's a lot of data that could potentially be in this address model.
-
potentially be in this address model. potentially be in this address model. And that'd be expensive to keep And that'd be expensive to keep And that'd be expensive to keep duplicating. Plus, if we made a change duplicating. Plus, if we made a change duplicating. Plus, if we made a change like this, that would be a problem like this, that would be a problem like this, that would be a problem because we'd only be changing one because we'd only be changing one because we'd only be changing one location, not every location that's location, not every location that's location, not every location that's using that. Therefore, we pass the using that. Therefore, we pass the using that. Therefore, we pass the reference around and store it once in reference around and store it once in reference around and store it once in memory. However, what that does is it memory. However, what that does is it memory. However, what that does is it says, hey, you know, in this now, this says, hey, you know, in this now, this says, hey, you know, in this now, this is a special case because main doesn't is a special case because main doesn't is a special case because main doesn't get done till the whole app is done. But get done till the whole app is done. But get done till the whole app is done. But pretend it's a different method. We pretend it's a different method. We pretend it's a different method. We don't know for certain that don't know for certain that don't know for certain that the the address model address gets the the address model address gets the the address model address gets destroyed by the time this is done destroyed by the time this is done destroyed by the time this is done again, if it wasn't main. So therefore, again, if it wasn't main. So therefore, again, if it wasn't main. So therefore, we have to figure out when that's we have to figure out when that's we have to figure out when that's destroyed in order to release that destroyed in order to release that destroyed in order to release that memory. We can't just keep a whole bunch memory. We can't just keep a whole bunch memory. We can't just keep a whole bunch of frames open because well, you might of frames open because well, you might of frames open because well, you might be needing that address, might be be needing that address, might be be needing that address, might be needing the uh the reference to P or P2, needing the uh the reference to P or P2, needing the uh the reference to P or P2, maybe pass it around to another method. maybe pass it around to another method. maybe pass it around to another method. So therefore we use the heap which So therefore we use the heap which So therefore we use the heap which allows us to destroy the memory we have allows us to destroy the memory we have allows us to destroy the memory we have used in the frame without used in the frame without used in the frame without disturbing the things that might live disturbing the things that might live disturbing the things that might live outside of the frame. So that's where we outside of the frame. So that's where we outside of the frame. So that's where we have stack versus heap. Okay. So there have stack versus heap. Okay. So there have stack versus heap. Okay. So there are a couple of tricky exceptions. Let's are a couple of tricky exceptions. Let's are a couple of tricky exceptions. Let's talk about them. Uh first of all talk about them. Uh first of all talk about them. Uh first of all when I say frame down here there is an when I say frame down here there is an when I say frame down here there is an exception. Okay. So when I say that this exception. Okay. So when I say that this exception. Okay. So when I say that this X and Y live in the AD frame, that might X and Y live in the AD frame, that might X and Y live in the AD frame, that might not be true. I love exceptions, right?
-
not be true. I love exceptions, right? not be true. I love exceptions, right? But here's what happens in the JIT, the But here's what happens in the JIT, the But here's what happens in the JIT, the just in time compiler. just in time compiler. just in time compiler. It might say, hey, you know what? It's It might say, hey, you know what? It's It might say, hey, you know what? It's actually more memory efficient or it's actually more memory efficient or it's actually more memory efficient or it's faster. Whatever reason, it's more faster. Whatever reason, it's more faster. Whatever reason, it's more efficient to actually combine this frame efficient to actually combine this frame efficient to actually combine this frame with say main or with another frame. And with say main or with another frame. And with say main or with another frame. And it might do that. it might actually um it might do that. it might actually um it might do that. it might actually um inline something and not have it call inline something and not have it call inline something and not have it call out to a method. So there are times when out to a method. So there are times when out to a method. So there are times when the the just in time compiler says, you the the just in time compiler says, you the the just in time compiler says, you know what, it's actually more efficient know what, it's actually more efficient know what, it's actually more efficient to do it this way. So that's not a to do it this way. So that's not a to do it this way. So that's not a problem. That's a good thing. Like it's problem. That's a good thing. Like it's problem. That's a good thing. Like it's it's making things more efficient. But it's making things more efficient. But it's making things more efficient. But it does mean when I say that this is it does mean when I say that this is it does mean when I say that this is creates a frame and gets the frame gets creates a frame and gets the frame gets creates a frame and gets the frame gets destroyed here is not always true. Put a destroyed here is not always true. Put a destroyed here is not always true. Put a little asterisk there. It's mostly true. little asterisk there. It's mostly true. little asterisk there. It's mostly true. Um, and I want to make sure we're we're Um, and I want to make sure we're we're Um, and I want to make sure we're we're as clear as possible. Okay. The other as clear as possible. Okay. The other as clear as possible. Okay. The other thing that we need to know about is the thing that we need to know about is the thing that we need to know about is the fact that strings are a bit tricky. So, fact that strings are a bit tricky. So, fact that strings are a bit tricky. So, let's let's talk about strings. Let's uh let's let's talk about strings. Let's uh let's let's talk about strings. Let's uh just comment out all of this because I just comment out all of this because I just comment out all of this because I want to make sure we're really clear want to make sure we're really clear want to make sure we're really clear about what we're talking about with about what we're talking about with about what we're talking about with strings here. So, let's say string strings here. So, let's say string strings here. So, let's say string string one. I know it's really creative string one. I know it's really creative string one. I know it's really creative naming there. U equals test. Okay. Now naming there. U equals test. Okay. Now naming there. U equals test. Okay. Now remember I said this lives in the heap.
-
remember I said this lives in the heap. remember I said this lives in the heap. The string lives it's actually a class The string lives it's actually a class The string lives it's actually a class and it lives in the heap. By the way, if and it lives in the heap. By the way, if and it lives in the heap. By the way, if we look at an integer, notice that it we look at an integer, notice that it we look at an integer, notice that it says integer um an integer is actually says integer um an integer is actually says integer um an integer is actually if you mouse over int, it's a strruct. if you mouse over int, it's a strruct. if you mouse over int, it's a strruct. Okay, remember I said strruct lives on Okay, remember I said strruct lives on Okay, remember I said strruct lives on the stack. So string is a class. It the stack. So string is a class. It the stack. So string is a class. It lives on the heap. You might say, well lives on the heap. You might say, well lives on the heap. You might say, well why? Because it seems pretty simple. why? Because it seems pretty simple. why? Because it seems pretty simple. Well, the reason why is because a Well, the reason why is because a Well, the reason why is because a string, yeah, it could be four string, yeah, it could be four string, yeah, it could be four characters or it could be a million characters or it could be a million characters or it could be a million characters. And let's just say it's a characters. And let's just say it's a characters. And let's just say it's a million characters. Imagine if you million characters. Imagine if you million characters. Imagine if you wanted to copy a string around and we do wanted to copy a string around and we do wanted to copy a string around and we do that a lot. Well, that could be very that a lot. Well, that could be very that a lot. Well, that could be very very expensive. So, a string is actually very expensive. So, a string is actually very expensive. So, a string is actually on the heap. So, it uses a reference. on the heap. So, it uses a reference. on the heap. So, it uses a reference. So, actually string one holds the So, actually string one holds the So, actually string one holds the reference to this actual string. which reference to this actual string. which reference to this actual string. which means that string string 2 means that string string 2 means that string string 2 equals string 1 like so what I did is I equals string 1 like so what I did is I equals string 1 like so what I did is I copied the reference from string one I copied the reference from string one I copied the reference from string one I copied that into string two. So now we copied that into string two. So now we copied that into string two. So now we have the same reference in two different have the same reference in two different have the same reference in two different variables and they both contain they variables and they both contain they variables and they both contain they both point to the same address or both point to the same address or both point to the same address or location in memory that stores the word location in memory that stores the word location in memory that stores the word test.
-
test. test. However, However, However, this is where things get a little this is where things get a little this is where things get a little trickier because imagine for a minute trickier because imagine for a minute trickier because imagine for a minute that I said, "Okay, I've got that right that I said, "Okay, I've got that right that I said, "Okay, I've got that right there." And I say string one equals uh there." And I say string one equals uh there." And I say string one equals uh another test. another test. another test. Now up here in person model in P2, we Now up here in person model in P2, we Now up here in person model in P2, we changed this address and it changed for changed this address and it changed for changed this address and it changed for both of them. So what is the value now both of them. So what is the value now both of them. So what is the value now of string two which holds the same of string two which holds the same of string two which holds the same memory location as string one? What do memory location as string one? What do memory location as string one? What do you think the value of string two would you think the value of string two would you think the value of string two would be? Well, if you've ever done it before be? Well, if you've ever done it before be? Well, if you've ever done it before in in code, string two says test. String in in code, string two says test. String in in code, string two says test. String one, even though down here we modify to one, even though down here we modify to one, even though down here we modify to say another test, what happened was that say another test, what happened was that say another test, what happened was that strings are immutable, meaning they strings are immutable, meaning they strings are immutable, meaning they cannot be changed. cannot be changed. cannot be changed. So what happened is that we assigned a So what happened is that we assigned a So what happened is that we assigned a new reference to string one. We put this new reference to string one. We put this new reference to string one. We put this in a different place in the heap and in a different place in the heap and in a different place in the heap and assigned this to string one, but string assigned this to string one, but string assigned this to string one, but string two still has the first reference.
-
two still has the first reference. two still has the first reference. You might ask yourself, why would they You might ask yourself, why would they You might ask yourself, why would they do that? Why wouldn't they do the same do that? Why wouldn't they do the same do that? Why wouldn't they do the same thing we do with classes? Well, the thing we do with classes? Well, the thing we do with classes? Well, the reason why is because strings are a bit reason why is because strings are a bit reason why is because strings are a bit different. And so we wouldn't different. And so we wouldn't different. And so we wouldn't necessarily expect that string two would necessarily expect that string two would necessarily expect that string two would become the same as string one even become the same as string one even become the same as string one even though they have sign of values but also though they have sign of values but also though they have sign of values but also because this actually is an efficiency because this actually is an efficiency because this actually is an efficiency thing believe it or not because yes you thing believe it or not because yes you thing believe it or not because yes you probably have heard that strings are probably have heard that strings are probably have heard that strings are inefficient and very careful how you do inefficient and very careful how you do inefficient and very careful how you do things like don't concatenate strings in things like don't concatenate strings in things like don't concatenate strings in a loop with a million loops right don't a loop with a million loops right don't a loop with a million loops right don't do that that'd be very expensive you do that that'd be very expensive you do that that'd be very expensive you might say well you know why would I use might say well you know why would I use might say well you know why would I use value types then well value types then well value types then well string strings are very very efficient. string strings are very very efficient. string strings are very very efficient. It's just that they take up a lot of It's just that they take up a lot of It's just that they take up a lot of memory no matter what. And so what memory no matter what. And so what memory no matter what. And so what happens here is that this string might happens here is that this string might happens here is that this string might reference test and string two references reference test and string two references reference test and string two references test. And in fact, if I were to say this test. And in fact, if I were to say this test. And in fact, if I were to say this string string three, I know these names string string three, I know these names string string three, I know these names are great. Equals test. Guess what?
-
are great. Equals test. Guess what? are great. Equals test. Guess what? I didn't necessarily and probably I didn't necessarily and probably I didn't necessarily and probably didn't. I did not create a new memory didn't. I did not create a new memory didn't. I did not create a new memory location for this value called test. location for this value called test. location for this value called test. What happened is the the se the uh the What happened is the the se the uh the What happened is the the se the uh the JIT will say hey you know what we JIT will say hey you know what we JIT will say hey you know what we already have a value in memory that already have a value in memory that already have a value in memory that stores the string test. So I'll just stores the string test. So I'll just stores the string test. So I'll just point string three at the same place I'm point string three at the same place I'm point string three at the same place I'm pointing string one because they're pointing string one because they're pointing string one because they're immutable meaning they cannot be immutable meaning they cannot be immutable meaning they cannot be changed. changed. changed. So that means that instead of using two So that means that instead of using two So that means that instead of using two different memory locations, I'm using different memory locations, I'm using different memory locations, I'm using one. I'm saving some memory because of one. I'm saving some memory because of one. I'm saving some memory because of the fact that we have two things in the fact that we have two things in the fact that we have two things in memory that have the same value. memory that have the same value. memory that have the same value. Therefore, we'll create the same Therefore, we'll create the same Therefore, we'll create the same reference, the same location, and call reference, the same location, and call reference, the same location, and call it good. But because it's immutable, I it good. But because it's immutable, I it good. But because it's immutable, I don't have to worry if I were to say, don't have to worry if I were to say, don't have to worry if I were to say, you know what, string well, like we you know what, string well, like we you know what, string well, like we already got right down here. I change already got right down here. I change already got right down here. I change string one to another test. That's not string one to another test. That's not string one to another test. That's not going to affect string three because all going to affect string three because all going to affect string three because all it's going to do is say, "Oh, we need to it's going to do is say, "Oh, we need to it's going to do is say, "Oh, we need to create a new memory location that stores create a new memory location that stores create a new memory location that stores the string another test." And if there's the string another test." And if there's the string another test." And if there's already another memory location already another memory location already another memory location somewhere else already storing that somewhere else already storing that somewhere else already storing that value, it's just going to point to that value, it's just going to point to that value, it's just going to point to that location. So location. So location. So yes, strings are more complicated. And yes, strings are more complicated. And yes, strings are more complicated. And if if you actually do something like if if you actually do something like if if you actually do something like this, uh if um string 1 equals string this, uh if um string 1 equals string this, uh if um string 1 equals string two, two, two, you know, say something like
-
you know, say something like you know, say something like they are equal, right? Now, right now they are equal, right? Now, right now they are equal, right? Now, right now they're not because they change string they're not because they change string they're not because they change string one. But this compares the values, not one. But this compares the values, not one. But this compares the values, not the reference location. Whereas if I the reference location. Whereas if I the reference location. Whereas if I were to say, let's um let's uncomment were to say, let's um let's uncomment were to say, let's um let's uncomment this stuff here. If I were to come down this stuff here. If I were to come down this stuff here. If I were to come down here and say if here and say if here and say if A or P equals P2, A or P equals P2, A or P equals P2, that's not going to compare the values. that's not going to compare the values. that's not going to compare the values. It's going to compare the reference It's going to compare the reference It's going to compare the reference location. location. location. Okay? So, if I were to say P.RUD address Okay? So, if I were to say P.RUD address Okay? So, if I were to say P.RUD address and P2 and P2 and P2 address. This would actually be true address. This would actually be true address. This would actually be true because they both use the same reference because they both use the same reference because they both use the same reference location for the instance of address but location for the instance of address but location for the instance of address but it's comparing the reference locations. it's comparing the reference locations. it's comparing the reference locations. This is not this is comparing the This is not this is comparing the This is not this is comparing the values. values. values. So yes, strings act a bit differently So yes, strings act a bit differently So yes, strings act a bit differently but they did that intentionally because but they did that intentionally because but they did that intentionally because of the fact that you would not think oh of the fact that you would not think oh of the fact that you would not think oh string is a class therefore I have to string is a class therefore I have to string is a class therefore I have to say string value. Nope. They overrode say string value. Nope. They overrode say string value. Nope. They overrode what the equals does and you compare what the equals does and you compare what the equals does and you compare values.
-
values. values. So, So, So, with all that being said, with all that being said, with all that being said, if we come back and look at our our if we come back and look at our our if we come back and look at our our stack versus heap, the big thing to stack versus heap, the big thing to stack versus heap, the big thing to understand is the stack is short-lived. understand is the stack is short-lived. understand is the stack is short-lived. It gets destroyed when the the frame is It gets destroyed when the the frame is It gets destroyed when the the frame is closed and it uses value types. value closed and it uses value types. value closed and it uses value types. value types are stored in the stack. types are stored in the stack. types are stored in the stack. For everything else, they're longer For everything else, they're longer For everything else, they're longer lived. Potentially, lived. Potentially, lived. Potentially, they live at least until there's no they live at least until there's no they live at least until there's no longer a reachable reference to them in longer a reachable reference to them in longer a reachable reference to them in the code. So, what that means is that the code. So, what that means is that the code. So, what that means is that even though even though even though let's just say I didn't do this down let's just say I didn't do this down let's just say I didn't do this down here. here. here. Okay? So, I have this main method. I Okay? So, I have this main method. I Okay? So, I have this main method. I create P and P2 but I never use them. create P and P2 but I never use them. create P and P2 but I never use them. Okay. Now it says unnecessary Okay. Now it says unnecessary Okay. Now it says unnecessary assignment. Let's just say for a minute assignment. Let's just say for a minute assignment. Let's just say for a minute that I did. Let's just say that console that I did. Let's just say that console that I did. Let's just say that console write line P.H.
-
It's going to print out the H. Okay. So It's going to print out the H. Okay. So now I've used the the P reference. So now I've used the the P reference. So now I've used the the P reference. So when can when can when can this reference go out of scope out of this reference go out of scope out of this reference go out of scope out of memory? When can be destroyed in the memory? When can be destroyed in the memory? When can be destroyed in the memory? Well, for P, it can be destroyed memory? Well, for P, it can be destroyed memory? Well, for P, it can be destroyed any time after line 18. For P2, it can any time after line 18. For P2, it can any time after line 18. For P2, it can actually be destroyed right away because actually be destroyed right away because actually be destroyed right away because there's never a use of it. So it can there's never a use of it. So it can there's never a use of it. So it can actually be destroyed like like the the actually be destroyed like like the the actually be destroyed like like the the compiler when it compiles this down compiler when it compiles this down compiler when it compiles this down actually runs it can go, hey, you know actually runs it can go, hey, you know actually runs it can go, hey, you know what? This is an unnecessary assignment what? This is an unnecessary assignment what? This is an unnecessary assignment to the value of P2 because it's never to the value of P2 because it's never to the value of P2 because it's never actually used. Therefore, let's not even actually used. Therefore, let's not even actually used. Therefore, let's not even create it. Okay, but for this for P, it create it. Okay, but for this for P, it create it. Okay, but for this for P, it created it used it right down here. But created it used it right down here. But created it used it right down here. But even though we're still in the same even though we're still in the same even though we're still in the same frame, even though we're in the main's frame, even though we're in the main's frame, even though we're in the main's frame, um not main frame, but the frame frame, um not main frame, but the frame frame, um not main frame, but the frame for main, for main, for main, this this P uh variable this this P uh variable this this P uh variable can be destroyed.
-
can be destroyed. can be destroyed. Well, the I'm sorry, the reference Well, the I'm sorry, the reference Well, the I'm sorry, the reference variable. So, it it's confusing. P variable. So, it it's confusing. P variable. So, it it's confusing. P actually gets stored on the stack. Okay? actually gets stored on the stack. Okay? actually gets stored on the stack. Okay? Because it's a reference. That reference Because it's a reference. That reference Because it's a reference. That reference gets stored on the stack. It refers to gets stored on the stack. It refers to gets stored on the stack. It refers to an item in the heap. That item in the an item in the heap. That item in the an item in the heap. That item in the heap can get destroyed after line 18. heap can get destroyed after line 18. heap can get destroyed after line 18. Now, is it? Maybe, maybe not. We can Now, is it? Maybe, maybe not. We can Now, is it? Maybe, maybe not. We can talk about that next week when it comes talk about that next week when it comes talk about that next week when it comes to garbage collection. But it can be to garbage collection. But it can be to garbage collection. But it can be destroyed anytime after line 18 because destroyed anytime after line 18 because destroyed anytime after line 18 because it's no longer referenced anywhere in my it's no longer referenced anywhere in my it's no longer referenced anywhere in my application. application. application. So when we talk about this integer, well So when we talk about this integer, well So when we talk about this integer, well they get destroyed naturally when the they get destroyed naturally when the they get destroyed naturally when the frame gets destroyed. When is the frame frame gets destroyed. When is the frame frame gets destroyed. When is the frame destroyed? When the method is over. So destroyed? When the method is over. So destroyed? When the method is over. So this X versus Y, even though we're not this X versus Y, even though we're not this X versus Y, even though we're not using X and Y anymore after line 39, it using X and Y anymore after line 39, it using X and Y anymore after line 39, it won't get destroyed before the frame is won't get destroyed before the frame is won't get destroyed before the frame is done. And the frame is done at the end done. And the frame is done at the end done. And the frame is done at the end here. Now, here. Now, here. Now, one last thing to be careful of when it one last thing to be careful of when it one last thing to be careful of when it comes to and we're going to see this comes to and we're going to see this comes to and we're going to see this more in the next video on garbage more in the next video on garbage more in the next video on garbage collection. But when you run your collection. But when you run your collection. But when you run your application in debug mode, a lot of application in debug mode, a lot of application in debug mode, a lot of these things go out the window. And these things go out the window. And these things go out the window. And again, this is more relevant when we're again, this is more relevant when we're again, this is more relevant when we're learning about garbage collection, but I learning about garbage collection, but I learning about garbage collection, but I want you to understand that debug mode want you to understand that debug mode want you to understand that debug mode holds things in memory longer than they holds things in memory longer than they holds things in memory longer than they normally would in a running application.
-
normally would in a running application. normally would in a running application. And the reason for that is so that you And the reason for that is so that you And the reason for that is so that you can debug things. Because if let's just can debug things. Because if let's just can debug things. Because if let's just say you wanted to understand what the say you wanted to understand what the say you wanted to understand what the what the instance of P was, but you put what the instance of P was, but you put what the instance of P was, but you put the break point here, well P might be the break point here, well P might be the break point here, well P might be gone by then. That would be a problem. gone by then. That would be a problem. gone by then. That would be a problem. So even though So even though So even though it could be destroyed as of line 19 it could be destroyed as of line 19 it could be destroyed as of line 19 essentially after the line 18, it's not essentially after the line 18, it's not essentially after the line 18, it's not going to be if you're in debug mode. going to be if you're in debug mode. going to be if you're in debug mode. Which is why whenever you talk about Which is why whenever you talk about Which is why whenever you talk about doing things like benchmarking with doing things like benchmarking with doing things like benchmarking with benchmark.net net or or trying to benchmark.net net or or trying to benchmark.net net or or trying to understand how fast your application is understand how fast your application is understand how fast your application is or how much memory it uses, don't use or how much memory it uses, don't use or how much memory it uses, don't use debug mode because there's a lot of debug mode because there's a lot of debug mode because there's a lot of extra stuff going on that's not extra stuff going on that's not extra stuff going on that's not efficient and it's not supposed to be. efficient and it's not supposed to be. efficient and it's not supposed to be. It's supposed to be more efficient for It's supposed to be more efficient for It's supposed to be more efficient for debugging, not for determining how much debugging, not for determining how much debugging, not for determining how much memory your application uses. Okay. So memory your application uses. Okay. So memory your application uses. Okay. So with that understanding the stack versus with that understanding the stack versus with that understanding the stack versus the heap is more in some ways as the heap is more in some ways as the heap is more in some ways as theoretical but in some ways is really theoretical but in some ways is really theoretical but in some ways is really really practical. Okay. So it's really practical. Okay. So it's really practical. Okay. So it's theoretical in the fact that well yeah theoretical in the fact that well yeah theoretical in the fact that well yeah stack versus heap these things live on a stack versus heap these things live on a stack versus heap these things live on a stack um except when they're inside of stack um except when they're inside of stack um except when they're inside of objects which live on the heap. Um so objects which live on the heap. Um so objects which live on the heap. Um so yeah there's a a bit of um you know yeah there's a a bit of um you know yeah there's a a bit of um you know ambiguity there a little bit but ambiguity there a little bit but ambiguity there a little bit but understanding okay when I create this understanding okay when I create this understanding okay when I create this object here's its lifespan in the memory
-
object here's its lifespan in the memory object here's its lifespan in the memory that's important understanding that that's important understanding that that's important understanding that memory is important to at least memory is important to at least memory is important to at least understand how it's used even though a understand how it's used even though a understand how it's used even though a lot of the work is done for us is also lot of the work is done for us is also lot of the work is done for us is also important. So whenever you create a important. So whenever you create a important. So whenever you create a class instance know it's going to get class instance know it's going to get class instance know it's going to get put on the heap. Whenever you use a put on the heap. Whenever you use a put on the heap. Whenever you use a double it's probably going to be put on double it's probably going to be put on double it's probably going to be put on the stack and that can be very very the stack and that can be very very the stack and that can be very very different in how we think about its different in how we think about its different in how we think about its usage. Now I do want to point out kind usage. Now I do want to point out kind usage. Now I do want to point out kind of one of the things right here it says of one of the things right here it says of one of the things right here it says arrays. This is important to understand. arrays. This is important to understand. arrays. This is important to understand. So one last kind of thing here. If we So one last kind of thing here. If we So one last kind of thing here. If we create an array, let's just create an create an array, let's just create an create an array, let's just create an array um right down here. Now, we're array um right down here. Now, we're array um right down here. Now, we're gonna say um an integer array. gonna say um an integer array. gonna say um an integer array. I don't know why it did that, but I don't know why it did that, but I don't know why it did that, but there we go. So, I create an integer there we go. So, I create an integer there we go. So, I create an integer array. I'm going to say um this is just array. I'm going to say um this is just array. I'm going to say um this is just numbers numbers numbers equals, and we're gonna put numbers in equals, and we're gonna put numbers in equals, and we're gonna put numbers in here.
-
That's good enough. Okay. So an array That's good enough. Okay. So an array goes in the heap. So this number goes in the heap. So this number goes in the heap. So this number is actually going to be on the heap. is actually going to be on the heap. is actually going to be on the heap. It's a reference to a heap item which It's a reference to a heap item which It's a reference to a heap item which stores an array of numbers. But these stores an array of numbers. But these stores an array of numbers. But these are all integers are all integers are all integers which means that in the heap there's one which means that in the heap there's one which means that in the heap there's one object. Let's let's kind of um let's object. Let's let's kind of um let's object. Let's let's kind of um let's let's define it this way. Let's see if I let's define it this way. Let's see if I let's define it this way. Let's see if I can draw here. Um so on the heap and can draw here. Um so on the heap and can draw here. Um so on the heap and this is the what's called the heap right this is the what's called the heap right this is the what's called the heap right so take one object and in here that's so take one object and in here that's so take one object and in here that's one object in the heap and this is going one object in the heap and this is going one object in the heap and this is going to be the uh the numbers to be the uh the numbers to be the uh the numbers uh reference I'm not going to you know uh reference I'm not going to you know uh reference I'm not going to you know drawing my mouse is not great but this drawing my mouse is not great but this drawing my mouse is not great but this is the reference to to the the numbers is the reference to to the the numbers is the reference to to the the numbers and in here is three five and in here is three five and in here is three five seven etc. Okay, these values in one seven etc. Okay, these values in one seven etc. Okay, these values in one item. Okay, one item, one object in the item. Okay, one item, one object in the item. Okay, one item, one object in the heap is going to store all those values.
-
heap is going to store all those values. heap is going to store all those values. So even though this numbers is a So even though this numbers is a So even though this numbers is a reference to a heap item, it's going to reference to a heap item, it's going to reference to a heap item, it's going to store all the numbers in one location. store all the numbers in one location. store all the numbers in one location. Now, um if we were to instead Now, um if we were to instead Now, um if we were to instead do something like this where we said uh do something like this where we said uh do something like this where we said uh uh person model array and call it uh uh person model array and call it uh uh person model array and call it uh I don't know more people I don't know more people I don't know more people and say equals and go P and P2 like so. and say equals and go P and P2 like so. and say equals and go P and P2 like so. Well, this is a array item. So Well, this is a array item. So Well, this is a array item. So therefore, it's a reference to an item therefore, it's a reference to an item therefore, it's a reference to an item on the heap. But this is a reference and on the heap. But this is a reference and on the heap. But this is a reference and this is a reference. So when we talk this is a reference. So when we talk this is a reference. So when we talk about Oops, not the timer. There we go. about Oops, not the timer. There we go. about Oops, not the timer. There we go. Uh when we talk about our heap again, Uh when we talk about our heap again, Uh when we talk about our heap again, we have a uh more people we have a uh more people we have a uh more people item.
-
item. item. And then we have a P item. And we have a And then we have a P item. And we have a And then we have a P item. And we have a P2 item. P2 item. P2 item. And we have an item right here that And we have an item right here that And we have an item right here that points to P. We have an item right here points to P. We have an item right here points to P. We have an item right here that points to P2. So this one has three that points to P2. So this one has three that points to P2. So this one has three different things in memory on the heap. different things in memory on the heap. different things in memory on the heap. Okay? Because each of these is a heap Okay? Because each of these is a heap Okay? Because each of these is a heap item and they've got an array of them item and they've got an array of them item and they've got an array of them which is also a heap item. whereas this which is also a heap item. whereas this which is also a heap item. whereas this one right here is a heap item, but it one right here is a heap item, but it one right here is a heap item, but it stores all the value types in that same stores all the value types in that same stores all the value types in that same object. So just a little bit of clarity object. So just a little bit of clarity object. So just a little bit of clarity there on on the differences. So there on on the differences. So there on on the differences. So yes, there's a lot to understand. Yes, yes, there's a lot to understand. Yes, yes, there's a lot to understand. Yes, it's a little bit more theory than you it's a little bit more theory than you it's a little bit more theory than you probably are used to, but it's good to probably are used to, but it's good to probably are used to, but it's good to understand what's happening here with understand what's happening here with understand what's happening here with heap versus stack. If no other reason, heap versus stack. If no other reason, heap versus stack. If no other reason, it's great for in your interview process it's great for in your interview process it's great for in your interview process because you'll probably talking about because you'll probably talking about because you'll probably talking about heat versus stack. But understanding the heat versus stack. But understanding the heat versus stack. But understanding the differences helps you understand better differences helps you understand better differences helps you understand better what's going on with your memory usage.
-
what's going on with your memory usage. what's going on with your memory usage. And you can't really diagnose your And you can't really diagnose your And you can't really diagnose your memory usage without understanding first memory usage without understanding first memory usage without understanding first where you're putting things in memory. where you're putting things in memory. where you're putting things in memory. And unlike C or some other lower level And unlike C or some other lower level And unlike C or some other lower level languages, we're not actually talking languages, we're not actually talking languages, we're not actually talking about memory addresses and freeing up about memory addresses and freeing up about memory addresses and freeing up memory and all the rest. In fact, a memory and all the rest. In fact, a memory and all the rest. In fact, a little spoiler for next week when we little spoiler for next week when we little spoiler for next week when we talk garbage collection. talk garbage collection. talk garbage collection. You don't touch garbage collection. You You don't touch garbage collection. You You don't touch garbage collection. You don't free up memory yourself. Don't do don't free up memory yourself. Don't do don't free up memory yourself. Don't do it. Stop. Don't do it. Okay? We'll talk it. Stop. Don't do it. Okay? We'll talk it. Stop. Don't do it. Okay? We'll talk about why in a little bit in next week's about why in a little bit in next week's about why in a little bit in next week's video. Um, but we just don't deal with a video. Um, but we just don't deal with a video. Um, but we just don't deal with a lot of that stuff. We don't want to do lot of that stuff. We don't want to do lot of that stuff. We don't want to do things manually. Instead, we want to things manually. Instead, we want to things manually. Instead, we want to write good code that can be easily write good code that can be easily write good code that can be easily collected or not even go to garbage collected or not even go to garbage collected or not even go to garbage collection because we're putting in the collection because we're putting in the collection because we're putting in the stack versus the heap. Now, one thing stack versus the heap. Now, one thing stack versus the heap. Now, one thing people try to do every once in a while people try to do every once in a while people try to do every once in a while is say, well, I'm just going to put it is say, well, I'm just going to put it is say, well, I'm just going to put it in strrus then because strrus are are in strrus then because strrus are are in strrus then because strrus are are value types. Therefore, they go on the value types. Therefore, they go on the value types. Therefore, they go on the stack. Therefore, I don't have the stack. Therefore, I don't have the stack. Therefore, I don't have the penalty of dealing with the heap. But penalty of dealing with the heap. But penalty of dealing with the heap. But that's not how this works. We this very that's not how this works. We this very that's not how this works. We this very clearly needs to be a val or a heap item clearly needs to be a val or a heap item clearly needs to be a val or a heap item a a reference type because otherwise a a reference type because otherwise a a reference type because otherwise we're making copies again of this we're making copies again of this we're making copies again of this address and they're causing all kinds of address and they're causing all kinds of address and they're causing all kinds of problems when you do stuff like this. So problems when you do stuff like this. So problems when you do stuff like this. So a strruct is not something that just is a strruct is not something that just is a strruct is not something that just is a cheaper class. That's not how the two a cheaper class. That's not how the two a cheaper class. That's not how the two work. A strruct is more for storing work. A strruct is more for storing work. A strruct is more for storing something that is a a single value. So
-
something that is a a single value. So something that is a a single value. So even if it has multiple values in it, even if it has multiple values in it, even if it has multiple values in it, it's a single value. For example, if it's a single value. For example, if it's a single value. For example, if you're storing um maybe coordinates, you you're storing um maybe coordinates, you you're storing um maybe coordinates, you might say this is this is the might say this is this is the might say this is this is the coordinates. Well, that might be two uh coordinates. Well, that might be two uh coordinates. Well, that might be two uh longs. So you put that in a strct and longs. So you put that in a strct and longs. So you put that in a strct and say coordinate. This is the strct say coordinate. This is the strct say coordinate. This is the strct coordinate. Okay, so that's one item, coordinate. Okay, so that's one item, coordinate. Okay, so that's one item, but it can be passed around and copied. but it can be passed around and copied. but it can be passed around and copied. So just be very careful not to try and So just be very careful not to try and So just be very careful not to try and cheat the system. But understanding cheat the system. But understanding cheat the system. But understanding stack versus heap is valuable knowledge stack versus heap is valuable knowledge stack versus heap is valuable knowledge and it is foundational for understanding and it is foundational for understanding and it is foundational for understanding how to make your application more how to make your application more how to make your application more performant. So with that that's a lot of performant. So with that that's a lot of performant. So with that that's a lot of theory but also a lot of practical stuff theory but also a lot of practical stuff theory but also a lot of practical stuff you should really know as a car you should really know as a car you should really know as a car developer. Hope it was helpful. Uh let developer. Hope it was helpful. Uh let developer. Hope it was helpful. Uh let me know down in the comments if you have me know down in the comments if you have me know down in the comments if you have any questions about stack versus heap. any questions about stack versus heap. any questions about stack versus heap. Yes, there's a lot of confusing stuff in Yes, there's a lot of confusing stuff in Yes, there's a lot of confusing stuff in here, but I think that it's it's all here, but I think that it's it's all here, but I think that it's it's all very valuable stuff as well. Um, but I'd very valuable stuff as well. Um, but I'd very valuable stuff as well. Um, but I'd love to know if there's anything missing love to know if there's anything missing love to know if there's anything missing or you have questions on. And next week, or you have questions on. And next week, or you have questions on. And next week, like I said, we'll get into garbage like I said, we'll get into garbage like I said, we'll get into garbage collection and how that works and how collection and how that works and how collection and how that works and how the garbage collector works and um why the garbage collector works and um why the garbage collector works and um why we shouldn't touch it. Okay, so thanks we shouldn't touch it. Okay, so thanks we shouldn't touch it. Okay, so thanks for watching. As always, I am Tim Corey.
Summary
This tech transcript delves into the fundamental concepts of stack versus heap memory allocation in C programming, distinguishing between how variables are stored and their lifecycle. It clarifies that the distinction isn't simply value types vs. reference types, noting that even value types can reside on the heap and strings are specifically heap-allocated. The practical takeaway is to understand these memory management differences to build more efficient applications, with a promise of future discussions on garbage collection.