Improve C++ performance in debug mode with inline expansion

When you’re dealing with an intensive real-time application, such as a game or simulation, a common problem is that debug builds in C++ can run much slower than release builds. This difference in run-time behaviour means it can be hard to reproduce and analyse bugs and other problems. There are several things you can do to improve it, and one which helped me recently was enabling inline expansion (or simply “inlining”).

A quick warning though: it won’t improve performance in all situations, and it can actually hinder debugging. For performance-critical code, you should first try manually optimising your algorithms.

Read more Improve C++ performance in debug mode with inline expansion

Introduction to Win32 Named Pipes (C++)

See also: Win32 named pipe example programs on GitHub.

There are times when it’s extremely useful to be able to pass some data between different programs running on the same system. For example, you might have multiple programs forming part of the same package, and they need to share some important information or work together to process something.

There are several ways to do it, but my choice in a recent task was to use the named pipes functionality of the Win32 API (working in C++). Note that pipes on other operating systems are a little different, so not all of this information is portable. Read more Introduction to Win32 Named Pipes (C++)

The joy of C++ streams

Many programmers complain about how poorly C++ handles strings. The unfortunate reality is that a language with such direct memory access as C++ cannot handle strings natively, because stack memory usage absolutely must be fixed at compile-time. Or, in simpler terms, you can’t expect to fit an unpredictably large peg into a small hole. The std::string class improves things considerably — it is a very robust and efficient string class programmed by people who are cleverer than most of us can ever hope to be.

It still doesn’t solve all our problems though, because it doesn’t do string concatenation very well, and you can’t easily convert other types (such as numbers) to a std::string. Thankfully, streams come to our rescue! They provide a beautifully simple and straightforward means of building up strings of text and sending them pretty much wherever we need them. Read more The joy of C++ streams

Fixing LNK4098 in Visual C++

Like many C++ linker errors and warnings, LNK4098 is a little vague and cryptic at first. It comes in different flavours, but it will often have other errors with it, such as LNK2005 (“symbol already defined”). Here’s one I got recently in VC++ 2010 Express:

LINK : warning LNK4098: defaultlib ‘LIBCMTD’ conflicts with use of other libs; use /NODEFAULTLIB:library

You might find “LIBCMTD” replaced with other things, such as “LIBC” or “LIBCD”. I recommend taking a little time to understand what’s going on, as knowledge really is power for programmers, and it will hopefully help you avoid similar problems in the future.
Read more Fixing LNK4098 in Visual C++

Dynamic types in C++: factories, templates, or lots of switches?

While doing some C++ programming today, I was faced with a design decision. My code had to be able to create and delete objects of various types, dynamically and on-demand at run-time. Easy if you’re working with a loosely-typed language such as PHP, but it’s a different story when you’re working with C++. The actual range of classes which could be created is fixed at compile-time, so there are still lots of possibilities, ranging from the very simplistic multi-way decisions, through to some downright groovy template shenanigans. Yes. I just said groovy. That’s how brilliant a good bit of template programming can be!
Read more Dynamic types in C++: factories, templates, or lots of switches?

Using GDI+ in C++ Win32 programming

If you do any Win32 programming in C++, then I strongly recommend you learn about GDI+. It’s been around for a while now, but not everybody knows about it. It can be great to have on hand even just to illustrate tests and prototypes.

Why is it so good? It provides an object-oriented way to draw graphics using the GDI, which is much nicer and easier to user than the basic C-style GDI functions and resources that used to be the norm. It also provides a lot of additional functionality which otherwise was not possible (or at least not easy) with the regular GDI functions alone, such as proper alpha blending, matrix transformations, file input/output, and loads more. It’s quite easy to setup too, as I will explain… Read more Using GDI+ in C++ Win32 programming