Eff CPP Emb Notes Sample
Eff CPP Emb Notes Sample
Presentation Materials
Effective C++
in an
Embedded Environment
Sample
artima Click here to purchase the complete PDF document!
Effective C++ in an Embedded Environment Sample
Thank you for downloading this sample from the presentation materials for Scott
Meyers’ Effective C++ in an Embedded Environment training course. If you’d like to
purchase the complete copy of these notes, please visit:
http://www.artima.com/shop/effective_cpp_in_an_embedded_environment
All information and materials in this document are provided “as is” and without war-
ranty of any kind.
The term “Artima” and the Artima logo are trademarks or registered trademarks of
Artima, Inc. All other company and/or product names may be trademarks or regis-
tered trademarks of their owners.
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Last Revised: 10/4/12
These are the official notes for Scott Meyers’ training course, “Effective C++ in an
Embedded Environment”. The course description is at http://www.aristeia.com/c++-in-
embedded.html. Licensing information is at http://aristeia.com/Licensing/licensing.html.
Please send bug reports and improvement suggestions to [email protected].
In these notes, references to numbered documents preceded by N (e.g., N3092) are
references to C++ standardization document. All such documents are available via
http://www.open-std.org/jtc1/sc22/wg21/docs/papers/.
[Comments in braces, such as this, are aimed at instructors presenting the course. All
other comments should be helpful for both instructors and people reading the notes on
their own.]
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Click here to purchase the complete PDF document!
Effective C++ in an Embedded Environment
Important!
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Slide 2
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Click here to purchase the complete PDF document!
Effective C++ in an Embedded Environment
Overview
Day 1 (Approximate):
“C++” and “Embedded Systems”
A Deeper Look at C++
Implementing language features
Understanding inlining
Avoiding code bloat
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Slide 3
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Click here to purchase the complete PDF document!
Effective C++ in an Embedded Environment
Overview
Day 2 (Approximate):
Modeling Memory-Mapped IO
Implementing Callbacks from C APIs
Interesting Template Applications:
Type-safe void*-based containers
Compile-time dimensional unit analysis
Specifying FSMs
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Slide 4
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Click here to purchase the complete PDF document!
Effective C++ in an Embedded Environment
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Slide 5
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Click here to purchase the complete PDF document!
Effective C++ in an Embedded Environment
Overview
Day 1 (Approximate):
“C++” and “Embedded Systems”
A Deeper Look at C++
Implementing language features
Understanding inlining
Avoiding code bloat
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Slide 6
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Click here to purchase the complete PDF document!
Effective C++ in an Embedded Environment
“C++”
Timeline and terminology:
1998: C++98: “Old” standard C++.
2003: C++03: Bugfix revision for C++98.
2005: TR1: Proposed extensions to standard C++ library.
Common for most parts to ship with current compilers.
Overview comes later in course.
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Slide 7
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Click here to purchase the complete PDF document!
Effective C++ in an Embedded Environment
“Embedded Systems”
Embedded systems using C++ are diverse:
Real-time? Maybe.
Safety-critical? Maybe.
Challenging memory limitations? Maybe.
Challenging CPU limitations? Maybe.
No heap? Maybe.
No OS? Maybe.
Multiple threads or tasks? Maybe.
“Old” or “weak” compilers, etc? Maybe.
No hard drive? Often.
Difficult to field-upgrade? Typically.
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Slide 8
[The goal of this slide is to get people to recognize that their view about what it means to
develop for embedded systems may not be the same as others’ views. The first time I
taught this class, I had one person writing code for a 4-bit microprocessor used in a digital
camera (i.e., a mass-market consumer device), and I also had a team writing real-time
radar analysis software to be used in military fighter planes. The latter would have a very
limited production run, and if the developers needed more CPU or memory, they simply
added a new board to the system. Both applications were “embedded,” but they had
almost nothing in common.]
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Click here to purchase the complete PDF document!
Effective C++ in an Embedded Environment
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Slide 9
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Click here to purchase the complete PDF document!
Effective C++ in an Embedded Environment
Overview
Day 1 (Approximate):
“C++” and “Embedded Systems”
A Deeper Look at C++
Implementing language features
Understanding inlining
Avoiding code bloat
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Slide 10
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Click here to purchase the complete PDF document!
Effective C++ in an Embedded Environment
Implementing C++
Why Do You Care?
You’re just curious: how do they do that?
You’re trying to figure out what’s going on while debugging.
You’re concerned: do they do that efficiently enough?
That’s the focus of this presentation
Baseline: C size/speed
Have faith:
C++ was designed to be competitive in performance with C.
Generally speaking, you don't pay for what you don't use.
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Slide 11
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Click here to purchase the complete PDF document!
Effective C++ in an Embedded Environment
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Slide 12
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Click here to purchase the complete PDF document!
Effective C++ in an Embedded Environment
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Slide 13
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Click here to purchase the complete PDF document!
Effective C++ in an Embedded Environment
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Slide 14
According to the “Pure Virtual Function Called” article by Paul Chisholm (see the
“Further Information” slides at the end of the notes), the Digital Mars compiler does not
always issue a message when a pure virtual function is called, it just halts execution of the
program.
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Click here to purchase the complete PDF document!
Effective C++ in an Embedded Environment
For the first example, gcc 4.4-4.7 issue warnings. VC9-11 do not.
For the second example, none of the compilers issues a warning.
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Click here to purchase the complete PDF document!
Effective C++ in an Embedded Environment
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Slide 16
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Click here to purchase the complete PDF document!
Effective C++ in an Embedded Environment
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Slide 17
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Click here to purchase the complete PDF document!
Effective C++ in an Embedded Environment
Object’s vptr
Data members
for
the object
This pointer is called the vptr (“virtual table pointer”).
Its location within an object varies from compiler to compiler
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Slide 18
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Click here to purchase the complete PDF document!
Effective C++ in an Embedded Environment
D1 Object D1’s
vptr D1 Object vtbl
Data vptr Implementations
Members of D1’s virtual
Data
Members functions
D1 Object
vptr
Data
Members D2 Object
vptr D2’s
vtbl
Data Implementations
Members of D2’s virtual
D2 Object
D2 Object functions
vptr
vptr
Data
Members Data
Members
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Slide 19
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Click here to purchase the complete PDF document!
Effective C++ in an Embedded Environment
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Slide 20
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Click here to purchase the complete PDF document!
Effective C++ in an Embedded Environment
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Slide 21
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Click here to purchase the complete PDF document!
Effective C++ in an Embedded Environment
The diagram shows that if the first data member declared in a class has a type that requires
double-word alignment (e.g., double or long double), a word of padding may need to be
inserted after the vptr is added to the class. If the second declared data member is a word
in size and requires only single-word alignment (e.g., int), reordering the data members in
the class can allow the compiler to eliminate the padding after the vptr.
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Click here to purchase the complete PDF document!
Effective C++ in an Embedded Environment
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Slide 23
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Click here to purchase the complete PDF document!
Effective C++ in an Embedded Environment
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Slide 24
Null pointers never get an offset. At runtime, a pointer nullness test must be performed
before applying an offset.
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Click here to purchase the complete PDF document!
Effective C++ in an Embedded Environment
class B2 {
public:
virtual void mf(); // may be overridden in
... // derived classes
};
void g(B2 *pb2) // as before
{
pb2->mf(); // requires offset adjustment
} // before calling mf?
An adjustment is needed only if D overrides mf and pb2 really points to a D.
What should a compiler do? When generating code for the call,
It may not know that D exists.
It can’t know whether pb2 points to a D.
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Slide 25
I don’t remember the details, but both B1 and B2 need to declare mf for the information on
this slide to be true for VC++. For g++, I believe it suffices for only B2 to declare mf.
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Click here to purchase the complete PDF document!
Effective C++ in an Embedded Environment
These special vptrs and vtbls apply only to derived class objects.
Virtual functions for B1 and B2 objects are implemented as described
before.
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Slide 26
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Click here to purchase the complete PDF document!
Effective C++ in an Embedded Environment
Virtual
Function (*pObject->vptr[index])(pObject+Δ);
Impls.
Virtual
this adjustment Function
Impls.
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Slide 27
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Click here to purchase the complete PDF document!
Effective C++ in an Embedded Environment
Thunk Implementation
Often a function with multiple entry points:
this adjustment
From
jump
vtbls
this adjustment
Virtual
Function
Implementation
Return
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Slide 28
I’m guessing about the jump in the diagram. An alternative would be for one thunk to fall
through to the next, with the sum of the offset adjustments calculated to ensure that the
proper this value is in place when the function body is entered.
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Click here to purchase the complete PDF document!
Effective C++ in an Embedded Environment
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Slide 29
As I recall, g++ enters the function into both vtbls, but VC++ enters it into only the vtbl for
B2. This means that the call in red shown above would use the B2 vtbl under VC++, and
that means that there’d be a D*→B2* offset adjustment made prior to calling through the
B2 vtbl.
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Click here to purchase the complete PDF document!
Effective C++ in an Embedded Environment
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Slide 30
The quote is from Lippman’s Inside the C++ Object Model, for which there is a full reference
in the “Further Information” slides at the end of the notes.
Scott Meyers, Software Development Consultant © 2012 Scott Meyers, all rights reserved.
http://www.aristeia.com/ Click here to purchase the complete PDF document!