Transcripts

Coding 101 63 (Transcript)

Net casts you love from people you trust, this is Twit! Bandwidth for Coding 101 is provided by Cachefly at cachefly.com

Father Robert Ballecer: This episode of Coding 101 is brought to you by Lynda.com, the online learning platform with over 3000 on demand video courses to help you strengthen your business, technology and creative skills. For a free 10 day trial visit Lynda.com/c101. That’s Lynda.com/c101.

Today on Coding 101 we’re continuing our C module and we’re wading into pointers. Hello and welcome to Coding 101. It’s the Twit show where we let you into the wonderful world of the code monkey and the code warrior. I’m father Robert Ballecer the digital Jesuit and I’m joined by my super special guest co-host/code warrior…we’ve really got to work on that title – Mr. Lou Maresca. Lou, welcome back to Coding 101.

Lou Maresca: Thanks for having me again. I’m excited.

Fr. Robert: Now of course we are pre-recording this show because I’m at NAB right now and we don’t usually say we’re pre-recording but I’m going to do it now because I want to be honest with the audience. We were supposed to do this a couple hours earlier but you had an interesting afternoon…stuck on a freeway somewhere?

Lou: Car trouble yes. I was changing a tire during rush hour. It was fun.

Fr. Robert: I hear this is what happens when you drive back from Microsoft, you just you know – lose a tire.

Lou: You get a flat tire.

Fr. Robert: Actually the story that I want to talk a little bit about does concern Microsoft because I was in a forum the other night and people were talking about how the rise of containerize services is changing the way that people program. I didn’t think about this but this is absolutely true. When we used to think about programming teams you would typically build up some sort of application suite, a development suite that you could hang things off of but we’ve gotten a lot of announcements from Microsoft this last week about their Nano server. It’s going to come in server 2016 and the idea is they’re breaking it down into just the bare bones and then you hang off micro services on it and it is called Nano Server. So that you can containerize and then just spin up the instances as you need them. What does that do to the programmer because you can’t think the same way right?

Lou: If you think about it, it’s basically taking virtualization to the next level because when you’re building an application you basically are building it in what’s like a sandbox and it’s allowing you to then virtualize it and have multiple different sandboxes on the same server or same machine. So when you’re actually building your application you might actually be running right next to the same instance of that application on the same machine and so you kind of have to think about – you have to be more careful with what you’re doing and how much resources you’re taking and if you’re taking too much CPU or memory and that kind of thing. So it changes kind of the way the programmer has to think when they’re building their application.

Fr. Robert: Right, if you look at the announcement that Microsoft made about this Nano Server it is going to be 1/20th – 1/20th the size of the Windows server core. It is going to only have things like hyper v, clustering, networking, storage, dot net, core, clr and no g y. So they really have stripped this thing down which historically is something that Microsoft has avoided doing. They’ve done it for Windows XP embedded, they’ve done it for a couple of dot net projects but they don’t like letting people have just the core components. That’s not been a core philosophy for Microsoft so of course that’s changed. As you said now that the shift has been to containerization where you can have a lot of instances of the same services to handle the load rather than 1 really big powerful framework. I guess like for example I can’t be always calling out for resources. How do I do that?

Lou: If you think about it Microsoft Windows has had the capability for you to just do gooey for a while – or no gooey for a while so you could do just a command prompt but like you said it has all these other things already installed on it. There’s lots of different packages and these things are still there when you have just the command prompt version of Windows. But like you said now they’re breaking it down to kind of almost like a Unix style where it is containerized and now when you’re talking about resources; when you boot this thing up it takes very little resources, almost no CPU and then you have the chance to build an application whether it be a service or just a standalone application that allows you to utilize almost 100% of the resources because now you’re breaking it up into these little containers that can use portions of the resources of that machine. Now day servers have hundreds of gigabytes of memory in them and they don’t necessarily always use that amount of memory all the time. But with containers you have the ability to break it up and use that memory 100% of the time.

Fr. Robert: Right. Actually we’ve got JJ to the 484 in the chat room who wants to have a conversation on Open Source Windows which you know the interesting thing is I don’t think that’s nearly as important anymore because I think even Microsoft has recognized that you can build applications, you can build these containers and you don’t really have to care about what platform it is running on. It will run on any hardware, it will run on any software, it will do its own thing. In fact if you look at the move that Microsoft made with dot net to contribute a large part of dot net to the Open Source foundation and also to create a free version of Windows 10 – a stripped down version of Windows 10 that will run on the Internet of Things it’s kind of like going Open Source without really going Open Source.

Lou: Yes, if you want to be a tinkerer and you want to play with the kernel and you want to change, you want to add services and change the gooey… a lot of people wish they could customize the start menu. There are companies out there that do it but they do operating system hooks to do it. They want to make it easier and I guess it makes sense that you’d want to Open Source but Windows is an eco-system kind of like iOS and it is making people feel – it is a little bit more comfortable because Microsoft controls that eco-system in a way that controls security and they’re opening the door to a lot more things like these Nano compartment containerized things with Windows Server. So they are making it easier for people to do things and they’re making it easier for people to virtualize. They’re making it easier for you to build a service so I think it’s getting closer but I’m not sure if it’ll ever be Open Source.

Fr. Robert: Right. Lou I could talk about this forever, in fact we did have conversation about this on This Week in Enterprise last week but I want to save us as much time in the show as possible for the rest of the C mini module because you are going to be getting us into pointers and I think people need as much time as they can get. But before we move on let’s go ahead and thank the sponsor for this episode of Coding 101 and it’s got to be Lynda. Now when you think of Lynda you should be thinking of a repository of knowledge on the internet. It’s not just programming skill, it’s not just computer skill. Lynda is the place to go if you need to increase your job skills training, if you want to learn Excel, if you want to learn hobby arts like photography and videography. It’s the place to go if you want to learn how to fly a quadcopter. Lynda.com really is a place to go to find out information about anything you want to find out about and it’s such high production value that it just helps you learn. You don’t spend a lot of time thinking about how you would do the lesson, you just get the information that you need. Now Lynda.com is for problem solvers, it’s for the curious, for people who want to make things happen. Maybe you do want to learn that new programming language, maybe you want to develop an app, maybe you want to sharpen those photography skills or maybe you want to redesign your website. Lynda.com has everything you need to feed your curious mind. Some of the courses that I would recommend are Up and Running with Mathematica 10, programming the Internet of Things with Android and the Foundations of Programming Code Efficiency. They also have an amazing series called Code Clinic. It’s a 6 course series where Lynda.com experts look at common code challenges and offer their solutions using C++, C Sharp, Java, PHP, Python and Ruby. Now I’ve been using Lynda.com for reference whenever we do Coding 101 modules and also here in the brick house we use Lynda because we switched from Final Cut on the Mac to Adobe Premier Pro on the PC and sometimes our editors just need to get that little clue for what tool transfers over in what way. With Lynda.com’s transcripts it means that you don’t have to sit through entire lessons, you can search for your question and find the exact part of the video that deals with that solution. You can watch and learn from top experts who are passionate about teaching, you can stream thousands of video courses on demand and you can learn on your own schedule. That’s important. Learning at your own pace allows you to watch these courses from start to finish when you have time, when you can consume them and that just means the information will stick. Again you can browse those course transcripts, you can take notes as you go and refer to them later, you can download tutorials and watch them on the go including access on your iOS and Android devices. You can create and save playlists of courses that you want to watch to customize your learning path or share with friends, colleagues and team members. And guess what? Your Linda.com membership gives you unlimited access to training on hundreds of topics, all for 1 flat rate. Whether you’re looking to become an expert, you’re passionate about a hobby or you just want to learn something new I want you to visit Lynda.com/c101 and sign up for a free 10 day trial. That’s right, free 10 day trial, so you’re out of excuses. This is how you need to learn and it’s how you need to learn now. That’s Lynda.com/c101 and we thank Lynda for their support of Coding 101.

Alright Lou, let’s get to it. When we last left our intrepid explorers they were just diving into C and now you’ve got a little something for us. Where do we go?

Lou: A little something. So I think the first thing I want to do is I want… somebody asked in the Google Plus the other day; can we use Visual Studio to program in C? I wanted to show really fast just how to get Visual Studio to work with C. So you have just the Windows desktop version on Visual Studio express. I have the ultimate edition but it doesn’t matter, it’s going to work the same exact way. All you have to do is create a new project and in there you’re going to choose visual C++. It’s a little confusing but that’s what the C compilers kind of coded or blanketed under and then you can just put whatever name you want here. So name, and then I’m going to just hit ok and watch what happens. We get this window that’s going to pop up and say let’s create a little application using a wizard. So I’m going to go through the wizard and all I’m going to do is click empty project, console application, empty project, uncheck that box and then hit finish. That’s all I’m going to do. Now I’m going to get what you’ll see over here – my little solution explorer once that gets created – hopefully gets created. There we go. Then what we’re going to do then is we’re going to add a source file to it and the source file that I’m going to add to it is I’m going to go to add new item and I’m again going to click on C++ file but instead of choosing that I’m just going to call it console .c and I’m going to hit add. Now what you’ll see is now I have a console application that I can run, that I can actually start coding in C code. It’s no problem once I compile this I can…

Fr. Robert: And there, that’s what we’re used to. That’s what we saw last time.

Lou: Right. So it’s exactly the same and it’s just as easy as that. This is the application we showed last week. We talked about this where we’re including the library and it lets me to standard io and then I have a little tiny function at the top here that lets me click enter when I want to enter and move on. But what we wanted to go into today is talk a little bit about kind of the under workings. What’s the advantage of using C? Why are we using C? I guess we talked about last week... if you remember we talked about that the C is really close to the metal right, it allows you to compile and code in a way where you feel really close to the metal, really close to the memory and really close to the machine. So I think that’s really the key to C and so the first thing I like to show is a couple things. 1 is the size of method there, a function that you have in C which what that does is allows you to show exactly the size of a variable that you have in your application. Like for instance how much memory it is taking up. We like to call memory as a currency for your application and back in the C days when people used to code a long time ago in C it used to be that they didn’t have much memory and they needed to be very specific on how much memory they used. So if you see here I have a quick really simple application that shows me creating a variable that’s an integer and we talked about that last week. There are different types of variables and in this case I’m choosing an integer and I’m setting a value to it. So I’m going to initialize it – they call it initialization. I could have done this on 1 line too but I wanted to be very explicit here so everyone can understand what I’m doing. Then I did the print statements like I did before but the difference is that we saw the first line where I’m using just a print statement and I’m passing the variable and saying hey this is what the value of it is. But then the second line I have another print statement that I’m using the size of and what this is going to do is if we run this it will actually show us the memory size of the variable. This is a lot different than – so you notice it has 4 bytes – this is a lot different than if you use a high level language like Python and C Sharp because you don’t necessarily know exactly how much memory you’re utilizing when you’re creating things. They try to help you and say if you do an integer in C Sharp it’s most definitely going to be this many bytes but again then you have the overhead of the CLR which is the run time and everything else. But when you’re doing C you know for a fact that my integer value is always going to be 4 bytes and no matter how many integers I have it’ll be 4 times that many integer points.

Fr. Robert: There are going to be people who are looking at this and they’re going to say wait a minute; why do I care? My computers got so much memory but you have to remember that one of the things that C is still used for today is a lot of embedded programming. If you are using any Arduino based machines, if you’re using a mega based chip set you’re probably going to be programming in C for just this reason because that you go to the higher level languages you’ve got so many layers of abstraction that you don’t know exactly where your memory is going. With C you always know exactly how much memory every variable type is going to use, you know how much memory every method is going to use and therefore you can predict how the limited resources of your embedded devices are actually used.

Lou: Exactly right. That’s so important because people don’t really think about it today because you have these massive desktops, laptops, even your cell phone can have 4 gigs or 8 gigs of RAM sometimes so it’s like why do I really care about memory. But like you said when you have these little type of interknitted thing devices you have to be very specific. Some of them only have – like the Raspberry Pie, some only have a small memory so you can load an application to it maybe 12 kilobytes, 24 kilobytes and you have to be very specific about how much memory you’re using so that’s why C is a really good language for it.

Fr. Robert: Alright so we’ve got that down that the primary advantage of C is that it is incredibly predictable on how much memory it is going to use which makes it a unique language among some of the higher abstraction languages that we may use today including things like C Sharp or Visual Basic.

Lou: Exactly. So one of the cool little syntax tricks that I like to do too is you have this value and we know that it’s a type – a number, an integer and we know what the size of it is but maybe we want to know where it is at. So like what we were talking about how the computer has memory and the memory has locations and those locations are represented by numbers and sometimes you want to know where that location is on the machine. So like for instance when this application spins up it gets loaded into a specific portion of my memory on my PC and then when I load up other things on that program it also loads them in that portion of the memory. So like for instance if I wanted to know what the memory address of this value is I use this cool syntax here called a reference operator. Basically all it is doing is saying go and get me the location in memory of that value. So if I run this all you’re going to see is this fancy number here but basically all it is saying is that’s the memory location of that variable. So again it is kind of cool because now you’re getting to the under pinning of what your operating system is doing when you’re creating variables, when you’re actually programming.

Fr. Robert: Right and this is in keeping with that theme of getting really close to the memory. You don’t see this if you’re programming in any other language because you only know it is the variable. This is the variable and that’s where I’m storing that value. But in reality that variable needs to actually exist in some physical piece of memory and that piece of physical memory has to have an address because when I go to the hardware I don’t say give me the variable called A, I say give me the variable, give me the data that starts at address this this and this and goes on for 4 bytes.

Lou: Right. We’ll get into it a little bit with something called pointer where it’s basically a memory location but one of the things in modern languages of today like C Sharp is you can – when you pass variables around in your application it can be done by what they call reference meaning you’re passing the same copy of that variable all around and when you’re changing it like say in a method or function it’s changing that original copy, that original location. But in C when you pass a value around if you don’t use a special construct called a pointer or a memory location you’re actually copying the data around so if I were to create a function that were to actually pass that as that value somewhere. Let’s say I created a function called show me and I passed it the value here and I did something with it here then what I could do is when I passed the value in here and called show me, it would actually pass a copy of it and not actually the original value. So if I were to  inside here set the value to something else, when it come back out it would be the original value because it was not actually changing anything.

Fr. Robert: Right.

Lou: So that’s actually the key here. With some of the modern languages today it allows you to pass by reference much easier but that’s where we get to the concept of what we call pointer. So what a pointer is, is it’s basically just a variable where you can store or hold a memory address and reason why you want to do it again is just like I said. Sometimes you want to be able to pass around not just the value but where the value is stored. So this way you have a better understanding, you actually have the ability to change the original value. So you’re not actually changing a copy of it. Again that’s all around size of your program. You want to be very close to the metal and you don’t want to be making copies of stuff and doubling up how much memory you’re using. The way you actually create a pointer is you use what they call a D referencing operator. It’s just a fancy way of saying if I wanted to create a pointer here called value 2 that’s all I have to do. What I could do is actually store the value of – so I give the memory location to my original value and that’s I’m actually storing the memory location now in my pointer. I can start using this and passing it around just as if I was passing around a variable.

Fr. Robert: Ok now let’s back off just a bit because this is typically where people who are brand new to programming – the first time they hear about pointers they’re going to go wait a minute, hold on Lou! Why wouldn’t I just use the variable name? I don’t understand why are you trying to make me do this additional step of trying to figure out where the memory actually starts and what its address is? What would be the simplest way you can tell them why and when you need to use a pointer versus just the variable name?

Lou: I think the simplest way is that if you’re just doing a simple application like this and it’s just a couple lines it doesn’t make sense to use a pointer but there are some instances. For instance if you’re going to be using an array or building an array… we talked about an array before. It’s just a table of things that are the same type. So like a whole bunch of numbers in a table and I can store those in a big table but what I can do for pointers is I can actually grab a location in that array right away by just using its memory location. Another way like I said before if you wanted to build a function in this application and you wanted to pass it a variable but you didn’t want to actually copy the variable to that function, you wanted to pass your original one that’s in your app, you wanted to pass the instance of it around you could use the pointer and that allows you to reference the original value. So it’s really dependent on your application but in simple apps you’re not really going to do it but more complex – having functions and stuff, as you start to build bigger applications you’re going to start using pointers for sure.

Fr. Robert: Right. That’s actually a really good explanation. As you start just use variable names but as you get more advanced and as you start going between methods and functions, pointers are going to become important.

Lou: Exactly right. So I wanted to really fast show you an example of what an array looks like so we can kind of show you an example. So we did an array before – I think it was just a very simple one but I wanted to show you an example of what that looks like. So this is an array of just numbers, integers and basically that’s how I’m initializing it. There are multiple ways to initialize it. You can initialize it by doing it this way, you can initialize it by putting that I’m going to have 5 values and then I can loop through this array, I can use loops and do that way or I can actually initialize just 1 index of that to some value. So there are lots of different ways to initialize but this is basically the easiest way of doing it – to just do that 1 line like I was showing you. An array will hold a bunch of different types. You can do an integer, you can do a character array meaning I can hold… A character array in C code is kind of like a string like we’ve done in other coding. So this will hold the word code in it. So there’s lots of different ways to initial arrays. Arrays are very useful because it allows you to store a whole bunch of data and be able to loop through it and array through it and all that stuff.

Fr. Robert: Lou I’m going to pull you back for just a bit because 1 of the things we like about being on the Twit TV network is we can get immediate feedback from our audience and we’ve got a question from Web Zero 4A – a very simple question and I think you know often we forget that this is actually a question and that is; what does it mean if your memory has an address of 144614641? How does that become something that stores a value? How is memory laid out?

Lou: Memory is laid out in a software system by what they call a stack and a heap and your application gets a specific amount of stack memory when you boot it up the first time so it gets very complex but it’s all dependent on different operating systems, the fitness of your operating system but you get a certain amount of memory and that memory has a beginning location and an ending location. That stack is kind of right in front of you at the forefront, kind of what they call your RAM I guess you could say and also there’s what they call a heap which is more like if you’re using C++. A heap stays around for much longer. So like if for instance if I were to create a class and an object I’d put something on the heap and it will store it on a block of memory called a heap and that will stay around a long time for like the life time of that object. But the stack allows me to kind of just put things on the stack and as I use them I pop them off the top and then they just disappear over time because they’re just values. So that’s kind of how your memory is structured. Obviously it’s more complicated than that but again your application gives a beginning address and an ending address and then as you put stuff in it your memory gets kind of reorganized and organized in the operating system.

Fr. Robert: Right exactly. Alright so we’re in arrays now and you’re explaining to us how arrays are formed and exactly what the structure is. What’s the next step?

Lou: So I think the next step is I wanted to give a really quick example here of how you could use pointers and then we’ll go back to how we can use pointers with arrays. It’ll make a bit more sense after we’re done with this. So let me show you an example of that a pointer is. We have this original example of us using a value and then setting the value but then I declared another value here called value 2 and I made it a pointer. What I’m going to show you here is the original line was printing out the value and then I was printing out the size and then I was printing out the actual memory location. But then I wanted to do something different down here. I wanted to set, to grab the memory location using that reference character there and I wanted to put it inside the pointer. So basically I’m saying that the value of this guy is now… the memory location of this original value is now put inside my pointer. Now what I’m going to do is I’m going to print it out but what you’ll notice is when I don’t use the actual little dereferencer here you actually print out the memory location of it, not actually the value. But if I use the dereferencer what you’ll see is when I print this out…so the first line is the value and the second line is the size, the third line I used that and percent so it was basically the referencer and then the third line is me just using the value, the pointer and you notice now I’ve got the variable – again the memory location. But then the final line is I’m using the pointer base variable and it’s basically printing me out the actual value again. So the key here is basically considering that value or that pointer to be the value. That’s kind of a cool way of showing that you can use a memory location and you can use the actual value or you can actually dereference it using a pointer. That makes it a little easier to understand from that point.

Fr. Robert: So this is giving us the value, this is giving us the address where it is actually located and it’s giving us the size of the location. Nice.

Lou: Right. Now I think the last thing we want to do is kind of give an example of how we can use arrays to… because arrays are different than pointers but they are very close to, they’re not the same thing but they have a ton in common. They’re very common. For instance an array sets aside a continuous span of memory for 1 specific data type but an element in the array can actually be just a small offset. So like for instance if I’m at memory location 1 and I put an integer in that array then I can say that the offset of that value is actually 1 plus 4 bytes because I’m basically moving over 4 bytes to get that value. I’ll show you an example of what that means so this way it’s a little easier to understand. For instance in this example…I’m going to get rid of this loop so it’ll be a little easier. I have a character array of 4 characters and what I’m going to do here is I’m going to show you that what the address of that 1st, just the array itself and what you’ll see here is the address of this guy… whoops, I’ve got to initialize thing first. Sorry about that.

Fr. Robert: It doesn’t know what that is.

Lou: There you go. That’s what happens when you compile and you don’t…or when you break the code and you don’t compile. Ok there we go. So what I’m going to show you is that the first element of this array has… the 0 element of this array because arrays start at 0, they don’t start at 1. The key here is if I were to increment that memory location, say I want this second 1, what we’ll see is it’ll actually go up in 4 bytes. So let’s print out 2 versions of this.

Fr. Robert: Yes give me the first 1. So people can actually see that no really an integer is 4 bytes.

Lou: That’s right. Oops, it would help if I did that. It’s not compiling. There we go. What we’re seeing is that I’m actually moving up 1 element in the array and it’s giving me the address of that next element in the array. So you see it’s actually 1 byte different because this is not an integer array that’s normally 4 bytes different. This is a character array. 1 character is 1 byte divided by themselves so basically I moved up to the second instance of that to the second element and now I moved up 1 byte. So if I were to add a third one or a fourth one what it would do is just move up 1 memory location by 1 byte. That kind of shows you how…

Fr. Robert: Lou just for giggles go ahead and change the array to an array of integers.

Lou: Sure, there we go. We’ll do 5 and then change this really fast.

Fr. Robert: Or just comment it out.

Lou: Yes.

Fr. Robert: Even better.

Lou: There we go and then we’ll do 2 of these. We’ll do this and then this and then we’ll show that guy. So what we can see here is the first one is a character array where we go up 1 byte as we go up each element in the array and then the second one is an integer. As you can see there is a memory location and then it goes up 4 bytes.

Fr. Robert: This is memory usage in real time. This is exactly what’s happening inside the machine. When you declare that array it’s going to take all the contiguous memory and you have a starting address of the array and you have an ending address of the array and every element will increment depending on how much space each of those elements will take.

Lou: That’s right. So again you’re kind of close to the metal here and you’re starting to understand using variables and just how much space they’re taking up in your application and then where they’re located in your physical memory. That’s actually a huge step into understanding how computers work if you think about it. Once you start to understand that and then doing embedded programming you’ll really start to understand why you need to pay attention to that type of stuff.

Fr. Robert: Lou there is something else here. Arrays are great I know, I actually spend a lot of my time working with arrays because I do a lot of analysis of data. However when we’re dealing with arrays we’re dealing with an array of similar elements. You’ve got an array of characters, you’ve got an array of integers, and you can have an array of doubles, whatever it’s going to be. There is another data type that actually can combine dispirit datas and that’s called a struct.

Lou: That’s right. A struct is a collection of variables that are kind of blanketed under a single name. For instance if I wanted to create – we talked about this before in some of the other programming languages where you can have structs that are… you bring things together and you basically characterize them. I’ll show you an example really fast of what that looks like if you show my screen again. I’m going to actually create a struct called person and I’m going to put it on… I’ll show you on the screen here. What this person has is several attributes that describe it. So in this case I have my first name, my middle name, my last name or the student’s last name, their height, their hair color and this allows me again like you said to collect a bunch of things and collect them under 1 single name for better handling. In this case it’s just a collection of properties or attributes in this case. There are lots of ways to declare this, the struct and then the struct name and then I put all this. There’s multiple ways to do this stuff and I’m going to try to show you a couple of ways so you can understand. If I wanted to start using this I would actually want to put this next line in here which is telling me that I’m creating a new type of this struct called student person and then I want to go and use it. But there is also another way to do it which is even easier than this and that is by using the type def. up here. So I can just do type def. and now I don’t need this line anymore.

Fr. Robert: We’ve seen this kind of collection in higher level languages and typically it would be a class.

Lou: Right exactly. Again structs are made to be value type so they are very mean and lean and again you can pass them around by using pointers and it allows you to then pass the original value. I should show an example of that what that means. Why you might want to actually pass an instance of this around versus just a copy of it. Because it doesn’t make sense exactly to just pass a copy. So a good example is that I’ll show you real quick is what we like to call pass by reference or pass by value. I’m going to create 2 little functions here that have pass by reference or pass by value and the key here is the first one is not going to use a pointer. It is just going to say I got an instance of this thing and I’m going to pass it and I’m going to set the height. Then the second function is saying ok I’m going to pass it but instead this time I’m going to use a pointer and I’m just going to pass the memory location of it and then I’m going to change the height of it. I’m showing you 2 ways to actually access here; 1 is called the dot reference or the memory operator is this little dot right here and the other one is called this structure pointer operator and that allows me to go and say I have a memory location, let me go and get this member of that actual object that’s at that memory location and set the value.

Fr. Robert: Right.

Lou: We’ll show you exactly what happens now if I actually create… Down here in my application I’m going to delete all this so you can actually see stuff and I’m going to create what we call an instance of this person called person 2 and I’m going to set their name to be change old names and I’m going to give them the age of 19 and their height of 65. I’m creating an instance of struct and I’m initializing some of the variables on there. Again this is very similar as we talked about in an array. I did the exact same thing for an array where I just used these little curly braces and initialized it. So now the first thing I want to do is pass it by value. Let’s see what happens. What I’m doing here is I’m passing the value and I’m going to print out the height in inches after it’s done so I’m going to use the debugger and this’ll be 1 step at a time so let’s see what happens here. The first thing that happens is I’m going to step into the application and I’m passing this by value so what happens is I’m going to set the value and height to inches. So what you’ll see right now is the height in inches and it’s really tiny on the screen – I apologize.

Fr. Robert: Actually Zach can you zoom into that?

Lou: What the debugger is showing you is the value right now is 65 right because I told them that the height of this person is 65 in inches. What I’m going to do is change it to 10. So once I’m done with this now this is 10. Now I’m going to pop out of this function and I’m going to come back down to print and what you’ll see is what actually prints out is 65. The reason is because I didn’t actually pass the actual instance of it which is this guy right here. I passed a copy of it so when I got inside this function it changed the value to 10 and when I popped out of that function it went right back to 65 because I don’t have the same copy evidently.

Fr. Robert: That’s actually a perfect example of when you’d want to use it because if you want these changes to actually affect the value that you’re going to display later on you can’t just pass a copy, you’d better pass the reference.

Lou: Exactly right. That kind of sag ways into the 2nd part. The 2nd part is now instead of passing it by value now I’m going to pass it by reference meaning now I’m going to go and grab the memory location of it and I’m going to pass by reference. Basically I’m passing it in a pointer which is this guy – a memory location and then I’m going to change it. So let’s look what happens now. I’m going to break point right here. That lets me stop, that’s the “I” reference and again we have this guy right here and then… oops I don’t want 2 continues here. Then let’s step into this guy. Now the value again if you look at this again our value is going to still be 65. Again it is really tiny down here but it says height, inches 65. Then we’re going to step into this pass by reference. We step in, again we’re going to change, we’re using this little point operator but we’re going to change the height in inches to 10 but when we pop back out now and I print it to screen what you’re going to see is the student’s height is 10.

Fr. Robert: That’s exactly what we want.

Lou: That’s right. The key here is I was actually passing or copying that data, I wasn’t making an exact copy of it. I was…whatever the size of that instance of that struct is or that type I wasn’t copying it over into another portion of memory and then changing a value and then when I was popping it back out I was again back to my original value. I was actually taking that original instance, getting the memory location of it and saying I’m just going to pass this number to it and then the application, the function is going to know that I’m going to take that number and actually set a value at that memory location. That’s what it was doing so when I popped back out now I have a new value. That’s kind of the key.

Fr. Robert: Let’s flip it around a little bit because now we know why you would want to pass by reference rather than just passing a copy. When wouldn’t you want to do that? When would you want to not give them the pointer because you’d be changing the original data?

Lou: That’s a good question. I think when you wouldn’t want to do this is if you were like for instance – you had a separate function that would go and do some operation but then you wanted to kind of go back to the original value when you came back out. For instance if I had a long list of students and I needed to calculate something, like for instance I needed to calculate what their bill is going to be. So if I passed an instance of a student in and then this application would go and get how much they owed and calculate it and print it to screen and then it would pop back out and loop through to the next student. The key here is if I was passing by reference it would actually change that student object over and over again to that value. So when you’re actually printing you’d find out that oh wait I said passing by reference. It’s actually showing the student’s name and address and what they owe for the same student over and over again. But if I actually pass by copy I’m just copying the data in there and then I’m doing something with it and then when I pop back out I can set a new value. That’s kind of the key. It’s all dependent on what your application is doing and when you actually want to pass the actual thing or when you just want to do something separate over here and then come back and do your own thing again.

Fr. Robert: Right. The example that was often given to me is if you were trying to create a program that does say conversions to multiple types of currency you wouldn’t want to be passing the reference every time you do a conversion because then you’re changing the original number and every subsequent conversion is going to be off. You’d pass copies and each function would do the conversion and have its unique value. This is actually something that you can figure out when you’re in the logic tree step of writing your program. Do I need to preserve this function, do I need to preserve this value, this data or do I want it to be overridden because the new value is what every other function is going to use?

Lou: Right exactly. It’s a little confusing but as you start to use it you’ll understand. If you start to use the size of function it’s kind of cool because you’ll start to see that ok the size of my student type is actually x. So let’s say it is 12 bytes and then when I actually pass it by value you’ll notice now you’re copying that 12 bytes to another function and doing something else. So when you’re doing embedded programming especially it’s important because now you have 24 bytes worth of data that’s on your memory in your stack of memory and it is taking up double the amount. But when you’re passing it by reference you’re basically saying no I’m not going to copy it, I’m just going to pass the actual original version of it and so now I still only have 12 bytes of data. So that’s super important when you’re working with little devices like that.

Fr. Robert: Right. We’re actually going to be doing more projects using our do we know and using imbedded processors so yes it’s a good time to get into the proper way of handling your memory addressing. This is good programming. Be efficient with how you program and then you never have to worry about using more resources than you think you’re using.

Lou: Exactly. The very last thing before we kind of wrap up is I wanted to show how you can kind of set up similar… I have a student person here and I wanted to show you… sorry for my naming. I had a bunch of other types I wanted to create.

Brad: There’s a lot of persons in there Lou. A lot of persons.

Lou: Student persons. In this case I want to create a person but what you’ll notice here that instead of using arrays I’m actually using pointers. The key here is you can actually change them in this case. So when I initialize this I can actually initialize this to a 100 element character array and that’s kind of the key. So if I were to go and initialize something like a person down here now I can initialize this guy to a 100 character array. If I want to call him Bill now a character array is the same thing as a string in this case and now person has… if I were to look at the person’s first name it would actually be Bill. That’s kind of the key. That’s why I said arrays are closely related to pointers because they’re just copies of the same type over and over again. This is just 100 characters in a large array. But again I can declare a struct that has an attribute call for first name and again it is a pointer but it will point to an array when I initialize it. That’s kind of the key that they’re almost interchangeable in a way.

Fr. Robert: Right. Nice! Well Lou what do we want to leave our Code Monkeys with? Because this was a mini module so this was just a 2 episode hit of C. Now we will be coming back to C again and again because it really is a nice foundational language. It is a nice place to ground yourself and learn some of the more important foundational pieces but if we wanted to challenge them what would you do because again we do have to give them the code. We have to give them the assets that they’ve seen in this episode but what else would you want to tag on?

Lou: Get familiar. For me I like to print stuff to the screen. Just get yourself familiar with code. For instance get yourself familiar with writing in – lab, doing some variables, passing them by value like I showed, print them out, show the size of them and do that kind of thing. Just really kind of understand what’s happening with the application. What you’ll do is actually find you’ll type it wrong, maybe you’ll use the wrong dereferencer versus the referencer and you’ll find that that at some point you’ll get the compiler and the run time will say oh you have an access violation in memory and it’ll tell you why. It’s basically just trial and error. Just get yourself in there and get yourself used to using and programming. We just went through very basic concepts but now as you get really into complex C coding it just builds on top of those spaces. I think take the code that we have, muck with it and break it and the compiler will help you, especially if you do Visual Studio it will help you and give you very specific errors. Then just try some other stuff. Maybe put an array of people and then learn to go and use what they call loop and loop through that array. Just start building out the application and doing other things and you’ll get to feel more comfortable about it.

Fr. Robert: Fantastic. Lou Maresca, he is our code warrior and our super special guest co-host. Seriously we’ve got to do something about that Lou. Could you please tell the folks where they can find you, because of course you’re going to be all over the Twit TV network. I have you as a guest very often on This Week in Enterprise Tech. I do believe we’ve got some reviews in the can from you in Before You Buy so we’ll be seeing you there and of course you are often, almost always now on Coding 101. But where else can people go if they want to find out what you’ve been working on?

Lou: You can always find me on Twitter. LouMM and of course all my daily work that I do on my day job is at crm.dynamics.com.

Fr. Robert: Before we bid you farewell I do want to say that the next time you see us, because this episode is actually going to be going up on Monday the 13th or whatever it is but the day after we have the 10th year anniversary for Twit TV. We’re going to have Lou in studio and I believe we’re going to be recording a couple of episodes and hopefully by then we might have some good news Lou? Do you think?

Lou: Yes. I’m super excited.

Fr. Robert: We should talk about that.

Lou: A 10 year anniversary too.

Fr. Robert: Right, it’s perfect. Everything is perfect. So good news coming and I think it’s going to involve LouMM. Lou Maresca we salute you sir for being our code warrior, for being the constant presence of knowledge here for Coding 101.

Lou: I love being here. Thanks so much.

Fr. Robert: Folks we want to thank you for sticking with us. We will be here every Monday. Well except for this pre-recording and any other pre-recording we do in the future. You’re going to find us at 2:30 PM Pacific time at live.twit.tv and as long as you’re going to join us live why not jump into our chatroom at irc.twit.tv. It’s a place where you can ask question, you can tell us whether or not you’re getting the material that we’re laying down and this is very important. When you tell us that you’re not understanding what we’re talking about it gives us a chance to back up and say we should probably explain that – that concept, that thing, that component. So jump in, that’s right, live.twit.tv and irc.twit.tv. Don’t forget that you can always go to our show page at twit.tv/code or coding 101. It always goes to the same place. Here you’ll find our entire back inventory of episodes so if you want to download a previous module, if you want to look at what we did with C Sharp – actually a couple of modules or with PHP or with Pearl you’re going to be able to get it right here and you’ll also find a place where you can subscribe so you get every episode automatically to your device of choice. It’s one of the things that we do because well, we love you. Don’t forget that we’ve got a very active Google Plus group. Just go to Google Plus and look for Coding 101. It’s a great place to go if you have knowledge that you want to give or if you have questions that you want to ask. But don’t worry about it if you can’t find that because you can also find me on Twitter. Just go to twitter.com/padresj, that’s @padresj and you’ll be able to ask me questions about the episodes, about the guests and suggest future languages that you want to learn and future concepts that you want us to talk about. Don’t forget that is twitter.com/padresj or in this case you can find out what happens when people on Wednesday try to wake me up. That’s right, just go back to bed. The next time we see you will be on 4 20. Until then I’m father Robert Ballecer. This is Coding 101. End of line!

All Transcripts posts