Gamasutra is part of the Informa Tech Division of Informa PLC

This site is operated by a business or businesses owned by Informa PLC and all copyright resides with them. Informa PLC's registered office is 5 Howick Place, London SW1P 1WG. Registered in England and Wales. Number 8860726.


Gamasutra: The Art & Business of Making Gamesspacer
arrowPress Releases








If you enjoy reading this site, you might also want to check out these UBM Tech sites:


 

In C++, Everything is Harder Than You Think

by Neil Gower on 08/14/09 04:05:00 pm   Expert Blogs   Featured Blogs

The following blog post, unless otherwise noted, was written by a member of Gamasutra’s community.
The thoughts and opinions expressed are those of the writer and not Gamasutra or its parent company.

 

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++ standardsWhy 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.


Related Jobs

Dream Harvest
Dream Harvest — Brighton, England, United Kingdom
[05.18.19]

Technical Game Designer
Pixar Animation Studios
Pixar Animation Studios — Emeryville, California, United States
[05.17.19]

Animation Tools Software Engineer
Disbelief
Disbelief — Chicago, Illinois, United States
[05.17.19]

Senior Programmer, Chicago
Disbelief
Disbelief — Chicago, Illinois, United States
[05.17.19]

Junior Programmer, Chicago





Loading Comments

loader image