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.

The hokey-cokey* of function calls

Functions are the lifeblood of a C program. The program flow is altered by passing parameters to functions, which are then manipulated. Conceptually function parameters are defined as being either:

Inputs (Read-only) – client-supplied objects manipulated within the function only
Outputs (Write-only) – objects generated by the function for use by the client.
Input-Outputs (Read-Write) – client objects that can be manipulated by the function.

Defining the use of a parameter gives vital information not only to the implementer, but (perhaps more importantly) to […]

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

Shock horror! I learned something about arrays in C

Every so often you pick up a snippet of information that completely changes the way you view things. This week, it’s the use of arrays as function parameters.

At first glance the code horrified me (as I’m sure it will horrify some of you out there!) but as I’ve played with it I can see real merit in the technique.

Arrays, pointers and syntactic sugar

In C there is a close (if somewhat messy!) relationship between arrays and pointers. As far as the […]

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

Casting – what could possibly go wrong?

Type casting in C++ is a form of what is known in computer science as type punning – that is, circumventing the type system of a programming language.

C++ inherits its conversion and casting mechanism from C, but supplements it (although sensibly we should say, replaces it) with four, more explicit cast operations:

static_cast
reinterpret_cast
const_cast
dynamic_cast

In C and C++ – and particularly in embedded systems – casting is a necessary evil; so much so that many programmers just accept it […]

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

The Rule of the Big Five

The dynamic creation and destruction of objects was always one of the bugbears of C. It requires the programmer to manually control the allocation, initialisation and deallocation of memory for the object. 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 unsafe, memory-leaking language.

C++ improved matters significantly with an idiom known as RAII/RRID; more generically referred to as resource […]

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

Namespaces

In this article we look at one of the issues inherent in C when building larger projects – the problem of function and object naming. We look at the C++ solution to this: namespaces.

A problem with big projects in C

When we move to multi-file projects the problem in C is having to create unique names for functions and externs in the global namespace. If we don’t have unique definitions this will lead to a link-time error.

We could, of course, make […]

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

Debunking priority

Before I start, a disclaimer:

For the purposes of this article I’m limiting the discussion to even-driven systems on priority-based, pre-emptive operating systems, on single processors.

I’m using the word task to mean ‘unit of execution’ or ‘unit of schedulability’, in preference to thread or process. I’m ignoring the different OS memory models.

There seems to be a fair amount of misunderstanding about the concept of priority in concurrent programming:

Priority means one task is more ’important’ than another.
Priority allows one task to […]

Posted in Design Issues, RTOS | Tagged , , , , , | Leave a comment

L-values, r-values, expressions and types

Simple question: Why does this code compile?

…and this code doesn’t?

The compiler gives the following:

L-values

What is this ‘l-value’ thing? When (most of us) were taught C we were told an l-value is a value that can be placed on the left-hand-side of an assignment expression. However, that doesn’t give much of a clue as to what might constitute an l-value; so most of the time we resort to guessing and trial-and-error.

Basically, an l-value is a named object; which may be modifiable […]

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

Sailing the Seven C’s of design*

I’m always looking for nice little mnemonics to help out remember the important concepts in design.  Here’s one for model-driven development I call the “Seven C’s”.  It basically enumerates the seven stages a design goes through, from initial idea to code.

CONCEPT The Concept phase is about understanding the problem.  In other words: requirements analysis.  When you’re in Concept mode your main focus is on validation – am I solving the right problem for my […]

Posted in Design Issues | Tagged , , , , , , | 2 Comments

Default construction and initialisation in C++11

Default constructors

In C++ if you don’t provide a constructor for the class the compiler provides one for you (that does nothing):

This, of course, isn’t very useful so typically we write our own constructors to initialise the attributes of our class.  However, as soon as you write a (non-default) constructor the compiler stops providing the default constructor:

The normal solution is to write our own default constructor.  In C++11 there is syntax to allow you to explicitly create the compiler-supplied constructor without […]

Posted in General | 2 Comments

The changing face of programming abstraction in C++

Iterating through containers… options, options options.

Iterating through a container of objects is something we do a lot.  It’s boilerplate code.  It’s also a nice indicator of how C++ is raising the level of abstraction in programming.

So let’s start with a simple container of ADTs:

Now let’s iterate through them, calling the mf() method on each object in turn.  First, we’ll ‘roll our own’ loop.

That’s imperative programming in action; and has the warm, fuzzy feeling of recognition for all C programmers.

Modern C++, […]

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