How to Check Access Rights

Given that everyone is always pushing for better security mechanisms, I’m always surprised at how incredibly difficult the simple tasks can be in the Win32 security model. At work, we have an application that wants to do the right thing — it wants to show a UAC shield badge on a menu item, because that menu may require elevated privileges in order to function properly. However, it may not require elevation either — the menu is used to copy a file to a particular pre-defined directory. So we want to show the UAC badge only if necessary. So how do you do that?
Continue reading

Posted in Win32 | Tagged , | 17 Comments

The Placement New Operator

I’d like to shed a little bit of light on a dusty corner of the C++ language: there’s more than one “new” operator! Well, since you’ve likely encountered the vector new (new[]) operator, I should say there’s more than two “new” operators! I want to cover the concept of a “placement new” operator.
Continue reading

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

The comma

As C and C++ programmers, we’ve probably seen and used the comma countless times in our applications, without thinking too much about it. However, there are some very interesting points to this piece of punctuation that are worth discussing. The comma is used as part of the syntax for a list of like objects (such as when declaring variables), or as part of the syntax for a list of unlike objects (such as a parameter list). It is even an operator that can be used as part of an expression!
Continue reading

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

The Joys of Bit Fields

In C and C++, bit fields are one of the more odd declaration types that you run into rarely. The basic idea behind them is to provide the programmer with a way to define declarations at the bit-level. For instance, let’s say you have 8 boolean flags you want the programmer to be able to specify. You could use 8 bools to do this, however that could mean those flags take up anywhere from 8 to 32 bytes of memory! (Remember that in C++, the size of a bool datatype is implementation defined.) But you really only need one bit of information to track each individual flag. In this case, you could use a single byte to encode all 8 flags by using a bit field.
Continue reading

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

Discriminated Unions

In computer science, a discriminated union is one of the many names given to the concept of a “catch-all” datatype. (You’ll also hear it referred to as a variant.) It’s meant to hold data of any type at any given point in time. It does so by “tagging” the type information within the union. Generally speaking, it’s also an efficient datatype because the underlying storage can be shared amongst all tags. Since you’re only allowed to use one tag at a time, this sharing of memory can greatly reduce the overhead for some applications.
Continue reading

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

Opaque Data Pointers

Most of the frameworks that I work on need to be usable from multiple programming languages (typically, C++, C# and Objective-C, but sometimes more). This means I must target the lowest common denominator in terms of the function prototypes, so I write a lot of C header files. But I’m an object-oriented junky at heart, and the languages which will eventually consume my frameworks all support object-oriented paradigms, so many of my C APIs end up being a flattened “classes.” This shouldn’t be too surprising as it’s a common technique for C frameworks.

What this means from a practical standpoint is that there is usually one or two APIs responsible for creating an “object” and this object gets passed into several other APIs as sort of a manual “this” pointer. What I want to talk about today are possible ways to design that object reference, and the pros and cons associated with each.
Continue reading

Posted in Framework Design | Tagged , | 8 Comments

Inline Namespaces

One of the neat, new language features of C++0x that is targeted firmly at framework designers is the ability to declare “inline” namespaces. While the name may seem a bit strange at first, the concept is quite intuitive. It allows a framework designer to version the APIs within their namespace.
Continue reading

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

Returning Stack-Based Values

QString GetSomeString( void )
{
	return QString( "foobar" );
}

The code looks innocuous enough, but something as simple as this can be the source of hard to track down bugs. I want to talk a bit about the dangers of returning stack-based values.
Continue reading

Posted in Framework Design | Tagged , | 4 Comments

Exceptions in Frameworks

Exceptions are a topic near and dear to my heart, mostly because I have some strong opinions about the benefits and disadvantages of exceptions. But this isn’t a blog posting about whether exceptions are good or not. Instead, this is a post about using exceptions when designing a C or C++ framework.
Continue reading

Posted in Framework Design | Tagged | Leave a comment

Warning: Don’t Ignore Warnings!

How many times have you run across code that looks fine, works fine, but still generates a message like “Signed/unsigned mismatch” when we compile it? How many times have you thought to yourself, “that’s stupid, I know this code is fine.” and continued working? I think if you were to poll 100 programmers (and they were honest), all 100 would say they’ve done that at least once in their life.
Continue reading

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