Author Archives: Glennan Carnie

About Glennan Carnie

Glennan is an embedded systems and software engineer with over 20 years experience, mostly in high-integrity systems for the defence and aerospace industry. He specialises in C++, UML, software modelling, Systems Engineering and process development.

Getting your head around auto’s type-deduction rules

Automatic type-deduction is perhaps one of the more divisive features of Modern C++.  At its core it’s a straightforward concept:  let the compiler deduce the type of an object from its initialiser.   Used in the right way this can improve the readability and maintainability of your code.

However, because auto is based on template type-deduction rules there are some subtleties that can catch the unwary programmer.

In this article we’ll have a look at auto in the context of the template type-deduction […]

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

Great Expectations

Previously, we’ve looked at the basic concepts of function parameter passing, and we’ve looked at the mechanics of how parameters are passed at the Application Binary Interface (ABI) level.

Far too often we focus on the mechanisms and efficiency of parameter passing, with the goal: if it’s efficient then it’s good; that’s all there is to it.  In this article I want to move past simple mechanics and start to explore function parameter design intent – that is, what can I […]

Posted in C/C++ Programming, Design Issues | Tagged , , , , , , , , | 2 Comments

The three ‘No’s of sequential consistency

In the previous article we looked at the memory consistency problem that occurs when writing multi-threaded code for modern multi-processor systems.

In this article is we’ll have a look at how we can solve the sequential consistency problem and restore some sanity to our programming

Posted in ARM, C/C++ Programming, Cortex | Tagged , , , , , , | Leave a comment

Memory consistency made simple(ish)

The C++11 memory consistency model is probably one of the most significant aspects of Modern C++; and yet probably one of the least well-understood.  I think the reason is simple:  it’s really difficult to understand what the problem actually is.

The memory consistency problem is a concurrency problem.  That is, it’s a problem that occurs when we start writing multi-threaded code.  More specifically, it’s a parallelism problem – the real subtleties occur when you have two or more processors executing code.

In the first […]

Posted in C/C++ Programming, Design Issues, General | Tagged , , | 1 Comment

Seeing stars. And dots. And arrows.

This time I want to look at a seemingly trivial concept in C++ programming: accessing class members, either directly or via a pointer.  More than anything it’s an excuse to talk about two of C++’s more obscure operators – .* and

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

Becoming a Rule of Zero Hero

“Do, or do not; there is no ‘try’.”

Previously, we’ve looked at The Rule of Zero which, in essence, says: avoid doing your own resource management; use a pre-defined resource-managing type instead.

This is an excellent guideline and can significantly improve the quality of your application code. However, there are some circumstances where you might not get exactly what you were expecting. It’s not that the code will fail; it just might not be as efficient as you thought.

Luckily, the solution is […]

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

Bitesize Modern C++ : Smart pointers

The dynamic creation and destruction of objects was always one of the bugbears of C. It required the programmer to (manually) control the allocation of memory for the object, handle the object’s initialisation then ensure that the object was safely cleaned-up after use and its memory returned to the heap. Because many C programmers weren’t educated in the potential problems (or were just plain lazy or delinquent in their programming) C got a reputation in some quarters for being an […]

Posted in C/C++ Programming | Tagged , , , , , , , , , , | 3 Comments

Bitesize Modern C++ : std::array

C++98 inherited C’s only built-in container, the array. Arrays of non-class types behave in exactly the same way as they do in C. For class types, when an array is constructed the default constructor is called on each element in the array

Explicitly initialising objects in an array is one of the few times you can explicitly invoke a class’s constructor.

For track[], the non-default constructor is called for first three elements, followed by default (no parameter) constructor for the last two […]

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

Bitesize Modern C++ : noexcept

We have some basic problems when trying to define error management in C:

There is no “standard” way of reporting errors. Each company / project / programmer has a different approach
Given the basic approaches, you cannot guarantee the error will be acted upon.
There are difficulties with error propagation; particularly with nested calls.

The C++ exception mechanism gives us a facility to deal with run-time errors or fault conditions that make further execution of a program meaningless.

In C++98 it is possible to specify […]

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

Bitesize Modern C++ : Override and Final

Override specifier

In C++98 using polymorphic types can sometimes lead to head-scratching results:

On the face of it this code looks sound; indeed it will compile with no errors or warnings. However, when it runs the Base version of op() will be executed!

The reason? Derived’s version of op() is not actually an override of Base::op since int and long are considered different types (it’s actually a conversion between an int and a long, not a promotion)

The compiler is more than happy to […]

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