Sponsored By

Featured Blog | This community-written post highlights the best of what the game industry has to offer. Read more like it on the Game Developer Blogs.

In C++, Everything is Harder Than You Think

C++ is a great foundation for building games, but you need more than just the core language to get anywhere fast and reliably. If you're not using C++ with STL and Boost, it might be time to upgrade your toolbox.

Neil Gower, Blogger

August 14, 2009

3 Min Read

I've recently been doing some low level C++ work, the kind of stuff where you have the Standard document open on one screen, and your code in the other. What I found interesting was how complicated relatively simple things become when you're aiming for standards conforming correctness.

To take a basic example, dynamically allocated objects and pointers are simple in principle - allocate with new, remember to deallocate with delete. Oh, except for arrays, if you use new[] then you have to use delete[]. And don't mix malloc and free with new and delete, although they're all available to you. C++ standards

C++ standards

Why yes you can dereference a dangling pointer, and it might even appear to work for a while.

Uninitialized pointers? No problem, you're the boss. Want to allocate some raw memory and then use it to for objects? You can do that, but if you get the alignment wrong, it might blow up in your face at runtime.

How do you check alignment anyway? Shh.. that's a secret! (Actually, it's an implementation detail of the compiler, which is almost the same thing)

Many of these memory management problems can be solved by smart pointers. But when I read Meyers' comment in Effective STL that his smart pointer class was buggy, I started thinking I might become a full time Python programmer.

The reason for most of the complexity in C++ is that the standard makes very few assumptions about the target system and compilers, and also tries to avoid imposing any extraneous overhead on the program. The STL introduces a good set of higher level functionality, but brings with it more complexity to master.

There are a lot of subtleties in C++ that most of us never give a moment's thought to until something goes wrong. The (More) Effective C++/STL books exist almost entirely to bridge the gap between our everyday understanding of C++ and the details of how it really works.

We don't have to abandon ship and start converting everything to C# and Python just yet though. I was motivated to write this post because I noticed that every time I thought to myself, there must be an easier way to do this, there was - Boost.

I had always thought of Boost for its add-on features like regular expressions and fancy math. However, Boost also takes many of the things in C++ that are good in principle (low level memory management, smart pointers, iterators...) and makes them good in practice.

The Boost community is full of hardcore C++ experts that have worked out solutions to handle even the most obscure scenarios that C++ presents, scenarios which most of us will never have the time or inclination to investigate. Boost is nice and modular too, so you can generally pick and choose the bits you want to use.

If you find yourself frustrated writing endless lines of boilerplate code for cases you don't even know will ever happen in your code, have a look through Boost and see if someone has done the work for you. You could be pleasantly surprised. Working in C++ can be great, you just have to bring a full toolbox to the job.

Read more about:

Featured Blogs

About the Author(s)

Daily news, dev blogs, and stories from Game Developer straight to your inbox

You May Also Like