Cleaning up C code for C++
The next step in migration to C++ is to review and modify existing C
code so that it is truly C++ compatible and can be processed by a C++
compiler without any problems. The resulting code may be called Clean
C. Here are the key issues to consider:
1) Function
prototypes " These are mandatory in C++.
2) Type casts
" Implicit casting is much stricter in C++; use explicit cast operators
like (char).
3) Enumerated
types " These are not taken very "seriously" by C compilers,
being considered a means of making typed, symbolic constants; in C++
enum declarations result in true data types.
4) String array
size " In C and C++ a char array is commonly used to contain a
NULL terminated character string. This requires the array to have
sufficient capacity for the characters and the terminator.
In C, it is not permitted to initialize an array with a string
larger than the declared array size, but the NULL terminator may be
discarded. This leniency is not extended in C++. A simple strategy is
to leave the array size unspecified and allow the compiler to allocate
the space, thus:
char str[] = "xyz";
5) Nested
structures " In C, if a struct is defined within another
struct, the inner struct may also be used declared by itself elsewhere
in the module. In C++, the scope of the inner struct is strictly
confined to the outer one. Hence, the following code would not be Clean
C:
struct out
{
struct in { int i; } m;
int j;
};
struct in inner;
struct out outer;
6) Multiple
declarations " In C, it is quite legal to declare a variable in
a module, outside of a function and then declare it again later in the
module. This only causes problems if the redeclaration is inconsistent.
In C++, multiple declarations are illegal.
7) Additional
keywords " C++ introduces a number of additional keywords on
top of those already reserved in C. This may be a problem if there is a
clash with identifiers in the C code. The best strategy is to introduce
a naming convention and apply it to the code. For example, all
functions, types and variables have an upper case initial letter and
all constants are fully capitalized.
C+: A better C?
The last step in the migration process, prior to considering object
oriented techniques, is to incrementally apply C++ language features.
This results in a hybrid language " a "better C" " that we might call
C+. Possibilities include, but are not confined to, the following C++
features:
1) New comment
notation " C++ introduces a new, end of line comment notation
using the // operator. This is neater and addresses issues with nesting
when using the traditional " /* ... */ " notation (which is still
available, of course).
2) Reference
parameters " In C++ by default, parameters are mostly passed by
value (copy) the same as in C. However, there is also the option to
pass by reference to avoid complications of explicit pointer usage.
3) Variable
declaration placement " In C, automatic variables must be
declared at the head of a block (commonly they are just placed at the
head of a function). This limitation is relaxed in C++, enabling
variables to be declared close to their point of first use. For
example:
for (int x=0; x<3; x++)
...
4) Constructors
in structures " In C++, a class definition may include
constructor and destructor member functions, which are automatically
called when an object comes into and goes out of scope, respectively.
Since a struct is, in most respects, identical to a class, it too may
include a constructor. This is useful to facilitate the automatic
initialization of the struct variable.
5) Memory leaks
" By use of paired constructors and destructors, memory leaks
may be avoided by ensuring that allocated resources (like dynamic
memory) are deallocated when no longer required.
6) Exception
handling " The EHS, introduced earlier in this paper, is not
strictly an object oriented language feature and, hence, its use may
logically be considered when using C+.
Next in Part 2: "Encpsulation of
expertise using C++ objects."
Colin Walls has over twenty-five years experience in the
electronics industry, largely involved with embedded software. A
frequent presenter at conferences and seminars including the Embedded
Systems Conference he is a member of the marketing team of the Embedded
Systems Division of Mentor Graphics.
Click here to see Part 2.