About this episodeThis show covers C++, a general-purpose programming language. News: Visualizing code to fail faster, the future of the used game market…
This show covers C++, a general-purpose programming language. News: Visualizing code to fail faster, the future of the used game market, Prince of Persia source code found. The tools of the biweek are sfxr and DC universe online.
★ Support this podcast on Patreon ★Transcript
From the publisherThis transcript comes from the podcast’s own feed, shown as the publisher wrote it.
C++
Hey, man. So I had some friends over, some friends of the family over from Florida visiting the San Francisco area. It was pretty fun. I got to show them around. We went to the—we actually went to the Monterey Bay Aquarium. And then we went to San Francisco proper, I guess you'd say. And I showed them like, have you seen that really old vintage arcade museum in San Francisco?
Yeah, so folks at home, check out my G Plus page and you'll see an original Pong, which is pretty awesome. And some other really cool, like old school, like we're talking like 1920s or something, arcades. Like they actually have a bunch of these manual arcades where everything is driven by servos and all that. I would love to get one of those in my house. Oh, it'd be awesome. They had one that was a real BB gun that shot BBs, and you had to like hit real targets that moved around. It was crazy.
One of the interesting things. So I have a tablet. I have—I think I have a Motorola Zoom. And so I was kind of like playing music in the kitchen with a tablet. And, you know, my friend from Florida was like, you know, I really want to get a tablet. I'm kind of really, you know, my techie side really wants a tablet, but I have no use case for it. And, you know, I started, you know, like I bought a tablet, so I feel like I need to like argue on the other side of this. So I was like, yeah, but, but, you know, you can play music. Yeah, I play music in the kitchen. And he's like, well, you know, I could just play music on my laptop. I can carry my laptop from one room to the other. And it was like really hard. You know, I thought about it and I was like, yeah, you know, I bought this tablet and I really don't do a whole lot with it. So, I don't know, it really made me think about like, you know, how I can sort of better utilize it. Wait, so not only did
A Motorola Zoom, but I have an iPad. And I will say that although it might be hard to come up with specific use cases, I have like a backup system on my computers that, you know, automatically backs up to the cloud or whatever just, you know, for safety reasons. And it sends me an email when a computer isn't backed up in a certain time frame. And I keep getting emails from my laptop because I literally do not turn my laptop on maybe once every other week. Oh, wow. I just don't use it anymore. And the reason being is that, well, first of all, if you have a young child like I have, a baby, that when you're like feeding the baby or holding the baby, you can't hold a laptop. Or you don't want to risk the baby like fluids coming onto your laptop. And plus it just being cumbersome. But you can hold a tablet and, you know, use it while you're having to hold the baby or feed the baby or whatever. So that's one good use. Another use I found is that if I'm doing casual web browsing, it's a much better experience to do that on a tablet than on a sitting in front of a computer or a laptop where you're not needing to use the keyboard. So why is it there?
Use this weird touchpad or a mouse. And I just it's not like I find myself sometimes tapping on my monitor by accident. It's like, oh, why is this not working? And so I find that it's useful. It's one of those things is not necessary. It doesn't give me any capability really that I didn't have before. But it's such a better user experience. I can take it from room to room. The battery on my iPad lasts an entire day of light usage versus my laptop will hardly last a couple hours. Normally three.
Yeah. And like for a laptop, I find I almost always use a plugged in because the battery just tends to run down so quickly. Yeah. And I'm sure there are nice laptops which don't do that, but not mine and not most people I know. Yeah. And so that's a really good experience. Also, like a tablet, if you're going on a car trip, it's very easy to just pick up your tablet and use it in the back of a car. And
Designed for this like pick up, put down. So like you can just put it to sleep, you know, and it's instant turn back on versus a computer. You can't just like set it there for like half an hour and then pick it back up because you had a conversation because you either would have shut it down, suspended it, hibernated it, like whatever. But it'll take 30 seconds to come back on. And that's annoying.
Yeah, that's true. You know, I did one of the, you know, most recent times where I use my tablet heavily was I went on a ski bus trip. And there's this program which will let you use a WiiMote to control your, like to talk to your tablet. So I actually had like Nintendo games running on the tablet and the WiiMote was the controller. Oh,
Yeah, it was pretty awesome. I was actually amazed that that worked at all because I kind of like, I had the idea. And I was like, oh, let me just see if there's some program like that'll let me use the WiiMote on the market. So I just went on Market and looked up, which I guess now is called Google Play. I went on Google Play and looked up, you know, WiiMote. And sure enough, someone had written some driver for it. Nice.
So the way around this is that if you know this in advance, which you can typically look it up. And once you have one, you have it, you know, for—you don't need to keep buying it again. But they have like travel vacation routers. So it's like either—they have some that are even battery-powered, but they also have ones that are really small, which are just a very basic, you know, just supports a few connections that's like $30 or $40 or whatever. What? Really? And you can plug it into the wall. Yeah, run it. It's like a travel router is what it is. That's
If you go to a hotel and there's a Wi-Fi and you pay to get on the Wi-Fi, some whatever outrageous rate they tend to charge. And the nicer the hotel, the more they charge, which is terrible. And so you can use this, pay for just the wired, plug this in, and then broadcast it. And then, you know, everybody traveling with you can share it. Versus if they only have Wi-Fi or you pay to use Wi-Fi, you're stuck only using—because it's like tied to your MAC address typically. So you only can use it on whatever device you paid for it on.
Oh, okay. You're leading us into our next topic. Somebody happens to be an expert on that. No, no, no, no, no. So we talked last episode about Stanford's online classes. And so I actually was really bad. And I had signed up for them, but I didn't realize signing up, you know, for like interest wasn't enough to like enroll you in the class. So I thought I was enrolled. And then I got an email like, oh, hey, enrollment is ending this day. So I went to check. And it turned out I wasn't enrolled in, you know, a couple of the classes I had picked out. So I was like, no. I had missed the enrollment deadline because they
everybody kind of starting at the same time because they have forums and stuff. And they want everybody to be around the same part of the class. Gotcha. And so, yeah, anyways. But I did, I'm going through the videos of two of the classes. And I'm not sure that I'll actually do the homework because I've just been really swamped with family and work stuff. Yeah, totally. But I have been going through the videos of Game Theory and Probabilistic Graphical Models. Awesome. The Game Theory one is just kind of high level. There's not like programming or anything involved in it. And so that was kind of interesting class.
yeah, it's very intriguing and interesting. And Probabilistic Graphical Models is turning out to be quite interesting as well. There's not an easy way to describe it. And anybody who's not very technical or math oriented would be like, 'Why are you taking this for fun?' But the idea is like, how do you represent dependencies? So their basic example, just to be short, is like, you know, if you want to predict somebody's grade in a class. Like the grade in a class that they're taking is going to be dependent on their intelligence, their SAT score, maybe. You know, how hard the class is, you know, just like various other factors. And so you can kind of draw like a dependency graph. So like maybe SAT is really only dependent on intelligence, and intelligence can also be a factor of what the grade in the class will be. And so then you don't really need it. And you kind of just draw connections between all these different factors. And then you assign weights to the probabilities of various conditioning happening. And you can kind of work out things that at first would seem very, very complex to determine. And I assume it will get even more complicated as it goes. But basically having these models be created by just looking at the data and the algorithms to take data and generate the models.
So yeah, well, basically Bayesian Networks is, or they're also called like Deep Belief Networks. This idea that like, if they try to figure out if things are correlated, like if one thing can influence another or not. So in other words, like if you tried to see if there was like a Bayesian relationship between when you flipped a coin the first time and when you flipped a coin the second time. Like a Deep Belief Network would tell you like, no, you know, you flipping a coin the second time is not influenced by flipping it the first time. Like if the first time you flipped it and you got heads, the second time, you still have a 50-50 chance, you know? But like something else like weather would have a Bayesian relationship. Like if it rained yesterday, it's probably more likely to rain today than if it didn't rain yesterday. You know what I'm saying? So yeah, so that sounds awesome, man. I mean, I think I might start looking at that class myself. That's pretty awesome.
But one interesting thing that I would like to talk about is that I've done online classes as part of my, you know, formal education. But I'm doing them here. It's interesting that you're watching the video. And normally people at work think I'm weird, but I do my programming and I'll have one of these videos running in the background. So I'm like half listening to it. Oh, that's great. But every so often they'll interrupt the video with like a quiz question. And it's not like scored or anything, but instead of asking about something you've just seen to see if you're paying attention, they actually ask you to answer something that they haven't quite covered yet. Like the next thing they're about to talk about. Like, you know, so they draw some probability and then say, what would the probability be? And then give you multiple choices. So it makes you like in class when you're actually sitting there, you're kind of having this interactive. What is it? The Socratic method or whatever. Right. Like, how would
would it be? And so they're kind of trying to elicit the same engagement from you. And I thought that was really interesting. Yeah, that's totally awesome, man. It's killer. Yeah. And the first couple of times I rewound the video, I just went back. I just clicked back. Not like there was an actual rewind. I went back in the video because I thought I wasn't paying attention. And it was like, oh, wait, no, they're asking about something that hasn't occurred yet. Nice.
Wow. You're on point today, sir. We have a news story about this. So this is actually by, I guess he's a fairly popular blogger, Jeff Atwood. I think he currently works at Stack Exchange, which is a startup most of our listeners have probably seen before. And he had an article he's writing about. I guess he actually posted a video that somebody else about a presentation and then a link to a website. But it was more just the notion I wanted to talk about, which was that he points out that we've kind of almost regressed in the advancedness of our IDEs. So integrated develop environments that he's talking about. If we could do better at trying to show programmers the impact of what they're writing, that they could think through the possibilities much more quickly. So that's kind of vague and okay, I don't know what you're talking about. But at a basic level, if you've ever used Eclipse, first time most people use Eclipse. Maybe less so now because other things do it as well. But they draw the squiggly line, you know, like the incorrect spelling under any sort of compilation error that you have in the code. And you can just kind of hover it over and it tells you what the error is, offers suggestions. And this is like a very quick way to actually help you correct the problems. And it's a delight.
Yeah, I'm at the point now where like I purposely, so for example, like let's say I need to reference something in another file. Like rather than like add the statement at the top, this is like include this slash this slash my file. I just put what I want in the code. I wait for Eclipse to squiggly line it. And then I go over it and I, yeah, and I have Eclipse at the line because I'm too lazy to do it myself.
So I'm lazier than that. I just add a bunch and then I do Control Shift O, which basically has Eclipse try to guess at what the right imports are and ask you if there's any sort of ambiguity. That's hilarious. I didn't know that existed. Yeah. It also deletes anything that you have that isn't being used. Like if you have any imports or includes that aren't being used, it goes ahead and removes them for you.
So, but so that's like at a basic level. But the things that are referenced in this blog article are also even more advanced, which is like if you had like a pathfinding algorithm or a PID proportional integral differential, a method of controlling something. And you wanted to see how it would be affected based on various input conditions. You could do graphical representations of it, you know, and allow like a person to have a slider and see what would happen as they adjusted various thresholds in their code, or even better. Like, you know, take it up a higher dimension and show them like a plot of, you know, on X, Y. Here's what various inputs and settings for two different variables will give you. And just like, wouldn't it be cool if we could think about this and kind of try to make our development lives easier by doing this?
Yeah, that's awesome, man. I feel like, you know, like IDEs do a pretty good job about code, like, but like, you know, about analyzing code, fixing code, things like that. But like that kind of stuff is actually really hard to do. Like they spend a lot of time because there's lots of customers of Eclipse, right? But for most things like scripting engines for games and things like that, it's just in general, it's just like really hard to do this kind of thing where it's like you're constantly simulating what would happen as you're making code changes and stuff. But I mean, if there's some way we could like we could make that happen, like in a way that is like generic, it'd be amazing.
Yeah, it's one of these things that like doesn't seem like look at just even just undo, right? I mean, think about undo. Undo. You have to there's only two there's only two ways I can think of anyways to implement undo. One is for everything you could possibly do in your program, you have to have another function that undoes it, right? Right. That's pretty hard. And then you have to keep a list of all the things you did. That probably isn't the way most undoes are written, right? The other way is that anytime you do anything, you keep like a snapshot. And then undo is just going back through these snapshots, you know, but both of those are hard, you know, one means you have to be able to serialize, which means serialize means turn like a bunch of data that you have in memory, or like a bunch of things that you're representing, like turn them into a block of ones and zeros that you can load back later. So you either have to like serialize everything so that you can deserialize it when you do the undo. And then just keep all of these serialized copies of the world around, or you have to, as I said before, create like a reverse function for everything. And I mean, think how hard that is. Like, let's say you have a function that, you know, deletes a line of code. Well, the reverse function has to, like, you have to have the line of code somewhere so that it can know what you deleted. You know what I mean? That kind of, just adding an undo is pretty hard. And so something like this is really hard, but it'd be totally awesome if it was, if we had it. Oh.
Yeah, and I think there are some things that get closer to that. Like, I don't know if you've ever watched any of the demo videos for the Unity game engine, but they have some stuff that's kind of like that, where it allows you to kind of just jump in the game very quickly and see the effects of the changes you've made. And then kind of jump back out. Oh, nice. Yeah, I haven't seen that. That's pretty awesome. Or maybe I'm making that up. I think
Maybe I'll have to send you some videos to prove it. All right, so next article before you lead me into it. So there's a lot of news coming out about the next generation of game consoles. The Wii U, I think, is supposed to be out this year or very soon. And the next generation, Xbox and PlayStation, are supposed to be out. And I don't think that Wii U has had anything about this. But the Xbox and PlayStation have both had these rumors come out that they're going to block the ability to have used games. So either the distribution will be all online, so think like Steam, or that you'll have these one-use codes, which are becoming more and more common in games for at least some portion of the features, that you have to enter a serial number or some sort of activation code to be able to play the game. And now it's mostly used to play it online or to unlock some special in-game thing. But in the future, you could actually just block the game if you don't have this code. And that will not allow people to sell the games when they're done playing them. Right, right. So what will happen? What will that do? So will that make the prices of games lower because now people are making more money? Or will they not sell as many copies because you won't buy a $60 game knowing that you can sell it back for $30 later?
So the way I see it is, you know, I have an Xbox 360. But I don't think, oh, for my birthday I got a game that was new. But aside from that one game, I play only games that are like two years old. And I just find like I go and look at the magazines online or whatever and find like what were the best games of two years ago. And then I buy them really cheap used. And I have no desire to play
I love playing really awesome old games. And then I can get them for like $5. And I much prefer doing that. And this way won't allow me to do that. But then if you look at something like Steam, I do buy games on Steam. Games that are, again, you know, like nine months, a year old. And I can buy them for very, very cheap.
So what you're saying is like if they keep reducing the price over time as the game gets older, then for you as a consumer it's effectively the same. But the difference is instead of GameStop like getting this like essentially like arbitrage, you know, you're buying it straight from the publisher again. And the person who would have sold the game new can't.
So the yeah, so I don't know exactly what I'm saying, but I do think there is an interest here like to see what will happen. And like one part of me says like, oh, it's in their, you know, ability to ban used game sales if they want. But then the other part of me says it's my freedom. Like I should be allowed to sell my game. Like I own the game. And I don't really like the idea of we're going to this subscription or license-based model. Like you bought this software CD, but you didn't really, you don't own the software. You own permission to rent it for an unlimited time, but only for you. And I don't really like that.
Really? Yeah. You know, a lot of people come up with this argument like with music and movies and things like that. Personally, it's never really bothered me. It only bothers me when Steam goes down. And then, you know, it's one of these things. It's so funny, right? Like I could go weeks without playing a video game. But then like Steam goes down and it's just like instantly it's like, oh, I really want to play and I can't. You know what I mean? Like it's, I don't know, it's like the forbidden fruit, I guess, or something. There's some like psychological thing going on there. But yeah, it's really frustrating when you can't play because it's down.
That's one aspect of it. So always being needed to be connected to the internet or at least often being connected. That's kind of annoying because you could be extended periods of time where you're not. And that would be bad. But the other thing is like I run into occasionally where like I have a computer and then I have my laptop and then my wife has a computer. And I feel like, you know, I want my family, like I don't want to give it to other people, but I want my family to be able to share one version of this. And I don't like the idea that I have to keep buying it over and over again. Oh, that's true. Amounts to like one shared use because it turns out like we're only ever using one at a time. Like I'm not using it at the same time as my wife, at the same time as, you know, my kids. Like it would only be only one of us using it at the same time. But we want to be able to have it on different computers at the same time.
Yeah. Like Steam kind of allows that, but like other things don't. It's pretty BS that like you can't have two instances of Steam running at the same time. So like in other words, like I couldn't play one game and my wife play a different game on the same Steam account at the same time. Like that seems ridiculous. You know what I mean? Like not being able to play the same Steam game. I mean, I personally think that's not entirely fair, but I understand how that could be exploited, you know. But if you have three games, you should be able to play all three of them at the same time.
Yeah. Prince of Persia. Definitely no DRM on that. Came out 23 years ago. But did you ever beat that game or get like really far or anything? That game was so hard, man. Like we all played it when we were kids. And I don't know anybody who got. You had actually had different themes. Like you started off in the castle, and then you went to like some cave theme, supposedly. I think it's. I don't think any of this really exists. I don't think I
That's what I'm saying. Like I don't know anybody who got past the second or third level of that game. And it has like supposedly like 20 levels or something like that. What? Yeah. It's like. There's another game like this. Oh, it's like Frogger. Like did you know Frogger has like all this complexity? Like after you get past this. Like the first level, you have to get all the little frogs in the top, right? Then the next level, the cars move faster, and there's like a snake that goes on the sidewalk before you get to the turtles. I think
And like it just gets crazier and crazier. The third level has like alligators. And then like the fourth level has like other kind of cars. The fifth level, like it gets to the point where like you have to save your girlfriend Frog, and she has to go in the same hole as you. Yeah. No. I'm serious, dude. You have to watch the video or something. It's crazy. Anyway. Okay.
Yes. So the guy who wrote Prince of Persia lost the source code. And I actually. From the article, I don't think it talks about how he lost it. Let me just scan it really quick. Along the way he misplaced it. Yeah. So somehow he lost the source code. Maybe a hard drive crash. I don't know. Yeah. And you know, I guess forever. Like you know, he always kind of wanted to go back and have the original. I'm sure it's been recreated a hundred times. But there's a lot to be said for having the original code with the comments. Yeah. And like maybe the jump used to be longer. Lack of comments. And he shrunk the jump speed or something, you know. Like always like you see the organic nature of like the game, right? So it turns out his father was going through some of his old boxes and found the source code on some floppies, and he's going to hit him and he's got somebody who's going to port the game—I'm assuming to SDL or something, something where we could play it on modern PCs and then open-source the whole thing. Cool. So yeah. It's pretty cool. You know. I mean when they open-sourced like Doom and Quake 3 and stuff like that, it's always like, 'Yeah, I'm going to make my own game.' And then you get into it, and it's like, 'The code is such crap. Like you'd be better off buying a game engine,' you know. But.
Yeah. Actually, yeah, you're right. We should do this justice. So, the code's crap. No, but no, but basically, you think about it. You know, Prince of Persia. You look at the video, and we have the link. You can watch a video of the gameplay, and you think, 'Oh, you know, I could hack that up. I could write that in JavaScript or something, like in HTML or something.' You know, but at the time, this totally pushed the limits, like having a panel that fell when you walked on it. It was like, whoa, big deal. Like he had to write his own memory manager to make that work, or whatever. You know, because so, so because of the—because of the, you know, for one, the technology being new, like either they were pioneers inventing the technology as they went, or you know, it had just come out a combination of that and just the limited computational resources. There's just the code is almost always full with filled with tons and tons of hacks and things like that. Yeah, but that in and of itself is interesting to read. So when the source code comes out, we'll definitely revisit this and you know take a look at what sort of craziness so you're going to
Well, keeping in my theme from last week or two weeks ago of completely absolute opposite of tools and instead things that waste your time. This week, not that I've had much time to play it, but I did play a little bit of DC Universe Online, which is a massively multiplayer online role-playing game, and it's free to play now. And I'm—a lot of these that are free to play, like I've tried them a couple times, and they always want you to end up buying something.
That's really cool. And I didn't get very far. And like I said, I've been pretty busy. So I pretty much just installed it and played around for like an hour or something. But it is really cool. So if you're interested in being some of the cool comic book heroes, check it out: DC Universe Online dot com. And it's free to play now. And as far as I can tell, you can do a lot in the game without needing to pay anything extra.
So how many people do you play with? Like I know it's massively. So there's different kinds of massive multiplayer, right? Like there's Yeah, Eve Online, where it's like literally thousands and thousands of people in one universe. Yeah. And then there's also like on the other end of it, there's Yahoo Games, which has like 10,000 people playing chess, but you're only playing with one person. You know what I mean? Right? Both of those are considered MMOs. But you know, so where does this fall in that spectrum?
So I don't know, I never ended up kind of like doing a mission with somebody else. But it kind of depends on the mission. Like there's some stuff you can just kind of go off and do yourself. And then there's other stuff where you can be in a party of, you know, probably like six or seven people. I don't remember exactly how many.
Yeah. Yeah. Because I was playing Star Trek Online, and they gave you like a little bit too much freedom with the character customization. And I'll have to put a screenshot or something. But it's pretty epic, like. I have this. I have this character, and she has. So you know, they have like a ton of sliders. Like you ever play this game where it's like, wait?
No. No. No. No. It's not. It's not. No one's going to be offended by this. But but they have, you know. Like, you play like I think Skyrim has this, where it's like they have a slider for your nose, and you slide it all the way, and you get a huge nose or a tiny nose, or slider for your cheekbone height, or whatever. All this crazy stuff. So this game was like that. Just pages and pages of sliders. And so I made a character that had huge legs, like really, really long legs, and then extremely tiny waist, extremely tiny torso, extremely tiny neck, huge arms, and then a huge head. And so it basically looked like a four-armed octopus. And like because the legs were so big and disproportionate, the character floated. This is on Star Trek Online. The character floated above the ground because it was so glitchy. The character like the legs clipped through each other when the character walked. And the head like clipped through any doorways because the character was like eight feet tall or something. And so and the arms were so long that they cut through, you know, the legs too, like constantly clipping through. And you know I played a mission, and literally like I'd walk around like the hub area, and people would just give me money and laugh at me because my character is so bizarre. Then finally we did a mission, and at the end of the mission everybody did some kind of like dance or salute or something. And and my The rig got so thrown off with these proportions that the character actually The arm inverted. Like the arm went inside. Like through the Inside out. Through the arm socket. And like back out the head or something. It was totally awesome, man. Yeah. My character, like purple skin.
But this guy was part of an indie game hackathon where it's like make a game in a day kind of thing. And you know, he had some background in audio, and so he made an 8-bit sound generator. And it's pretty cool. Like you can actually put bullet, and it will like click random, and it'll make a bunch of random like 8-bit sounds with square and triangle waves. That kind of sound like a bullet. Like it's hard. I don't have any background in audio, so I don't really know how to explain it other than to say you know there's a bunch of buttons, but not so much that it's overwhelming. And the buttons are pretty intuitive, like like angry or something like that. You just sort of like weld all these adjectives and nouns together and then click random a bunch of times until you get the kind of sound you want. But it's really cool. Yeah, makes some great sounds. Exports to WAV, which you know then you can put in Audacity and export to AUG or MP3 or whatever. And yeah, I definitely if I ever need to make sounds for anything, I'll definitely be using it again. So good stuff.
Yeah, I think there are other ways to make games too. But yeah, probably. Anyways, yeah. So the history of C++. Now C++ is obviously the plus plus. Meaning that it wasn't the first. So there was something before it, and we've talked about that before. That was C. Yep, totally. And somebody had the idea to kind of make C better. And that person was—would you care
Yeah. So he, Mr. Straustrup. We're going to get all sorts of corrections, I'm sure. Yeah. He had the idea that you know, he wanted to add classes to C. And so with you know, with anything as C was coming out, people were hesitant because they wanted to write everything in assembly. And then as C++ came out, you know, oh we're going to have these classes thing. He wanted to be able to show people, look, it really is just kind of like C but better. And so the original compiler was actually just to compile C++ to C and then use the existing C compilers to generate machine code.
Yep. I mean, if you put yourself in his shoes, like people had been really working on C compilers for a long time, you know, and they were highly optimized and things like that. And you know, you want to invent your own language. If you make your own compiler to assembly, it's not going to be anywhere near as good as C, you know, because they've done so many optimizations. So you know, as a first step, it makes sense he wrote the C++ compiler to go to C and then you can use your highly optimized C compiler. It's sort of like what's going on now with LLVM.
Yes. Yes. And it I mean, people eventually you know started to come around and say, wow, this is nice. But there was the original versions, and you'll still hear if you go to somewhere where people have been programming for a long time, especially performant programs for a long time, a lot of people still have a hesitancy of C++ because they think it's so much more inefficient than C.
Yep. Yeah, totally. I mean, I think that one of the things too is and you know we've touched on this before, but just to recap, it's in the end it really comes down to how you use the language. Like for C++. For example, you know as we mentioned, it's C with classes. If you have you know let's say you're doing some kind of like fluid dynamics, right? And so you have a bunch of a bunch of cells which have water in them that are moving in a certain direction. Well, if you make a separate class for every cell and then you try and do some kind of fluid dynamics where you go to all the classes and then in each class you do some like computation, that's going to be way slower than just having an array of floats, right? Or something like that. So like that's an example where like you don't really want to make a class for like everything. You just want an array of floats and then maybe just one class that does like the fluid dynamics for the whole world or whatever, you know. So you know in the beginning with most languages people it's a combination of the language isn't optimized but then also people don't really know what to do with the language. But then yeah over time people start you know the language as it gets more adoption people start to understand the best ways to use it.
I did look into it a little. You know having some background in C++. I was very intrigued. Like oh they're upgrading. You know what are they going to add? Like how are they going to bring this into the you know kind of 21st century. And so it was originally you know going to be called C++. You know, 0x or whatever, you know implying that it would come out in the single digits of the 2000s. But they kind of missed that deadline and getting agreement on what they were going to put in. So I think the official name ended up being C++11. Oh, I didn't know that for coming out in 2011. I always thought
ratified. Or whatever. Got it. And so C++11, which we haven't talked about, all about what C++ has. But they just try to make the language a little easier to use while at the same time keeping it being powerful and adding, trying to add some stuff for, you know, the idea of multi-threading and concurrency, which wasn't as big when C++, of course, was in earlier versions. And then just making some things nicer, like kind of inferring what values should be, as opposed to having to make them explicit. Or if you have like a constant function, you can create an array by calling that function and you know, like adding a number to it. So like if you have a function that just returns the number five, you can make the size of the static array be, you know, calling this function plus two, and it'll be—it knows to make it seven. Whereas before, that would have been illegal because in C++, the static arrays, you know, have to have a constant size definition.
Gotcha. Yeah. One of the big things that's in the new C++. That, you know, it's probably—actually, it's probably not that big, but it's really important for me anyways because it's good for people who want to develop quickly. It's something called a shared pointer. And so what this is is as we talked about in the C podcast, you know, C++ and C, most of the time you're dealing with pointers that you allocate. So you say, you know, hey computer, give me a chunk of memory so that I can do something with it. And usually, you know, in C++, in C, you'll say malloc. In C++, you say new. But they do like close to the same thing. And then later on when you're done with that memory, in C you would say free, and then the chunk of memory. In C++, you say delete. But it's the same kind of thing. Once you're done with the memory, you have to delete it otherwise, so
Right. Right. So and if you don't delete the memory, then eventually you will run out of memory because you'll just keep creating asking the computer for memory and not returning it back. Right? You'll leak memory. So what shared pointer does is you wrap your new inside the For a shared pointer, and it knows that. It knows when to delete it. So basically, it does what's called reference counting. So if you say like SharedPointer X equals New Int, well now like X points to a reference. X contains a reference saying hey right now X is using Int. So you know we got to keep it around. Then later on if you say Y equals X, now there's two references. So like behind the scenes it's saying hey, you know, two people, Y and X, are both using this integer. So we can't free it. And then eventually your program keeps running, keeps running, and then just as part of the program, Y and X let's say fall out of scope, like maybe they're part of another SharedPointer, like they're in a class that is itself contained in a shared pointer, or your program ends, or whatever. It says hey, there's no more references to this Int. So now I can free this Int. So it does all the freeing for you. That's one really nice thing that was added in the new C++.
I'm okay well we'll explain it just really quick. Garbage collection is the idea that you know over time there so some languages like Java you don't free things and but because of there's like actually so no to answer the question now now that I thought about it, no, it doesn't implement garbage collection. And so what that means is let's say you have a shared pointer X that points to some class and then that class has a shared pointer Y that points to X. So you have a loop, right? So X refers to something that has Y and Y refers back to something that has X. In Java the garbage collector will run every now and then and it'll say hey, you know these two things are referring to each other but they're nothing refers to them. They're isolated and there's actually no way to get to X because there's no way to get to Y and Y is the only thing pointing to X. So so we're going to delete X and Y. That's garbage collection or that's one part of it. So C++ doesn't do that. So actually if you have a circular reference and you don't sort of clean it up yourself, then you'll leak memory that way. So that's actually a really good point, very subtle but it happens.
Yeah yeah so I mean it is kind of a loose approximation of it in some ways, but some people don't like garbage collection because the idea that and in Java has gotten better about like allowing you to specify when and how garbage collection will run, but the idea that something is going to run that you don't have control over and a lot of uses of C++ people want to have absolute control over everything that's running and when it runs and how it runs.
Right, like you'll see in a lot of games, especially like the critical, you know, graphics or physics loop in a game, they allocate everything up front. So, in other words, you know, they create an array, like right when the game starts, they create an array for, let's say it's NBA Live, some basketball game. They create an array for 30 players, like they include the players on the court and on the bench, right? And for 30 ragdolls in their physics engine. And that's it, you know. And so when the first player gets initialized, like after you pick your team—you pick Philadelphia 76ers or whatever—and it starts initializing players, there's already locations in memory for those ragdolls, and then it just kind of fills in the specifics. But all of the memory gets allocated up front. And that's because, as Patrick suggesting, like you don't want to be allocating and deallocating memory, you know, while the game is running, because what that means is you have no way of guaranteeing how long something will take. Like you're if you're writing C++, you're often in an environment where your boss is coming to you and saying, 'Look, Patrick, you have 10 milliseconds.' You know, like you have every cycle. You have 10 milliseconds that's probably a lot. You have one millisecond dedicated to, like the graphics engine. And so if you ever take more than a millisecond, some red flag is going to go off, right?
These are the big things that, you know, as we mentioned, C++ is used for like really time-critical programs and um things like that. But you might ask yourself, why not use C? Like what does C++ really bring to the table? Right? And one of the biggest things is obviously classes and the things that come with classes, like virtual functions with inheritance. C++ actually has multiple inheritance. Um, you know different—wait, what does that mean? Yeah. So so in Java we might have talked about this inheritance, but basically let's say you have a class, like let's say you have a class called Shape. Shape that's amazing. Okay. So you have Shape and then Square and Circle both inherit from Shape. And Shape is an abstract class, which means that Shape contains some functions that aren't implemented. And because it's abstract, you can't actually create a Shape. So, you know, in C++ you would say 'abstract class Shape,' right? And you'd give it a function, let's say Area, getArea. And getArea would be what's called a pure virtual function. So in C++ lingo, pure virtual function means that someone else who's implementing or inheriting this class has to create the getArea function because the Shape class can't do it. So then you have say Square and the Square class inherits from Shape, and Square class will define getArea. So it's like, you know, int getArea and then do side times side, right? You'd also have the Circle class, and the Circle class inherits from Shape as well, and its getArea function would do what radius times pi, right? Or radius was it radius squared times pi or something for the.
Squared. Yeah, that's right. So so it's two different classes that have different getArea functions, but they both inherit from Shape. Then what you can do later on, let's say you're making a physics engine or something, you can have an array of Shapes. Even though you can't create a Shape directly, you can do what's called downcasting. So you can have an array of Shapes and then you can say, 'Oh, I want to add a Square to this array.' So I can say like shapes dot add new Square, shapes dot add new Circle, shapes dot add new Square for another square. And then you can go through this list of Shapes and get the area for all the Shapes even though, you know, you don't necessarily know which one's a Square and which one's a Circle. You know that both of those have the getArea function because they extended or they inherited from Shape. So that's kind of in a nutshell inheritance. Now, um what multiple inheritance means is kind of what the name suggests: you can inherit from multiple things. And this is really useful. So especially let's say um let me think of an example. Let's say let's say like a Square is a Shape, but a Square is also something that can be drawn, like a renderable. So you're making a game. You have this these Squares. Let's say you're making like Angry Birds or something, right? So uh and you have something called Circle which is going to be your Angry Bird. So Circle is a renderable, like you want to actually draw, you know, a little bird-shaped circle. But then it's also a Shape in the physics sense, like you want it to bounce, like a circle. So your your class, your Angry Bird class would extend—would inherit um Shape and inherit um Circle at the same time. So multiple inheritance actually gives C++ a lot of power.
And Java does try to handle the same thing, but it does it through what's called interfaces. So you can kind of inherit multiple inheritance by inheriting multiple interfaces. But interfaces are restricted in what they can be, and C++ basically allows you to inherit from multiple things which are more complex than just an interface.
That's right. Is that yeah, you're totally right. So yeah, and C++ is just a class; you don't put abstract in front. In Java, you put abstract in front. Yeah, but if, as Patrick said, if you have any virtual functions, and the way you make a virtual function is you put the prototype, like in the case of the Shape, you'd put, you know, int getArea, and then parentheses. But then instead of—oh right, yeah, virtual and getArea in parentheses, but instead of putting, you know, function there, you'd put equal. Right? You'd put equal zero, and that lets the compiler know that anyone who extends or anyone who implements this function or this class is going to have to create the getArea function.
Yeah, they must override it because you can't come up with a good generic implementation of it. Right? Yep. The other thing that C++ allows you to do is something called templating, which a lot of high-level languages allow you to do, but this is basically instead of having to have so like in let's say getArea, you might want to be able to get areas with integers, but maybe also doubles or maybe I don't know, like imaginary area. Is that—
Don't know, complex number area, I guess maybe anyway. So you might want to have like all these different things, but instead of having to write if the—the way it's handled is very similar, then you may not want to have to write like getIntegerArea, getDoubleArea, getFloatArea. Right? That would be very tedious. So templating allows you to write the function once. If Area is just let's say in the case of just, you know, squares and rectangles—you know width and height—and you're just multiplying together, and it doesn't matter what type they are, then basically allows you with a special syntax to define like hey, the person has to specify what type it is when they call this, but I don't care what type it is, or even like I care, but only in a certain way. And so that, then it's just, you know, width times height, and you don't care if those are integers or floats or doubles; it'll just kind of handle it for.
You right. Yeah, it's really interesting what the compiler actually does with templates. It looks at all the times you use the class and it creates that many copies of the class. So in other words, let's say you had a list and you had like the List class was templated. Then later on in your program, you have a list of integers, and then later on you have a list of Shapes, and then later on you have a list of floats, like throughout different places in your program. Well, the compiler will count all those up, and then it'll actually take your List class and create copies of it. So you'll have like a FloatList, you'll have an IntList, you'll have a ShapeList, but it does all this for you like behind the scenes so you don't have to actually go through all the trouble of duplicating, copying, and pasting all of your own code.
Some things that you need to know at compile time, but you need to use equations. Let's say you have three plus four in your program. Well, you know the com—the computer, the compiler doesn't know that that's seven. The compiler like you have to actually run the program, and then your computer once running the program will do the three plus four and then return seven. But there are some things, like some computations that need to be known at what's called at compile time. So like while the compiler is compiling, it needs to know that this three plus four is seven or this X plus Y is whatever it is. And so to actually do this plus at compile time, you can—you can actually do things like this using what's called, as Patrick mentioned, template meta-programming, but it's pretty—it's very rare that you have to do something like that, but it does come.
Else that C++ adds is operator overloading. So once you define a class, it would be really nice if you could do something, you know, like I want to add two of my classes together. Like my classes represent probabilities, and I want to add these two probabilities together and have them be added in a way that is unique to the probabilities of my classes that are defined. And so I want to redefine how the plus is—what happens when I say, you know, my shape and then the plus sign, and or, you know, my probability, the plus sign, and then, you know, another instance of my probability, and C++ allows you to do that. So you can still use the plus sign, but it knows to call special code that you wrote to do that operation.
Sure. The one we—you know, there's many, many things that we can continue to talk about, but another one we talk about is that C++ adds exception handling. And this is kind of a controversial topic, you know, whether or not exception handling is something that should be used or shouldn't be used, how expensive is it? How cheap is it? And everybody's got their kind of thing, but C++, if you want to use exception handling—so throwing and catching exceptions—it does add that capability. So,
Interestingly enough, I'm really glad I looked this up. So Java, you know, doesn't support operator overloading. So what that means is like, and the example they use is there's a class in Java called BigInteger. As the name suggests, it can hold an integer of effectively infinite size, right? It'll just keep allocating memory and as much as it needs to hold any size number. And so to add two BigIntegers, you actually have to say A dot add parentheses B because you cannot do A plus sign B like you can in C++, but Scala, which is a language derived from Java that we probably should do a podcast on sometime, Scala actually does have operator overloading. So okay, that's pretty wild. Yeah, that's cool, but yeah, C++ as Patrick mentioned, clearly has operator overloading, and it's amazing so useful it is.
Yeah, I don't think you should do that typically. If you operate—if you overload an operator, there's this understanding that you're going to do something that's intuitive to everybody. Um so like even if you could, you know, let's say add two graphs together, typically you'll use a function for that just because you want to have a function with a descriptive name, like maybe—like merge graphs with edges or something like that. Like you want to let the person know kind of when you add these two graphs together what are you really doing? Um if if you if you do like just an operator A plus sign for two graphs, then what somebody else thinks adding two graphs is might differ from what you think. Yeah, that can cause.
Like just to give an example. I wrote a—I actually wrote a NES emu, which is just a name I made up, but basically it takes pictures and it does like takes the NES, like the old Nintendo palette, like the color palette for Nintendo with like 54 colors or something, and it—it does like this K-means clustering. It does like way more than it really needs to, but it tries to make the image look like it would if a Nintendo video game designer drew it. So you could just give it a picture of your face, and it'll look like your face belonged in a Nintendo game. But awesome. But yeah, I wrote it in Python, and it takes like probably 10, 15—it probably takes about a minute to run. Um so, and to test it became hard to debug because you know it'd literally take a minute. I'd find a bug, it'd take a minute. And I rewrote it in C++, and it runs almost instantly now. Now there—
Yeah, that's a good point. So Python actually ironically enough has what's called C types, which adds support for C and C++ like data structures. And I could have used the C types array and stuff. So don't take that as a—as a slam on Python or anything like that. It's not like your code will just—it's not magic or anything like that. Um so, you know, but yeah, if you just code normally, your code will typically be much faster on C++.
So another thing is that object-oriented allows for collaboration amongst very large teams in an easy way. So Jason's working on the Shape class, and I'm going to inherit from it, and I don't really—well, that's a bad example. So we both have the Shape class, and I'm going to implement Square, and he's going to implement Circle. And you know, as long as if we have kind of the interfaces between those defined, we can all work at the same time on our own pieces of code, and that's very
nice. Yeah, definitely. I mean, it makes it very easy to write, you know, APIs. And one thing about C++ is that a lot of languages don't have; C++ has separate header files and source files. And so the idea is the header file, also called like the .h or the .hpp file, will sort of have a description of what your class looks like. So it'll say, 'Hey, you have this class, it's called PatrickWheeler.' You know, it has this function called getArea, but it has these—like it has these functions, you know, you have this like Shape class. It has getArea, it has, you know, some other thing like getBounds or getCenter or something like that. And then it also in addition to the .h file, there's a .cc file or .cpp file, depending on what extension you use, but both of them are the same, which actually has the implementation. So if you have some class—let's say it's FileReader. You know, the .h file would just show like there's a function called read, there's a function called readLine, there's a function called close. But then if you go to the .cc file, you'll see actually what read does and what close does, you know, what readLine does. Um and so this is really nice because you can give people the .h file, and they can get an understanding really quickly of what your program does without having to—or like rather, what the architecture of your program is without having to know what each function does. So if you're on like a huge team, you know, and the other thing is the .h files, you might make those like up front and not change them very often. So let's say I'm working on some graphics engine. I can just hack together some .h files. I can say, 'Okay, I know there's going to be a GraphicsEngine.' and it's going to have a list of shapes, and the shapes there's going to be a Square and a Circle, and there'll be some getArea functions and some rendering functions. And I'll just send this to Patrick so he has an idea of what I'm going to do, and then he can code to those .h files, and he can include them in his code, and then you know we can both work in parallel that way. So it's a great way to communicate. The language itself allows you to do things like that.
So some of the weaknesses—um we talked about that you have absolute control over the memory. And so it's in a similar way just in having memory leaks, you can also have memory corruption that you know, you can overwrite other pieces of code. If you're—you know C++ has a notion of a pointer. So it's this variable holds a memory address, but I can change what the memory address points to, and as long as if my program has permission to write there, I can write whatever I want to.
That right? Yeah. I mean, a big case where things like this happen is—let's say you have an array and the array has the array has 10 elements, and you're looping through the array. Um so you might have a loop that says like go from 1 to 10. Like you might say at the top it might say, 'Okay, int size equals whatever the size is.' So now my size variable is set to 10, and then I write this for loop that goes from 1 to size. But—I never try to recalculate size. I just know it's 10, and I go through this for loop and do some stuff. Well, what if my program is multi-threaded, and while I'm in this for loop somebody else goes through and deletes like a few elements? Well, now I'm going to go through 1 to 10, but there's only five elements. And C++ it won't give you an error or crash or anything like that. It'll actually just read whatever memory is there. You know, and like this happens the worst case where this kind of happens—even worse than adding element or removing elements is you might add an element. So let's say you add the 11th element, and that causes your vector to shift to another location in memory, and you just end up with all sorts of crazy stuff where you know you're just you're reading memory that doesn't make sense, and but you're not crashing. You're getting data. It's just bogus. And so these are incredibly hard to debug, and this is why a lot of old school games have these like really esoteric or really like interesting errors. Like, have you ever seen the Negative One World in Mario? No. Oh man, you should watch his YouTube video. There's there's a glitch in Mario, and you can exploit the glitch and actually go to the world negative one. Um there's another bug in Castlevania 2 for Nintendo where you can—and these are both written in C, but the same kind of bugs can happen in C++ where in Castlevania 2 you can actually fall through the ground at like a very specific spot and you're in like random memory in the game. And so like effectively, some percentage just based on, you know, randomness or whatever of the ground is fall-through, and the rest of it is solid. So like you're not stuck, and you can actually people have like just spent days just going through this crazy world where you're just walking through the memory, and everything's crazy. And one guy got to the spot in Memory where the time was being like, you know, put, and so like as the seconds ticked, like the world shaped in weird ways. It's really trippy. And so but the point is like you don't get a crash with like, hey, at this point in your code, you know something weird happened. You just weird things happen with no way to track them down. Yeah. Yeah, so that's my soapbox. All right.
Plus. Yeah, yeah, let's jump to tools. All right. I personally really like Code::Blocks. Have you ever used Code::Blocks? I have not. Um so you know, I do a lot of development on Linux, but there was a time where I was doing my home development on Linux, but I was doing my work stuff on Windows. Now I happen to actually be on Linux 100, but at that time I really wanted something where I the same IDE on both Windows and Linux, and Code::Blocks does exactly that. It actually works on Mac, Windows, and Linux. It's very consistent, and it does sort of, you know, it's not as nice as Visual Studio, um but it does like 99% of what you need and totally cross-platform. Yeah.
About you another yeah, another cross-development, cross-platform development is Eclipse CDT. We talked about Eclipse, and Eclipse originally came out as Java only, but since then people have added some C functionality in what's called CDT, C Developer Toolkit, I think that's what it stands for. And so it basically does the same thing for C. It requires, you know, some extra setting up and handling, um but it works pretty good. Oh, nice. Yeah.
And then aside from just tools to use to write code, I also wanted to talk about Boost, which is a very popular library for C++. Which adds a lot of the features that were now added to the new C++11. So like shared pointers and stuff, and just a lot of common usages that you might want that instead of having to implement yourself, you can just use the Boost libraries version, and it tends to be very good and well.
++ is commonly used. We talked about game programming. We didn't really talk about it, but we kind of talked alluded to it, which is embedded computers—so things that are low power, low memory things where you want to be able to have tight control on everything that happens. They're used there. What else are they used? JSON or C++ used?
Uh so all web browsers—right, Chrome, Firefox, Internet Explorer—all these are written in C++. Most, you know, huge programs that you can think of are probably written in C++. And that just goes back to our, you know, our explanation of just how like it's possible to make just gigantic programs and many libraries, and, uh, you know, and all sort of coordinate. Yeah. Um.
So I think that pretty much wraps it up for C++. I wanted to say thank you. A number of people have written in to say how they're glad the show is back. It's been encouraging a number of comments and suggestions, and that kind of stuff that's appreciated as well. Um, you know, we're glad to hear from you guys. We enjoy doing this. We're glad that some of you.
Enjoy hearing it. Yeah, totally. We used to announce Programming Throwdown at Gmail. It's probably good to mention we don't follow that as much now that we have the Programming Throwdown page on Google+. Um so if you if you want to write to us, we do follow like we check it maybe once a week or something like that. So um you can definitely write to us if you have something you don't want to share to the public or something like that, but um most of the time, uh, you you definitely want to check out the Google+ page. If Google+, that's right. If you go on Google+ and you put in Programming Throwdown, it will take you to the Google+ site for Programming Throwdown. You can feel free to leave any comments. We post all the show notes there in addition to the blog. So yeah, definitely add it. Add it to.
see you guys later the intro music is aqso by binar pilot programming throwdown is distributed under a creative commons attribution share alike 2.0 license you're free to share copy distribute transmit the work to remix adapt the work but you must provide attribution uh to uh patrick and i and uh share alike in kind
