0% found this document useful (0 votes)
53 views

Doulos QA - Admin

This document contains a question and answer session on migrating from embedded C to C++. Participants asked questions about C++ standards, error handling without exceptions, static initialization issues, and when C may still be preferable to embedded C++. The responses indicated that modern C++ is quite efficient for embedded applications and address historic issues like static initialization and overhead. It was recommended to choose C++ over C whenever possible.

Uploaded by

Maja Peric
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
53 views

Doulos QA - Admin

This document contains a question and answer session on migrating from embedded C to C++. Participants asked questions about C++ standards, error handling without exceptions, static initialization issues, and when C may still be preferable to embedded C++. The responses indicated that modern C++ is quite efficient for embedded applications and address historic issues like static initialization and overhead. It was recommended to choose C++ over C whenever possible.

Uploaded by

Maja Peric
Copyright
© © All Rights Reserved
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 10

Welcome to Migrating from Embedded C to C++

Doulos QA - Admin (to All - Entire


Audience):
11.01: Welcome if you're joining us after the start of the webinar.

Doulos QA - Admin (to All - Entire


Audience):
11.01: Please use the text chat in the GoToWebinar control panel to ask any questions about the
content of the presentation throughout the broadcast.

Doulos QA - Admin (to All - Entire


Audience):
11.09: Expert technical staff are online both during and after the presentation to answer your
questions by text.

Audience Question
Q: Is Embedded C++ standard used in
Arduino IDE, or they have their own version
of C++?
A: Aruduino supports C++11 since version 1.6.6 by default.
Audience Question
Q: Can we apply the same (that EC++ is
quite efficient nowadays) to relatively
modern 8-bit/16-bit low power
microcontrollers, like msp430, atmega328p
etc.
A: Yes, properly understand C++ works very well with smaller processors too if the cross-
compilers exist for the target processors. The classic argument against C++ was memory
utilization and overhead. Features of C++11 now avoid most of that assuming the programmer
understands the issues, which we teach in our C++ for Embedded Systems courses.

Audience Question
Q: what you mean by static cast?
A: In C, there is only one type of cast. For example (char*)var. In C++, there are at least four
different types of cast to differentiate the safety and appropriateness of a particular cast. Static
cast is a cast the compiler can determine as completely safe at compile-time (e.g., a downcast
from a compatible class). For example, static_cast<Drrived*>(Base*).

Audience Question
Q: What about C++14, C++17 and C++20?
A: For many compilers these newer standards are simply not yet supported; although, that is
rapidly changing. Considered as a group, C++11 through C++20 are now commonly referred to
as Modern C++. The largest set of changes came in 2011 and so many think of this as just C+
+11. C++14 made mostly minor changes/fixes. We will be addressing the newer versions as we
see them becoming more supported and more popular. Some aspects only apply to larger
embedded systems that use Embedded Linux. Smaller systems may not benefit as much from
more recent features.
Audience Question
Q: Code for ESP32 using esp-idf - Is it ready
and stable to C++?
A: You will need to consult the tool vendor on that.

Audience Question
Q: how to reduce function calling overhead?
still with inline keyword or compiler will do?
A: Functions defined inside the class declaration are automatically inline. Modern compilers are
also quite good at automatically inlining functions. There are other overheads to be aware of, but
are easily avoided. For instance, dynamic polymorphic overhead can be avoided by simply
avoiding virtual methods. Exception overhead can be avoided with a switch on most compilers
(often turned off by default) or applying the 'noexcept' keyword. C++ is much more sensitive to
these overheads than it used to be.

Audience Question
Q: What is the difference between using
namespace and a class with all static
declararations?
A: Namespaces allow you to keep anything you want inside its own namespace and not just
private to a file. You can use the same namespace in two different files and they will only see
each other.

Audience Question
Q: why not make the "private" functions -
static ? to avoid linker error
A: Static has two meanings in C, but has been updated in C++ to have the original definition
(which was not the "private" one"). Namespaces can be used to avoid the linker clashes. There is
also something called an anoymous namespace, which is file specific (i.e. not shared).

Doulos QA - Admin (to All - Entire


Audience):
11.31: Thanks for all your questions - we've had quite a few in Q&A. We will get to each one as
soon as possible and try to prioritise questions closely linked to the webinar content. Thanks for
your patience!

Audience Question
Q: why cann't we use class here? instead of
just namespace
A: Classes can be used, but namespaces are easier to understand and apply as an initial concept.
Classes and OO programming are definitely useful and will be discussed.

Audience Question
Q: Is it beneficial to keep a file with multiple
namespaces rather than split them out into
seprate files?
A: That depends. Multiple smaller files is easier to manage, but larger files can provide more
compiler optimization opportunities. We recommend more files, and then later use multi-file
compilation if combining them for better compilation results is desired.

Audience Question
Q: I have the feeling that using namespaces is
like making a class that has structs and
functions. What is the difference between
making a class or a namespace?
A: Namespaces are not a replacement for classes. In fact many times multiple classes are
included shared namespaces. Namespaces have to do with keeping variable/function scopes from
clashing. For example a library supporting communications might have a namespace and several
classes. This would allow it to worry less about collisions with another library (e.g. an RTOS).

Audience Question
Q: Not a question, but related to a previous
question, modern compilers also do
devirtualization very well, so even using
polymorphism can be safe as long as you
understand what you are doing.
A: Correct. Modern compilers are very good at optimization.

Doulos QA - Admin (to All - Entire


Audience):
11.39: Sorry for the delay in answering your questions quickly - there are lots in Q&A! We'll get
there as quickly as we can.

Audience Question
Q: What would you recommend for error
handling when turning off exceptions?
A: That is a complex question and depends on the sitatuation. Turning off exceptions makes the
code less entwined and more appropriate for small embedded. Doing so means you will need to
use techniques for error handling similar to C. So you have to choose. In some areas, exception
overhead is unacceptable, but realize when you add ad hoc (managed by the programmer)
handling, it can be challenging too.
Audience Question
Q: What exactly is the difference between
“pass by reference” in C and in C++?
A: In C there is no "pass-by-reference" direct semantics. Instead C uses pass-by-value or pass-
by-pointer (the pointer itself is passed by value). Pass-by-reference means the compiler takes
care of the pointer issues for you and removes a source of error. So C++ has pass-by-value, pass-
by-pointer, and pass-by-reference. C++ programmers tend to avoid pass-by-pointer if they can.
There are also pass-by-const-reference, which provides read-only semantics, which adds safety.

Audience Question
Q: I usually structure overlay for registers,
really wondering how cpp class helps. Is
similar overlay possible in cpp class. ?
A: Yes. Discussed a bit later.

Audience Question
Q: Could you also say something about the
embedded applications that require
compliance of Safety Standards ? Is this
achievable with C++ ?
A: Safety standards do exist (e.g. MISRA C++)

Audience Question
Q: how to select laguage C or C++ depending
on application?
A: Personally, if the tools support it, I would choose C++ every time. C++ has C as a proper
subset with only a few minor differences.

Audience Question
Q: When using classes with methods and
fields, but without virtual methods, what will
be overhead (with EC++)?
A: Basically zero.

Doulos QA - Admin (to All - Entire


Audience):
11.48: Doxygen link:
https://github.com/theolind/mahm3lib/wiki/Integrating-Doxygen-with-Eclipse

Audience Question
Q: How optimal are open source GCC
complilers for particular microcontrollers, or
vendor's native EC++ compilers are typically
more optimal?
A: GCC (g++) is quite good as is LLVM (clang++). Obviously, a processor vendor may have a
targeted more optimal tool, but that is a moving target.

Audience Question
Q: It's been a time since I used C++ in
embedded. Is there nowadays a reasonable
solution to the static initialization fiasco?
A: There are now parts of the standard that better address the static initialization issue. More
complex than will be discussed here (not enough time).
Audience Question
Q: Does this tool has Linux support?
A: C++ is a programming language. You have the question backwards. Yes, Linux supports C++
in the sense you can easily interface with OS calls. Linux itself is written in C, but that does not
stop use of C++ (or Java or Python or Rust).

Audience Question
Q: What can be a strategy for processing of
"strong" errors. In desktop computers it can
be acceptible to rise exception from C++
code, show error message and stop the
program. What can we do for
microcontrollers, which run unattended, and
there is no possibility to "stop" the program?
What resources would you recommend to
look at?
A: That is a webinar in itself with many approaches depending on the application area. For small
microcontrollers, the answer may simply be to call terminate(), which proceeds to reboot
Q: What can be a strategy for processing of
"strong" errors. In desktop computers it can
be acceptible to rise exception from C++
code, show error message and stop the
program. What can we do for
microcontrollers, which run unattended, and
there is no possibility to "stop" the program?
What resources would you recommend to
look at?
A: That is a webinar in itself with many approaches depending on the application area. For small
microcontrollers, the answer may simply be to call terminate(), which proceeds to reboot.

Audience Question
Q: Are there any projects or situations, where
C could still be preferable over EC++ (not
assuming supporting legacy code)?
A: I don't think so. With few exceptions, you can use the C subset in C++ without any issue and
identical code. With some of the features of Modern C++, the code is actually smaller and faster.
Many embedded teams are switching. Teams that don't change, simply don't have a complete
understanding of the issue in most cases. There is of course an issue with education.

Audience Question
Q: What about using STL ? Is this relevant ?
A: In many cases, the STL is not appropriate for embedded because it uses a lot of dynamic
memory and virtualization; however, some of that is changing. For instance, the std::array<T>
container has efficiencies identical to standard C arrays with extra safety.
Audience Question
Q: What runs faster, calling a function by a
functionPointer or calling a virtual function.
A: This does not have an absolute answer. The underlying mechanisms are often identical;
however, C++ compilers have the opportunity for more optimizations because the higher level
abstraction gives it better insight into the desired requirements.

You might also like