Your Variables are Not Real.

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
today we're going to talk about why variables aren't real [Music] welcome back everybody today I want to talk about a lesson that is critical it might be the most crucial lesson that I could teach any beginning C and C plus plus programmer or really anyone who wants to understand how computer software Works under the hood that is and that is that your variables aren't real they're not or I mean I guess you could say software nothing in software is real but the point is is variables probably aren't if you're a beginner they're probably not what you imagine them to be and so today I want to help you see this in action and hopefully better understand what variables are and how they work and maybe help you avoid some annoying tricky debugging situations we're definitely going to be looking at source code in this video but before we dive into the code I just want to say a huge thank you to all of you who support the channel on patreon thank you for your support thanks for making this channel possible I couldn't do what I do without your help but so now let's take a look at some code So today we're going to start with this really simple program it's just an empty Main and a couple of includes I also have a make file over here that's going to compile my code really nothing crazy here just the sort of thing that you've seen in just about all my videos if you've never seen make files before do check out my videos on make and today I'm going to be working in C but I could do this demonstration in C plus plus just as well and it would be exactly the same but so since we're talking about variables today let's start out by declaring some let's say that I come in here and I say I want an INT and I want to call it a and let's set it equal to some arbitrary integer something like 5000 and we can also create another int called B and we'll set this to 23. it doesn't really matter just pulling these numbers out of the air also Just for kicks and demonstration purposes let's also make an array we'll just give it one element although you know it's again this would work with bigger or smaller arrays but just one element and we're going to let's just initialize that value to one and let's make one more called D also one element and we'll set its value to two now you might be wondering why I used one element arrays that seems kind of silly why didn't I just make ins and I do have a good reason for using arrays and that is that this variable is not being real thing tends to get real for programmers when they're using arrays and of course I could demonstrate this on larger arrays but look Size Doesn't Really Matter here and a single element array just means less typing so we can see these values just make sure that everything's working the way we expect let's also add just a little print statement in here and I'm going to say a equals percent D I'll just print out b as well so each of these I'm just printing out these these numerical integer values C we'll say C square bracket zero it's just to say you know it is the the element of C not C itself and that's also going to be a percent D and we'll do the same thing with d and then we'll come in here and do a b c square bracket zero and d square bracket zero okay and I'm doing this just so you can see there's nothing funny going on here I can come down here and I can compile my code and I can run it and you can see sure enough we get the values that we put into these variables so that's simple now as a new programmer you may have a picture in your head that I've just created these four buckets right these four buckets called a b c and d and now I'm gonna just I can dump data into them I can say store this value in this bucket and so forth and maybe these variables feel very real they're my very real Creations but like so many things with Computing that's not actually what's going on here what's really going on well rather than thinking of your variables as buckets I think it's a little healthier to think of memory as a big block of data ones and zeros all in a line or maybe you could think of it as it's like a Blackboard and like just this big clean slate and when I say to the compiler that I want an INT called a it simply designates a location on this Blackboard and that's where it will put a so if I say store something to variable a it's just going to store something to that designated space in memory on this big board okay so let's make this concrete and let's actually see this in action so to do that I'm going to print out a few more things let's print out some addresses so let's print out the address of a and use percent P here that specifies that we're printing out a pointer which pointer store addresses or locations in memory and we'll print out the address of a and we'll do the same thing here for B and I would like to do the same things for C and D only for C and D because they are arrays we don't need the address of operator and that's because arrays are almost the same thing as a pointer I do have another video on that so if you're a little confused about the sameness between pointers and arrays thing and C do check out that video it might make things a little less confusing but so now if I take this I compile it compile it and I run it now you can see that I've got a bunch of locations addresses in memory where it has put my variables and you notice that these variables well these numbers might be they might look a little funny check out I do have a video on hexadecimal if you're new to that if you haven't seen hex before this is just a different way of writing numbers one that I don't have time to go into today but I do have another video on it so check that out and it's also really important to keep in mind that the compiler we use determines how these things get laid out right so these addresses are largely determined by the compiler you notice in this case I'm using GCC and GCC is putting a first then B then C then D sort of in the order in which I declared them that's fine let's say we come to our make file and we come in here and say let's let's say instead that we're going to use clang if I come down here if I come down yeah so uh let's make clean and compile it again and now if we run it again now interestingly you see that the order is reversed first okay so now D is actually that's that's coming at the first address in memory c b and then a okay so for today we'll stick with the clang approach but the point is is that different versions of the compiler might this example you might get different results and that's perfectly fine just adapt it to the addresses that you see here but okay great so at this point my variables aren't buckets but why is this Blackboard model more helpful why does it matter that I understand that they're just part of one big contiguous block of bits and the reason is is that it can help you make sense of some really tricky bugs so to see this let's come back to our code and let's just change things up a little bit okay so let's say that I want my program to be able to change an element of this D array based on some argument so what we'll do is we'll just come in here and we're going to make a variable called index and we're going to set it we want to get Arc V1 we want to get an argument and it needs to be an integer so let's pass it to atoi I'm not going to do any error checking in this case but in a real program you'd want to but really I'm just trying to demonstrate this as quickly as I can so here we're going to get an integer from our first argument and then what we're going to do is something like this we can say d square bracket index so we're going to take the indexed element of D and we'll set that to some arbitrary number like 42. and now if I let's save it and if I come down here and compile it and if we run it well it's seg faults now because I didn't specify an argument so but if I come in here and say 0 saying I want the zeroth element of d That's the only element that actually is in D then you can see that I've changed it to 42 okay that's simple enough right but what happens if I come in here and I say I want to change element one right well now we've changed the value of c and of course we can keep doing this if we keep going we can change the value of B we can say 3 and we can change the value of a and then of course if I keep going who knows what I'm changing after this you know I'm just clobbering memory whatever came before a and this is probably the most common way that we see one variable interacting with another or when we see you know our the idea these buckets that we've created we see them overflowing and impacting each other and the result of course is that we end up change changing some variable like variable a without ever explicitly writing in our program that we want to change variable a because the variables are all laid out together in one big chunk and what really happened is I just wrote too far at the end of variable D and I clobbered its neighbors now just to try to hammer this home let's do it one other way that is much less common in real programs but I think it may make it a little clearer what's actually happening or maybe I just like to play with pointers but you see when you index an array there's a really what's happening is we're doing some arithmetic with addresses so let's let's look how we could do this with something that's not an array so instead let's say that I come in here and I just take the I could take the address of B for example now the address of B this is an integer pointer this is going to give me a pointer to an end which happens to point to this location that's holding B okay so we'll come in here and then we're just going to add one to it what that's going to do is it's going to increase that pointer by the size of an inch by a size of a single int so that's going to be adding 4 4 bytes to it and then let's just take this whole thing so this is going to be an end pointer that points to the integer location right next to B and then if I come down here and I just put an asterisk I'm going to dereference this so what I'm saying is that integer that this thing points to I want to assign it to the value of 80. now this is mimicking what actually happens when I say d square bracket index except that D is already a pointer so I don't need to get its address but now if we come down and we compile one more time we can see if we run it let's go back to zero you can see that my code here my code that is basically trying to access the thing right after B ended up clobbering the value stored in a or in location a and so this is one of the reasons why you always hear me and others talk about the importance of Bounce checking in your arrays this can lead to a lot of security issues where you can change one value through another variable this also leads to a whole lot of really messy bugs and this is something that a lot lot of other programming languages with safer type systems protect us from go Ruby Java python and a whole lot of other languages but so next time you come across a really nasty bug where a variable that shouldn't be changing is suddenly changing to a new value then just click your heels together a couple of times and repeat to yourself variables aren't real variables aren't real they're just locations in a big contiguous block of data ones and zeros and maybe something else is clobbering this variable and then maybe bust out something like a watch point in your debugger and that can help you track down what's actually clobbering it so I hope that helps I hope it helps clear some things up some nasty bugs I hope it helps you be more confident capable programmers and until next week I'll see you later
Info
Channel: Jacob Sorber
Views: 16,850
Rating: undefined out of 5
Keywords: variables are not real, variables aren't real, variables are nto real, program variables, c programming tutorial, c variables, variables in programs, what are variables really, understanding variables in programming, declared variables not real
Id: YO6K5K1TUj4
Channel Id: undefined
Length: 10min 38sec (638 seconds)
Published: Tue Feb 21 2023
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.