Embedded C++ Programming Guide

Migrating from C to C++

  1. The size of a character constant has gone from sizeof(int) to sizeof(char).
  2. An object declared at file scope should be initialized. All references to that global object in other files must use the extern keyword and must not be initialized.
  3. const-qualified objects with external linkage must have an explicit extern specifier.
  4. Use an explicit cast when converting a void* to another pointer type.
  5. Provide typesafe implementations for operator++ and operator-- as needed for enumeration types.
  6. Do not define types in a cast expression, parameter declaration or sizeof expression.
  7. Do not use goto or switch statements to bypass initialization of a local object.
  8. Do not initialize an array of characters using a string literal with more characters (including the null terminator) than the size of array.
  9. Always declare the prototype before calling a function. Use f(void) to emphasize that function "f" has no arguments.
  10. Do not use a C++ keyword as an identifier.
  11. Do not use a type outside of the scope in which it was defined.
  12. In C++, use only new and delete.
  13. Avoid writing a C-style comment "/* */" immediately after the token '/'.

Guidelines for Code Size

  1. Use declarations of the form "T x(i);" rather than "T x; x=i;".
  2. Inline small functions only.
  3. Use compound assignment operators, such as "+=", in preference to '+' and '=', to avoid creating and destroying temporary objects unnecessarily.
  4. Implement class-specific operators new and delete as needed to improve the speed and memory utilization of dynamic memory management.
  5. Avoid coding which depends on the initialization order of global objects across translation units.

Guidelines for Speed

  1. Avoid creating and destroying large arrays of objects during time-critical processing.
  2. Avoid declaring objects inside a loop. Declare them before the loop instead.

Guidelines for ROMable code

  1. Declare objects to be placed in ROM with: static, constant initializer, and POD (plain ol' data) types. A POD type is any of: