C++: Heed the Signs!

We’ve all seen this warning at some point…

warning: comparison between signed and unsigned integer expressions [-Wsign-compare]

Comparisons and arithmetic operations involving a mix of signed and unsigned numbers creep into code all the time. And when it does, compilers will sometimes produce helpful warnings such as the one shown above.

But what do you do if you see such a warning? Maybe you say to yourself, “What’s the big deal? The compiler should be able to figure out how to deal with mixed signage, right?” You then reassure yourself that it’s in-fact not a big deal, the compiler is smarter than you, and a blind eye is turned. If you’re feeling particularly cocky, you may even disable that compiler warning altogether.

But what are you ignoring? Could there be something sinister lurking in the dark, waiting to strike when you’re not paying attention?

Treading Into Murky Water

Take a look at this snippet of code.

#include <iostream>
int main(int argc, char **argv)
{
    unsigned int a = 1;
    signed int b = -1;
 
    if (b < a)
        std::cout << "All is right with the world.\n";
    else
        std::cout << "Up is down and down is up!\n";
 
    return 0; 
}

The variable a, which is unsigned, is initialized to 1. The variable b, which is signed, is initialized to -1. And b is obviously less than a, right?

Both Visual Studio and GCC compile the code with a warning similar to that shown above. Running the code produces the following output.

Up is down and down is up!

“Hey now! That’s madness!” you exclaim.

Perhaps. But before I explain what’s happening, let’s journey a little farther down the rabbit hole with one more example…

#include <iostream>
int main(int argc, char **argv)
{
    unsigned int a = 1;
    signed int b = -1;
 
    std::cout << "1 + -1 = " << (a + b) << "\n";
 
    b = -2;
 
    std::cout << "1 + -2 = " << (a + b) << "\n";
 
    return 0; 
}

In this example, both Visual Studio and GCC compile the code without error and without warning (even with all warnings turned on). The following output is produced.

1 + -1 = 0
1 + -2 = 4294967295

“What in the world is going on here?!” In short – unsigned promotions.

Let’s see what the C++ standard has to say. You’ll find the following excerpt in Section 5, “Expressions”.

Many binary operators that expect operands of arithmetic or enumeration type cause conversions and yield result types in a similar way. The purpose is to yield a common type, which is also the type of the result. This pattern is called the usual arithmetic conversions, which are defined as follows:

  • If either operand is of scoped enumeration type, no conversions are performed; if the other operand does not have the same type, the expression is ill-formed.
  • If either operand is of type long double, the other shall be converted to long double.
  • Otherwise, if either operand is double, the other shall be converted to double.
  • Otherwise, if either operand is float, the other shall be converted to float.
  • Otherwise, the integral promotions shall be performed on both operands. Then the following rules shall be applied to the promoted operands:
    • If both operands have the same type, no further conversion is needed.
    • Otherwise, if both operands have signed integer types or both have unsigned integer types, the operand with the type of lesser integer conversion rank shall be converted to the type of the operand with greater rank.
    • Otherwise, if the operand that has unsigned integer type has rank greater than or equal to the rank of the type of the other operand, the operand with signed integer type shall be converted to the type of the operand with unsigned integer type.
    • Otherwise, if the type of the operand with signed integer type can represent all of the values of the type of the operand with unsigned integer type, the operand with unsigned integer type shall be converted to the type of the operand with signed integer type.
    • Otherwise, both operands shall be converted to the unsigned integer type corresponding to the type of the operand with signed integer type.

In short, the last conversion rule is applied if none of the other rules apply – “..both operands shall be converted to the unsigned integer type corresponding to the type of the operand with signed integer type.”

In our first example, the variable b is converted to an unsigned int during the comparison to a. What happens when you convert the value -1 to an unsigned value? It’s the same value as UINT_MAX, which on a platform with 32-bit integers equates to 4294967295. And, of course, that’s not smaller than 1 which is why we saw the output we did.

What about the second example? The first line of output seemed to work just fine. Only the second line produced unexpected results.

In the first line of output, we saw:

1 + -1 = 0

Here, a was equal to 1 and b was equal to -1. The code for this was…

std::cout << "1 + -1 = " << (a + b) << "\n";

However, if we substitute the values in for (a + b), applying the unsigned promotion to b, we have the following (assuming 32-bit integers)…

std::cout << "1 + -1 = " << (1 + 4294967295) << "\n";

In this particular case, adding 1 to 4294967295 overflows resulting in a value of 0. What’s especially interesting here is that 0 was our expected result, so this code actually provides us with a false sense that everything is working as it should.

It was only after we set b to a value of -2 that we saw weird things happen. Once again, here’s what the code looks like once b is promoted.

std::cout << "1 + -2 = " << (1 + 4294967294) << "\n";

And this, of course, equals 4294967295.

The Takeaway

Many C++ experts are adamant that the only time you should ever use an unsigned data type is when you need to store a bitmask. Signed data types should be used in ALL other cases. If you end up needing a value that exceeds the range of a given signed data type, use a bigger signed data type.

I agree with the sentiment of this. But the reality is that we don’t always have the luxury of picking our data types. We’re often at the mercy of APIs, sensor specifications, file formats, network protocols, etc.

Any time you encounter or write code that mixes signed and unsigned data types, proceed with caution. Think carefully about how the data is used, and apply a healthy dose of skepticism. And, of course, when in doubt, test, test, test.

* The STL makes heavy use of size_t, which is an unsigned type. When brought up in conversation, this point is often met with loud and uncomfortable groans. The general feeling is that allowing size_t into the STL was a mistake. But it’s something we’re stuck with for now.

Appendix B – Introduction to Windows Core Audio

For a little while now, I’ve been hard at work on a little side project. I almost hesitate to announce it at this point, because it’s still very early. But, what the heck. Why not. I’ve started writing a book. And no, it’s not one full of suspense and intrigue. Nor is it the next young adult break-out series. Turns out, I’m writing a programming book. The working title is “Practical Digital Audio for C++ Programmers”, which I admit is a mouthful. Henceforth (at least as far as this blog entry is concerned), I shall refer to it as PDA4CPP.

When I first started my audio programming journey, I quickly discovered there was a huge hole in the information available to newcomers to the field. There was plenty of material to be found regarding specific audio libraries. And there was even more material that discussed, in very mind-bendy ways, things like audio effects and synthesis that assumed you already had some level of comfort with digital audio programming. But there was very little in-between. And as a complete newb, I found it super discouraging. So I decided to do something about it. PDA4CPP is the fruits of my labor.

As I mentioned, the book is in its infancy. Only one chapter has been completed to date – Appendix B: Introduction to Windows Core Audio. But it’s a beast, coming in at 170 pages. In it, I talk about where Core Audio fits into the Windows story, the Windows audio architecture, device discovery, audio formats, WASAPI, audio rendering, and audio capturing.

Why did I start with Appendix B? Some of it was because of the questions and feedback I received from my blog entry, “A Brief History of Windows Audio APIs”. But mostly, I started with Appendix B because that’s where I needed to. Most of the book’s code will be implemented around a custom audio library that’s effectively a thin wrapper around platform-specific audio code. The Windows side of things provided as great a starting point as any.

Something I’m going to experiment with is making drafts of the book’s chapters available for purchase as I complete them. Not only will this help motivate me to keep writing, but it will also help me gauge interest. Appendix B is the first chapter available for purchase. Pricing for each chapter will vary based on each chapter’s size and density. More information can be found on the book’s page, which can be found under the “Pages” menu. An excerpt is available, as well as the chapter’s source code.

If you purchase the chapter and love it, hate it, or have ideas on how to improve it, please email me or leave a comment below.

Thanks!

-Shane

CppCon 2016 Trip Report


CppCon is an annual, week-long conference for the entire C++ community. Every year thus far, it’s been held at the Meydenbauer Center in beautiful Bellevue, Washington. It’s a young conference. This year marks only its third year. But the scale of the whole thing has allowed it to easily set itself apart from other C++ conferences. It’s huge.

This year, there were well over 900 attendees.(Edit: I had originally stated a 50% growth figure, which Jon Kalb pointed out was very wrong. Whoops! Sorry about that. Thanks, Jon! CppCon continues to grow, however!) The weekend leading up to conference is full of classes. And the weekdays are jam-packed with content, with sessions starting as early as 8AM and running as late as 10 at night. This year, there were typically six to seven tracks of sessions happening at the same time.

There’s always plenty to do and experience at CppCon. And it’s not always easy to decide on where to spend your time. Fortunately, all of the regular sessions are professionally recorded. So if you miss something, chances are you’ll be to find it on YouTube later on (the exceptions being classes, open content sessions, some lightning talks, etc.)

New This Year

There were a few things new at CppCon this year.

Last year, the committee experimented with a class offering for the weekend leading up to the conference. The class was such a success that they decided to expand the menu to five for this year.

Early-bird registrants got free CppCon t-shirts. This was a first for the conference.

This was also the first year they attempted a poster contest at CppCon. I believe there were only 4 posters submitted for this year. But this number will surely grow for next year, as attendees now know what to expect.

Session Stand-Outs

CppCon kicked-off with a keynote from Bjarne Stroustrup regarding the state of C++, how it got to be where it is today, and where it’s going. During this talk, Bjarne became uncharacteristically aggressive towards the C++ standards committee concerning C++17. It was clear that he was dissatisfied with the lack of progress made towards standardization of such features as concepts, modules, co-routines, etc. And it appears to me that the community as a whole sides with Bjarne on this matter. I counted at least ten, perhaps more, talks on features that are either in technical specification or in proposal status.

These were the sessions that really stood out for me this year.

Howard Hinnant’s “A <chrono> Tutorial” was a superb introduction to std::chrono. I have to confess that I’ve struggled to use this library effectively. It always frustrated me at how much code and how many data structures are involved in order to do the most basic of time-based calculations. Howard’s presentation changed my view of std::chrono entirely and made me realize that more often than not, I was just using std::chrono all wrong. 🙂

An interesting tidbit that came out of this discussion is that std::chrono’s epoch isn’t specified by the standard. I had (wrongly) assumed that it was always Jan 1 1970, just like that used by the time() function. Stephan Lavavej, who happened to be in the audience, spoke up and stated that the Microsoft VC++ implementation of std::chrono is changing its epoch to January 1, 1601, which is consistent with the Windows NT epoch. (Edit 2016/10/10: As Howard Hinnant notes in a comment below, this decision has been reversed by the VC++ team. Howard is currently working on a proposal to standardize the system_clock epoch to Jan. 1 1970.)

Timur Doumler’s “Want Fast C++? Know Your Hardware!”, was also an eye-opening talk. It was all about code organization and how it can affect performance, especially as it relates to the cache. After this talk, I’ll never look at reinterpret_cast the same. Timur is an audio guy. And audio is an area where performance really matters. Every talk I’ve seen of his has been wonderful. If you haven’t seen his CppCon 2015, JUCE Summit 2015, or BoostCon talks, I strongly recommend checking them out (in that order).

Jason Turner gave two solo talks at CppCon this year that were fantastic. “Rich Code for Tiny Machines: A Simple Commodore 64 Game in C++ 17” and “Practical Performance Practices” were both about performance. Throughout both of his presentations, he used the Godbolt Compiler Explorer to demonstrate the impact simple language features can have upon the resulting assembly code. Everything he discussed revolved around the premise that less instructions = faster code, so you didn’t really need to understand assembly to appreciate his talk.

Other sessions that I enjoyed were Tim Shen’s “Regular Expressions in C++, Present and Future”, Steve Carroll and Daniel Moth’s “Latest and Greatest from the Visual Studio Family for C++ Developers”, James McNellis’ “An Introduction to C++ Coroutines”, Dan Saks’ “extern “C”: Talking to C Programmers about C++”, Rob Irving and Jason Turner’s “What We’ve Learned from the C++ Community”, David Sankel’s “Building Software Capital”, Miodrag Milanovic’s “The MAME story: From C to Modern C++”, and Any Bondy’s “AAAARGH!? Adopting Almost Always Auto Reinforces Good Habits!?”

Conclusion

I’m sure there were many other stand-out sessions from other presenters. With so much going on at the same time, it’s impossible to not miss out on something fantastic. Once the videos are posted on YouTube, I’m sure I’ll stumble across more than one thing that I’ll kick myself for missing.

There were some faces that were notably absent this year, such as Andrei Alexandrescu, Scott Meyers (retired from the C++ scene, but I still hope to see pop up from time to time), and Peter Sommerlad. There were also a few people who I enjoy listening to that were present, but didn’t speak (Titus Winters, I’m talking about you).

All in all, 2016 was another great year for CppCon. If you’re a C++ programmer and have the opportunity to attend this conference, you should definitely go.