Switching on Expressions

The switch statement is a powerful construct in C that allows you to clearly express complex if/else if/else blocks as a single statement. By providing this construct, you can write code that’s easier to understand and maintain. The downside to this is that you must use constant values for the case statements, and these constants must sensibly compare using the == operator. So, for instance, it is possible to have integers in your case statements, but not possible to have strings. Attempting to compare strings with == will not work reliably due to pointer comparisons. However, there is a solution that has the power and clarity of a switch statement in C++, and thanks to lambda functions in C++11, it can be elegant as well.
Continue reading

Posted in C/C++ | Tagged , , | 1 Comment

The Plan and Random Points

For about six months, I managed to stick to a bi-weekly update schedule of Mondays and Fridays. However, I’ve exhausted my entire backlog of topics, as well as my todo list for things to write about. Instead of trying to force myself to write about something random purely due to an arbitrary schedule, I’m scaling back. Now I’m going to post whenever I have something of value to contribute to the world.
Continue reading

Posted in Uncategorized | Tagged , , | 1 Comment

Virtual Inheritance

A question came up on LinkedIn in the C++ group relating to how virtual class inheritance actually works. Since LinkedIn limits the amount of space for responses, and also manages to screw up code formatting, I decided to tackle the answer here. Before continuing, I suggest you familiarize yourself with the thread in question.
Continue reading

Posted in C/C++ | Tagged , , | 4 Comments

Virtual Methods and Multiple Inheritance

Previously, we covered the basics behind virtual methods. If you aren’t wholly comfortable with the subject yet, I’d recommend you go check that post out first. But if you are comfortable, we’re going to delve into the wacky fun world of multiple inheritance, and see how that affects virtual function calls.
Continue reading

Posted in C/C++ | Tagged , | 5 Comments

How DLL Imports Work

When you make a function call into a function that exists in a DLL, what happens, exactly? How does the function call happen, and what work goes on behind the scenes to make it so? I want to cover some of this information so you can have a more clear picture of the way shared libraries work on Windows. I am not going to cover how exporting a function works, but instead focus on how importing a function works as that’s a bit more involved.
Continue reading

Posted in Win32 | Tagged , , , | 1 Comment

Virtual Methods

Virtual functions are a fairly well-understood programming construct in terms of how and when to use them. But have you ever stopped to think about how they actually work under the hood? You’ve probably heard the term “vtable” thrown around at some point, but do you have a firm understanding of what one is? I’d like to cover a bit of the gritty details so you can leave with a bit more arcane knowledge than you arrived with.
Continue reading

Posted in C/C++ | Tagged , | 7 Comments

Static asserts

One of the new features of C++11 is the ability to do a compile-time assertions. These assert functions are similar in concept to the runtime assert functions we all know and love. You pass in a constant expression and a string; if the expression does not evaluate to true, then the compiler generates an error message from the string. For instance:

// Make sure we only compile this code for 64-bit platforms
static_assert( sizeof( long ) == 8, "This code requires a 64-bit compiler" );

Continue reading

Posted in C/C++ | Tagged , | 1 Comment

Threading on Windows

Threads are becoming one of the more ubiquitous concepts in programming. Chances are quite good that you’ve a few of them before. But have you ever stopped to think about how a thread works under the hood? There are some obvious things, like allocating a stack for the thread, updating some process structures to track the threads, and so forth. But what about the fuzzy stuff that happens between the kernel bookkeeping of making a thread object and your thread entrypoint code?
Continue reading

Posted in Win32 | Tagged , , | Leave a comment

Understanding Undefined Behavior

One of the harder concepts for people to understand in C++, in my opinion, is “behavior.” In C++, the language has some very specific wording for what the various behaviors are, and I’ve seen a lot of people get them mixed up or misunderstand the meaning when brought up.
Continue reading

Posted in C/C++ | Tagged , | Leave a comment

Be Carefully Consistent With Memory

One of the things that most C/C++ programmers start to take for granted is memory. It’s always there, and when used properly, it always “just works.” However, frameworks throw a bit of a monkey wrench into the equation because they are their own, separate entity. As framework designers, we have to be careful with the way we expose memory to the consumer lest we shatter that illusion that memory “just works.”
Continue reading

Posted in Framework Design | Tagged , , | 1 Comment