fbpx

I just got back from CppCon 2018 in Bellevue Washington. And since this is a podcast where I teach you how to program, I thought I’d share something that I learned at the conference.

It was a great conference with full days that went from 8 in the morning to usually 10:30 at night for five straight days. And then there were classes before and after the conference. There were so many choices for presentations to attend that it was tough to choose. It was packed with people. I think there were over 1,200 people attending. But at the same time, it was as smooth as you can imagine. The organizers deserve amazing credits. Even with all the people, I never had trouble finding a good spot. Sometimes I would sit up front. And sometimes I would sit at a table in the back of the room.

One of my favorite talks was from Nicolai Josuttis. This is something that I’ve been aware of since I first started programming C++. There’s a lot of different ways to declare a variable and initialize that variable so it has a value.

This explanation really needs text for you to be able to see the actual code. So I won’t be able to describe everything from the presentation. I’ll stick to the big ideas. In a couple weeks, more or less, you should be able to watch the full presentation on YouTube. It’s called “The Nightmare of Initialization in C++”.

Probably the main thing to understand is that in C++ if you have a method that declares an int variable and gives that variable a name, then the value of that variable is undefined until you write a value first. If you just declare a variable and read from it without first providing a value, then you’re in undefined behavior. The value could be anything.

The rules are changing all the time. Some of the things I learned from this talk were about how things change in C++17 vs. earlier versions of the language. You’ll also gain insight into some changes that might take effect in C++20. Listen to the full episode or read the full transcript below to learn more and make sure to watch the video once it’s published.

Transcript

And since this is a podcast where I teach you how to program, I thought I’d share something that I learned at the conference.

It was a great conference with full days that went from 8 in the morning to usually 10:30 at night for five straight days. And then there were classes before and after the conference. There were so many choices for presentations to attend that it was tough to choose.

Everything will be posted on YouTube later for you to watch. Which is good because unlike Hermione in Harry Potter, I didn’t have a time turner so that I could attend multiple talks at the same time. It took me over an hour just to plan out which talks I wanted to attend. I don’t know how many talks there were in total but the conference sometimes had about eight different talks going at the same time in different rooms.

It was packed with people. I think there were over 1,200 people attending. But at the same time, it was as smooth as you can imagine. The organizers deserve amazing credits. Even with all the people, I never had trouble finding a good spot. Sometimes I would sit up front. And sometimes I would sit at a table in the back of the room.

The largest meeting room was big enough to hold everybody at once with extra seats to spare. And I didn’t need binoculars to see the presentation. There were multiple projection screens available to watch from any seat in any of the rooms. All in all, it was an amazing conference and I’m still processing the things I learned.

Which brings me to the topic of this episode. This podcast is all about teaching you how to code. It’s not just news and it doesn’t focus on interviews. There actually hasn’t been a single interview yet on this podcast. Not that I’m against interviews. But my list of things that I want to explain to you doesn’t seem to be getting shorter.

So what’s one thing that I learned in this conference? Let’s see. I might have to split this into multiple episodes. For today, I’d like to explain some things about variable initialization.

One of my favorite talks was from Nicolai Josuttis. This is something that I’ve been aware of since I first started programming C++. There’s a lot of different ways to declare a variable and initialize that variable so it has a value.

This explanation really needs text for you to be able to see the actual code. So I won’t be able to describe everything from the presentation. I’ll stick to the big ideas. In a couple weeks, more or less, you should be able to watch the full presentation on YouTube. It’s called “The Nightmare of Initialization in C++”

Probably the main thing to understand is that in C++ if you have a method that declares an int variable and gives that variable a name, then the value of that variable is undefined until you write a value first. If you just declare a variable and read from it without first providing a value, then you’re in undefined behavior. The value could be anything.

And because it could be anything, the compiler doesn’t even have to give you a real value. It might decide to make up a value for you. After all, if you have no idea what to expect, then who’s to say that a value the compiler gives you isn’t the correct value?

You shouldn’t rely on this behavior for some strange reason. Trying to read uninitialized variables is always a bug. If you really want a random value, then there’s better ways to do this.

It’s easy to provide an initial value though. that is if you ignore all the different ways to do this. You might just write int i = 2. And you now have a fully defined integer variable with an initial value of 2.

Now why would C++ even let you define uninitialized variables if reading them is undefined? Because C++ lets you write code as you need to. There’s nothing wrong with declaring a variable with no initial value.

As long as you make sure to write a value to the variable before you try reading from it. Maybe you want to declare several variables together in one spot and you don’t yet have an initial value. C++ doesn’t make you provide a fake initial value. It doesn’t make you store a zero in the variable.

I would recommend that you declare variables at the moment they’re needed. It’s a habit from the C language where all the method variables would be declared at the beginning of a method. Even if some of those variables don’t appear in the code until later in the method.

C++ lets you declare variables at any point in your method. And declaring them at the point they’re first needed makes your code easier to read.

But there could be times where you have several similar variables and you want to declare them together. You might not yet have values to go into those variables. C++ is okay with this. Just don’t read from them until you’ve written something to them.

All of this was basic information to me. And I’ve also already been aware of many different ways to declare and initialize variables. But this talk really brought everything together. I liked how it described clearly all the ways a variable can be declared and what the effects will be.

For example, if you declare a char called c and give it an initial value of the letter a, then it’s an error to declare another char called c1 and give it the value c + 1 inside of curly braces. But if you declare a char called c2 and give it the same value of c + 1 without using any braces at all, then this is okay. Or you can declare a char called c3 and give it the value c + 1 inside of parenthesis and this is also okay.

The reason the first one fails with an error is because the compiler will perform the addition of 1 using integer arithmetic and then try to assign the integer result back to a char. Since an integer can hold more information than a char, this is a narrowing operation and could result in the compiler needing to chop some bits off of the integer. It won’t do this unless you tell it that it’s okay.

The whole point is that there’s often subtle differences between using parenthesis and curly braces. The rules are not always easy to understand especially if you’re not aware of the possible problems.

And when you include auto in the mix, it gets even more complicated. Now, it’s possible that just using an equal sign or no equal sign in the variable declaration can affect not only the value but the basic type of the variable being declared.

I hope I’ve given you enough motivation to watch the video once it becomes available. It’s about an hour long and unless you do this sort of detailed work everyday writing your own compiler or something, I can say that you’ll definitely learn something from the video.

The rules are changing all the time. Some of the things I learned from this talk were about how things change in C++17 vs. earlier versions of the language. You’ll also gain insight into some changes that might take effect in C++20.