SlideShare a Scribd company logo
1 of 23
Exiting the Dark Ages of Void* with C++11
*YOU* are full of bullshit.
C++ is a horrible language. It's made more horrible by the fact that a
lot of substandard programmers use it, to the point where it's much
much easier to generate total and utter crap with it. Quite frankly,
even if the choice of C were to do *nothing* but keep the C++
programmers out, that in itself would be a huge reason to use C.
-Linus Torvalds (2007)
http://harmful.cat-v.org/software/c++/linus
What is an embedded system?
Well, a lot of different things, but generally speaking:
● Dedicated to a single purpose
● Minimalist approach to reduce cost
● “Always on”
● Has stiff reliability requirements
● uses safeguards like watchdogs for recovery
Software selection based on
hardware?
Bare metal:
ATtiny85, 8K of flash
512 byte of SRAM
512 bytes of EEPROM
general-
purpose OS
(hopefully
ruggedized)
● Bare metal or
microkernel / RTOS
● Lots of C!
(ok, NASA uses some ADA)
Myths about C++ in embedded systems
● C++ code is slow compared to C
● C++ produces binaries too big for embedded systems
● Templates create too much code “bloat”
● Dynamic dispatch is slow. Virtual functions are too “heavyweight”.
● C++ is synonymous with dynamic allocation
● It’s too hard to port C++ to other architectures
● STL is the devil for embedded systems, therefore you shouldn’t use C++
Myth #1: C++ is slow
Truth: If you use C++ as
“C with classes” you will
get virtually indistinguishable
performance. Other lightweight
features add minimal overhead.
Certain features of C++ can be too slow or otherwise
unsuitable for certain embedded applications.
Myth #2: C++ Binaries Are Too Big
Truth:
Using a reasonable subset of features leads to similar
binary files sizes.
Frequently, higher level abstractions can reduce
copy/paste coding.
libstdc++ is big. It’s also frequently not available!
Myth #3: Templates create too much code bloat
Truth:
If you factor template independent code outside of your
templates, you can minimize this effect. Every good C++
programmer knows this.
Extensive use of macros in C produce bloat just as easily,
with more obfuscation, and with less type safety.
Myth #4: Dynamic Dispatch is “Slow”
Truth: The overhead from virtual functions &
vtables is actually pretty acceptable for almost
all applications.
● one vtable per class (not per object)
● one pointer to the vtable per object
● one dereferencing operation per call (a couple cycles on most hardware)
Virtual functions can be abused like any language feature. However, abstraction via
interfaces (abstract classes in C++) is too powerful a tool for building APIs and testing code
to ignore.
Lulz
http://freshbsd.org/commit/openbsd/8a6680833c42bde7de74b9ddb70bbad193c5359b
“Why do we hide from the
OpenSSL police, dad?”
“Because they're not like us,
son. They use macros to wrap
stdio routines, for an
undocumented
(OPENSSL_USE_APPLINK) use
case, which only serves to
obfuscate the code.”
Myth #5: C++ == dynamic allocation
Truth:
There is nothing about C++
that forces you to use dynamic
allocation.
If you don’t like the STL, don’t
use it. The STL is a library not
the language.
Myth #6: C++ leads to non-portable code
Truth:
This used to be true. C++ compilers have made
enormous strides in recent years for a number of
reasons. Clang and GCC are now driving progress, and
even MSVC++ is quite compliant to the standard.
If you develop with multiple compilers frequently and
avoid platform extensions, you’ll have excellent
portability. C++ abstractions make it easier to separate
platform-specific code.
Lulz
“I'm glad to know that Ultrix CC
has a bug optimizing switch()
statements lacking an explicit
`case 0:' construct. But Ultrix has
been dead for more than 15 years,
really. Don't give it any reason to
move out of its coffin.”
http://freshbsd.org/commit/openbsd/111423c9d25cd2128223242f0a3d7dcaa9acb3b5
Why is C++ “better now”?
● Renewed interest in native languages
● Clang (C-family front-end compiler for LLVM) has been pushing GCC to
improve
● Coopetition between Intel/Microsoft/OSS tools to be “conformant”
● Many embedded vendors have switched from proprietary toolchains to
GCC-based tools (expect even more to switch to Clang/LLVM)
What is Clang/LLVM?
LLVM is a language-agnostic compiler architecture. It separates language-
specific lexing / early-stage compilation from the translation & optimization of
specific targets.
Interested in creating a new language? No problem, it will easily be able to
target different architectures if your compile to LLVM.
Interested in adding support for a new architecture? Great, you only have to
translate LLVM instructions to your target.
“Lightweight” C++ for MCUs (MISRA?)
Free (no significant overhead vs. C)
● Classes
● Constructors / destructors & OOP modularity
● Single inheritance
● C++11 lambdas, constexpr, access modifiers
● templates (if factored appropriately)
Cheap:
● Virtual methods
Costly to some metric (performance / determinism / code size / runtime / memory / etc)
● dynamic allocation
● Virtual inheritance
● Run-time type information (RTTI == dynamic_cast<> & typeid)
● Exceptions
How can C++ improve robustness/security?
● More powerful abstractions
o private members - data hiding / encapsulation
o const correctness - enforce immutability
o deterministic destructors - guaranteed resource release
● Wrappers to that mediate access to dangerous resources
o e.g. buffer views (wrappers)
● Better type-safety means that you make fewer assumptions in code.
● Powerful design patterns can be applied to encourage better practices
Simple pattern, big effect: RAII
The Problem (pseudo code in C)
int stupidprefix_some_function_that_returns_error_code(.... arguments)
{
int err = 0;
stupidprefix_acquire_resource(&resource);
stupidprefix_do_stuff_with_resource(&resource);
// … yada yada lots of code branches that require resource
// from which you can’t return (hint: use GOTO)
CLEANUP:
stupidprefix_release_resource(&resource);
return err;
}
C++ version w/ RAII
namespace kickasswehavenamespaces
{
int some_function_that_returns_error_code(.... arguments)
{
Acquisition mediator(raw_resource);
do_stuff_with_resource(mediator);
//... Code branches
//... Any return calls mediator’s destructor
return 0;
}
}
The act of acquiring a resource
guarantees its release.
“Resource Acquisition Is
Initialization”.
Java doesn’t even have an
acceptable substitute.
C# has “using” statement.
“embeddable” C++11 features
● static_assert - Compile time assertions complete with custom compiler
error messages. Ensure that your library is being configured with safe
constants / types.
● constexpr - Compile time calculations (yet another Turing complete
metaprogramming language)
● Access modifiers for method (override final)
What specific design aims guided
the committee?
“Improve performance and ability to
work directly with hardware -- make
C++ even better for embedded
systems programming and high-
performance computation.”
http://www.stroustrup.com/C++11FAQ.html
So, dnp3 on Atmega2560?
CPU: 8-bit AVR
Max. Operating Freq. (MHz): 16 MHz
SRAM: 8K
Flash (Kbytes): 256 Kbytes
https://github.com/automatak/dnp3/blob/2.0.x/embedded/atmelavr/demooutstation
/main.cpp
AVR DNP3 Demo
● C++11 using GCC 4.8.1 and 8-bit AVR backend
● USART only at this point
● Level 3 stack - more feature-rich than many RTUs / IEDs
● All statically allocated
● All interrupt based event-loop
● Makes use of AVR sleep mode
● SAv5? maybe possible? Definitely on ARM Cortex (Arduino DUE)
● Community members have been prototyping on TI ARM & PIC
● Need an interesting I/O shield to make a real “IED”

More Related Content

What's hot

Presentation1
Presentation1Presentation1
Presentation1
kpkcsc
 
Introduction to llvm
Introduction to llvmIntroduction to llvm
Introduction to llvm
Tao He
 
Dotnet framework
Dotnet frameworkDotnet framework
Dotnet framework
Nitu Pandey
 
"Making OpenCV Code Run Fast," a Presentation from Intel
"Making OpenCV Code Run Fast," a Presentation from Intel"Making OpenCV Code Run Fast," a Presentation from Intel
"Making OpenCV Code Run Fast," a Presentation from Intel
Edge AI and Vision Alliance
 

What's hot (20)

Native development kit (ndk) introduction
Native development kit (ndk)  introductionNative development kit (ndk)  introduction
Native development kit (ndk) introduction
 
Presentation1
Presentation1Presentation1
Presentation1
 
Engineer Engineering Software
Engineer Engineering SoftwareEngineer Engineering Software
Engineer Engineering Software
 
Mbuf oflow - Finding vulnerabilities in iOS/macOS networking code - kevin ba...
 Mbuf oflow - Finding vulnerabilities in iOS/macOS networking code - kevin ba... Mbuf oflow - Finding vulnerabilities in iOS/macOS networking code - kevin ba...
Mbuf oflow - Finding vulnerabilities in iOS/macOS networking code - kevin ba...
 
Android Native Development Kit
Android Native Development KitAndroid Native Development Kit
Android Native Development Kit
 
Android NDK
Android NDKAndroid NDK
Android NDK
 
Net framework
Net frameworkNet framework
Net framework
 
Android Developer Meetup
Android Developer MeetupAndroid Developer Meetup
Android Developer Meetup
 
Introduction to llvm
Introduction to llvmIntroduction to llvm
Introduction to llvm
 
NDK Programming in Android
NDK Programming in AndroidNDK Programming in Android
NDK Programming in Android
 
GPCE16 Poster: Automatic Non-functional Testing of Code Generators Families
GPCE16 Poster: Automatic Non-functional Testing of Code Generators Families GPCE16 Poster: Automatic Non-functional Testing of Code Generators Families
GPCE16 Poster: Automatic Non-functional Testing of Code Generators Families
 
NDK Introduction
NDK IntroductionNDK Introduction
NDK Introduction
 
Python for IoT, A return of experience
Python for IoT, A return of experiencePython for IoT, A return of experience
Python for IoT, A return of experience
 
برنامه نویسی مستقل از پلتفرم با استفاده از .NET Core و C#
برنامه نویسی مستقل از پلتفرم با استفاده از .NET Core و C#برنامه نویسی مستقل از پلتفرم با استفاده از .NET Core و C#
برنامه نویسی مستقل از پلتفرم با استفاده از .NET Core و C#
 
Introduction to the Android NDK
Introduction to the Android NDKIntroduction to the Android NDK
Introduction to the Android NDK
 
Dotnet framework
Dotnet frameworkDotnet framework
Dotnet framework
 
"Making OpenCV Code Run Fast," a Presentation from Intel
"Making OpenCV Code Run Fast," a Presentation from Intel"Making OpenCV Code Run Fast," a Presentation from Intel
"Making OpenCV Code Run Fast," a Presentation from Intel
 
Dennis Benkert & Matthias Lübken - Patterns in a containerized world? - code....
Dennis Benkert & Matthias Lübken - Patterns in a containerized world? - code....Dennis Benkert & Matthias Lübken - Patterns in a containerized world? - code....
Dennis Benkert & Matthias Lübken - Patterns in a containerized world? - code....
 
Ndk
NdkNdk
Ndk
 
Using the android ndk - DroidCon Paris 2014
Using the android ndk - DroidCon Paris 2014Using the android ndk - DroidCon Paris 2014
Using the android ndk - DroidCon Paris 2014
 

Similar to Legacy of Void*

Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJIntroduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
meharikiros2
 
tybsc it asp.net full unit 1,2,3,4,5,6 notes
tybsc it asp.net full unit 1,2,3,4,5,6 notestybsc it asp.net full unit 1,2,3,4,5,6 notes
tybsc it asp.net full unit 1,2,3,4,5,6 notes
WE-IT TUTORIALS
 
Summer training PPT Manasv Singharia.pptx
Summer training PPT Manasv Singharia.pptxSummer training PPT Manasv Singharia.pptx
Summer training PPT Manasv Singharia.pptx
shokeenk14
 
ASP.NET Session 1
ASP.NET Session 1ASP.NET Session 1
ASP.NET Session 1
Sisir Ghosh
 

Similar to Legacy of Void* (20)

02 c++g3 d
02 c++g3 d02 c++g3 d
02 c++g3 d
 
Introduction-to-C-Part-1.pdf
Introduction-to-C-Part-1.pdfIntroduction-to-C-Part-1.pdf
Introduction-to-C-Part-1.pdf
 
Introduction-to-C-Part-1.pptx
Introduction-to-C-Part-1.pptxIntroduction-to-C-Part-1.pptx
Introduction-to-C-Part-1.pptx
 
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJIntroduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
Introduction-to-C-Part-1 JSAHSHAHSJAHSJAHSJHASJ
 
C++Basics2022.pptx
C++Basics2022.pptxC++Basics2022.pptx
C++Basics2022.pptx
 
Introduction-to-C-Part-1 (1).doc
Introduction-to-C-Part-1 (1).docIntroduction-to-C-Part-1 (1).doc
Introduction-to-C-Part-1 (1).doc
 
C++ helps you to format the I/O operations like determining the number of dig...
C++ helps you to format the I/O operations like determining the number of dig...C++ helps you to format the I/O operations like determining the number of dig...
C++ helps you to format the I/O operations like determining the number of dig...
 
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...
C++ was developed by Bjarne Stroustrup, as an extension to the C language. cp...
 
ewili13_submission_14
ewili13_submission_14ewili13_submission_14
ewili13_submission_14
 
Introduction to .NET by QuontraSolutions
Introduction to .NET by QuontraSolutionsIntroduction to .NET by QuontraSolutions
Introduction to .NET by QuontraSolutions
 
Unit 1 of c++ part 1 basic introduction
Unit 1 of c++ part 1 basic introductionUnit 1 of c++ part 1 basic introduction
Unit 1 of c++ part 1 basic introduction
 
Advance Android Application Development
Advance Android Application DevelopmentAdvance Android Application Development
Advance Android Application Development
 
tybsc it asp.net full unit 1,2,3,4,5,6 notes
tybsc it asp.net full unit 1,2,3,4,5,6 notestybsc it asp.net full unit 1,2,3,4,5,6 notes
tybsc it asp.net full unit 1,2,3,4,5,6 notes
 
Summer training PPT Manasv Singharia.pptx
Summer training PPT Manasv Singharia.pptxSummer training PPT Manasv Singharia.pptx
Summer training PPT Manasv Singharia.pptx
 
02 c++g3 d (1)
02 c++g3 d (1)02 c++g3 d (1)
02 c++g3 d (1)
 
Hacking the Kinect with GAFFTA Day 1
Hacking the Kinect with GAFFTA Day 1Hacking the Kinect with GAFFTA Day 1
Hacking the Kinect with GAFFTA Day 1
 
Introduction to .net framework
Introduction to .net frameworkIntroduction to .net framework
Introduction to .net framework
 
ASP.NET Session 1
ASP.NET Session 1ASP.NET Session 1
ASP.NET Session 1
 
Introduction to .net FrameWork by QuontraSolutions
Introduction to .net FrameWork by QuontraSolutionsIntroduction to .net FrameWork by QuontraSolutions
Introduction to .net FrameWork by QuontraSolutions
 
Bounded Model Checking for C Programs in an Enterprise Environment
Bounded Model Checking for C Programs in an Enterprise EnvironmentBounded Model Checking for C Programs in an Enterprise Environment
Bounded Model Checking for C Programs in an Enterprise Environment
 

Recently uploaded

TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
mohitmore19
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
masabamasaba
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
9953056974 Low Rate Call Girls In Saket, Delhi NCR
 

Recently uploaded (20)

The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdfThe Ultimate Test Automation Guide_ Best Practices and Tips.pdf
The Ultimate Test Automation Guide_ Best Practices and Tips.pdf
 
TECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service providerTECUNIQUE: Success Stories: IT Service provider
TECUNIQUE: Success Stories: IT Service provider
 
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
%+27788225528 love spells in Boston Psychic Readings, Attraction spells,Bring...
 
Right Money Management App For Your Financial Goals
Right Money Management App For Your Financial GoalsRight Money Management App For Your Financial Goals
Right Money Management App For Your Financial Goals
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park %in kempton park+277-882-255-28 abortion pills for sale in kempton park
%in kempton park+277-882-255-28 abortion pills for sale in kempton park
 
Unlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language ModelsUnlocking the Future of AI Agents with Large Language Models
Unlocking the Future of AI Agents with Large Language Models
 
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
%in kaalfontein+277-882-255-28 abortion pills for sale in kaalfontein
 
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
Direct Style Effect Systems -The Print[A] Example- A Comprehension AidDirect Style Effect Systems -The Print[A] Example- A Comprehension Aid
Direct Style Effect Systems - The Print[A] Example - A Comprehension Aid
 
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
%in Lydenburg+277-882-255-28 abortion pills for sale in Lydenburg
 
Software Quality Assurance Interview Questions
Software Quality Assurance Interview QuestionsSoftware Quality Assurance Interview Questions
Software Quality Assurance Interview Questions
 
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
call girls in Vaishali (Ghaziabad) 🔝 >༒8448380779 🔝 genuine Escort Service 🔝✔️✔️
 
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICECHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
CHEAP Call Girls in Pushp Vihar (-DELHI )🔝 9953056974🔝(=)/CALL GIRLS SERVICE
 
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) SolutionIntroducing Microsoft’s new Enterprise Work Management (EWM) Solution
Introducing Microsoft’s new Enterprise Work Management (EWM) Solution
 
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...Chinsurah Escorts ☎️8617697112  Starting From 5K to 15K High Profile Escorts ...
Chinsurah Escorts ☎️8617697112 Starting From 5K to 15K High Profile Escorts ...
 
VTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learnVTU technical seminar 8Th Sem on Scikit-learn
VTU technical seminar 8Th Sem on Scikit-learn
 
Generic or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisionsGeneric or specific? Making sensible software design decisions
Generic or specific? Making sensible software design decisions
 
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa%in tembisa+277-882-255-28 abortion pills for sale in tembisa
%in tembisa+277-882-255-28 abortion pills for sale in tembisa
 
Architecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the pastArchitecture decision records - How not to get lost in the past
Architecture decision records - How not to get lost in the past
 
%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban%in Durban+277-882-255-28 abortion pills for sale in Durban
%in Durban+277-882-255-28 abortion pills for sale in Durban
 

Legacy of Void*

  • 1. Exiting the Dark Ages of Void* with C++11
  • 2. *YOU* are full of bullshit. C++ is a horrible language. It's made more horrible by the fact that a lot of substandard programmers use it, to the point where it's much much easier to generate total and utter crap with it. Quite frankly, even if the choice of C were to do *nothing* but keep the C++ programmers out, that in itself would be a huge reason to use C. -Linus Torvalds (2007) http://harmful.cat-v.org/software/c++/linus
  • 3. What is an embedded system? Well, a lot of different things, but generally speaking: ● Dedicated to a single purpose ● Minimalist approach to reduce cost ● “Always on” ● Has stiff reliability requirements ● uses safeguards like watchdogs for recovery
  • 4. Software selection based on hardware? Bare metal: ATtiny85, 8K of flash 512 byte of SRAM 512 bytes of EEPROM general- purpose OS (hopefully ruggedized) ● Bare metal or microkernel / RTOS ● Lots of C! (ok, NASA uses some ADA)
  • 5. Myths about C++ in embedded systems ● C++ code is slow compared to C ● C++ produces binaries too big for embedded systems ● Templates create too much code “bloat” ● Dynamic dispatch is slow. Virtual functions are too “heavyweight”. ● C++ is synonymous with dynamic allocation ● It’s too hard to port C++ to other architectures ● STL is the devil for embedded systems, therefore you shouldn’t use C++
  • 6. Myth #1: C++ is slow Truth: If you use C++ as “C with classes” you will get virtually indistinguishable performance. Other lightweight features add minimal overhead. Certain features of C++ can be too slow or otherwise unsuitable for certain embedded applications.
  • 7. Myth #2: C++ Binaries Are Too Big Truth: Using a reasonable subset of features leads to similar binary files sizes. Frequently, higher level abstractions can reduce copy/paste coding. libstdc++ is big. It’s also frequently not available!
  • 8. Myth #3: Templates create too much code bloat Truth: If you factor template independent code outside of your templates, you can minimize this effect. Every good C++ programmer knows this. Extensive use of macros in C produce bloat just as easily, with more obfuscation, and with less type safety.
  • 9. Myth #4: Dynamic Dispatch is “Slow” Truth: The overhead from virtual functions & vtables is actually pretty acceptable for almost all applications. ● one vtable per class (not per object) ● one pointer to the vtable per object ● one dereferencing operation per call (a couple cycles on most hardware) Virtual functions can be abused like any language feature. However, abstraction via interfaces (abstract classes in C++) is too powerful a tool for building APIs and testing code to ignore.
  • 10. Lulz http://freshbsd.org/commit/openbsd/8a6680833c42bde7de74b9ddb70bbad193c5359b “Why do we hide from the OpenSSL police, dad?” “Because they're not like us, son. They use macros to wrap stdio routines, for an undocumented (OPENSSL_USE_APPLINK) use case, which only serves to obfuscate the code.”
  • 11. Myth #5: C++ == dynamic allocation Truth: There is nothing about C++ that forces you to use dynamic allocation. If you don’t like the STL, don’t use it. The STL is a library not the language.
  • 12. Myth #6: C++ leads to non-portable code Truth: This used to be true. C++ compilers have made enormous strides in recent years for a number of reasons. Clang and GCC are now driving progress, and even MSVC++ is quite compliant to the standard. If you develop with multiple compilers frequently and avoid platform extensions, you’ll have excellent portability. C++ abstractions make it easier to separate platform-specific code.
  • 13. Lulz “I'm glad to know that Ultrix CC has a bug optimizing switch() statements lacking an explicit `case 0:' construct. But Ultrix has been dead for more than 15 years, really. Don't give it any reason to move out of its coffin.” http://freshbsd.org/commit/openbsd/111423c9d25cd2128223242f0a3d7dcaa9acb3b5
  • 14. Why is C++ “better now”? ● Renewed interest in native languages ● Clang (C-family front-end compiler for LLVM) has been pushing GCC to improve ● Coopetition between Intel/Microsoft/OSS tools to be “conformant” ● Many embedded vendors have switched from proprietary toolchains to GCC-based tools (expect even more to switch to Clang/LLVM)
  • 15. What is Clang/LLVM? LLVM is a language-agnostic compiler architecture. It separates language- specific lexing / early-stage compilation from the translation & optimization of specific targets. Interested in creating a new language? No problem, it will easily be able to target different architectures if your compile to LLVM. Interested in adding support for a new architecture? Great, you only have to translate LLVM instructions to your target.
  • 16. “Lightweight” C++ for MCUs (MISRA?) Free (no significant overhead vs. C) ● Classes ● Constructors / destructors & OOP modularity ● Single inheritance ● C++11 lambdas, constexpr, access modifiers ● templates (if factored appropriately) Cheap: ● Virtual methods Costly to some metric (performance / determinism / code size / runtime / memory / etc) ● dynamic allocation ● Virtual inheritance ● Run-time type information (RTTI == dynamic_cast<> & typeid) ● Exceptions
  • 17. How can C++ improve robustness/security? ● More powerful abstractions o private members - data hiding / encapsulation o const correctness - enforce immutability o deterministic destructors - guaranteed resource release ● Wrappers to that mediate access to dangerous resources o e.g. buffer views (wrappers) ● Better type-safety means that you make fewer assumptions in code. ● Powerful design patterns can be applied to encourage better practices
  • 18. Simple pattern, big effect: RAII The Problem (pseudo code in C) int stupidprefix_some_function_that_returns_error_code(.... arguments) { int err = 0; stupidprefix_acquire_resource(&resource); stupidprefix_do_stuff_with_resource(&resource); // … yada yada lots of code branches that require resource // from which you can’t return (hint: use GOTO) CLEANUP: stupidprefix_release_resource(&resource); return err; }
  • 19. C++ version w/ RAII namespace kickasswehavenamespaces { int some_function_that_returns_error_code(.... arguments) { Acquisition mediator(raw_resource); do_stuff_with_resource(mediator); //... Code branches //... Any return calls mediator’s destructor return 0; } } The act of acquiring a resource guarantees its release. “Resource Acquisition Is Initialization”. Java doesn’t even have an acceptable substitute. C# has “using” statement.
  • 20. “embeddable” C++11 features ● static_assert - Compile time assertions complete with custom compiler error messages. Ensure that your library is being configured with safe constants / types. ● constexpr - Compile time calculations (yet another Turing complete metaprogramming language) ● Access modifiers for method (override final)
  • 21. What specific design aims guided the committee? “Improve performance and ability to work directly with hardware -- make C++ even better for embedded systems programming and high- performance computation.” http://www.stroustrup.com/C++11FAQ.html
  • 22. So, dnp3 on Atmega2560? CPU: 8-bit AVR Max. Operating Freq. (MHz): 16 MHz SRAM: 8K Flash (Kbytes): 256 Kbytes https://github.com/automatak/dnp3/blob/2.0.x/embedded/atmelavr/demooutstation /main.cpp
  • 23. AVR DNP3 Demo ● C++11 using GCC 4.8.1 and 8-bit AVR backend ● USART only at this point ● Level 3 stack - more feature-rich than many RTUs / IEDs ● All statically allocated ● All interrupt based event-loop ● Makes use of AVR sleep mode ● SAv5? maybe possible? Definitely on ARM Cortex (Arduino DUE) ● Community members have been prototyping on TI ARM & PIC ● Need an interesting I/O shield to make a real “IED”