Reader small image

You're reading from  Hands-On Embedded Programming with C++17

Product typeBook
Published inJan 2019
Reading LevelIntermediate
PublisherPackt
ISBN-139781788629300
Edition1st Edition
Languages
Tools
Right arrow
Author (1)
Maya Posch
Maya Posch
author image
Maya Posch

Maya Posch is a senior C++ developer with more than 15 years of experience. Discovering the joys of programming early on, and later the joys of electronics, she has always expressed a profound interest in technology, a passion that she gladly shares with others. Describing herself as a C developer who happens to like C++ and Ada, she likes to seek the limits of what can be done with the minimum of code and hardware to accomplish everything that is cool, new, and exciting. She also enjoys FPGA development, AI, and robotics research, in addition to creative writing, music, and drawing.
Read more about Maya Posch

Right arrow

Chapter 2. C++ as an Embedded Language

When it comes to embedded development on resource-restricted systems, it is still common to consider only C and ASM as viable choices, accompanied by the thought that C++ has a larger footprint than C, or adds a significant amount of complexity. In this chapter, we will look at all of these issues in detail and consider the merits of C++ as an embedded programming language:

  • C++ relative to C
  • Advantages of C++ as a multi-paradigm language
  • Compatibility with existing C and ASM
  • Changes with C++11, C++14, and C++17

C++ relative to C


The lineages of C and C++ both trace their lineage back to the ALGOL programming language, which saw its first version in 1958 (ALGOL 58), followed by updates in 1960 and 1968. ALGOL introduced the concept of imperative programming—a programming style in which statements explicitly tell the machine how to make changes to data for output and control flow.

A paradigm that emerges rather naturally from imperative programming is theuse of procedures. We will start with an example, to introduce the terminology. Procedures are synonymous to sub-routines and functions. They identify the groups of statements and make them self-contained, which has the effects of confiningthereachofthese statements to the limited scope of the section they are contained within, creating hierarchy and consequentially introducing these procedures as new, more abstract statements. Heavy use of this procedural programming style finds its place in so-called structured programming, alongside loop and branching...

C++ as an embedded language


Around 1983 when C++ had just been conceived and got its name, popular personal computer systems for a general audience, as well as businesses, had specifications like ones listed in the following table:

System

CPU

Clock speed (MHz)

RAM (KB)

ROM (KB)

Storage (KB)

BBC Micro

6502 (B+ 6512A)

2

16-128

32-128

Max 1,280 (ADFS floppy)

Max 20 MB (hard drive)

MSX

Zilog Z80

3.58

8-128

32

720 (floppy)

Commodore 64

6510

~1

64

20

1,000 (tape)

170 (floppy)

Sinclair ZX81

Zilog Z80

3.58

1

8

15 (cartridge)

IBM PC

Intel 8080

4.77

16-256

8

360 (floppy)

 

Now compare these computer systems to a recent 8-bit microcontroller (MCU) such as the AVR ATMega 2560 with the following specifications:

  • 16 MHz clock speed
  • 8 KB RAM
  • 256 KB ROM (program)
  • 4 KB ROM (data)

The ATMega 2560 was launched in 2005 and is among the more powerful 8-bit MCUs available nowadays. Its features stack up favorably against the 1980s computer systems, but on top of that the MCU does not rely on any external memory components.

The MCU core clock speed is significantly...

C++ language features


We previously took a look at the explicit nature of changes to data and system state that defines imperative programming as opposed to declarative programming, where instead of manipulating data in a loop such functionality could be declared as mapping an operator to some data, thus spelling out the functionality, not the specific order of operations. But why should programming languages necessarily be a choice between imperative and declarative paradigms?

In fact, one of the main distinguishing features of C++ is its multi-paradigm nature making use of both imperative and declarative paradigms. With the inclusion of object-oriented, generic, and functional programming into C++ in addition to C's procedural programming, it would seem natural to assume that this would all have to come at a cost, whether in terms of higher CPU usage or more RAM and/or ROM consumed.

However, as we learned earlier in this chapter, C++ language features are ultimately built upon the C language...

The standard template library


The standard library for C++ (STL) contains a comprehensive and ever-growing collection of functions, classes, and more that allows for common tasks to be performed without having to rely on external libraries. The STL string class is very popular, and allows you to safely handle strings without having to deal with null terminators and anything similar.

Most embedded platforms support all or at least a significant part of the STL, barring limitations on available RAM and the like that prevent the implementation of full hash tables and other complex data structures. Many embedded STL implementations contain optimizations for the target platform, minimizing RAM and CPU usage.

Maintainability


In the preceding sections, we have seen a number of features that C++ offers, and the viability of using them on a resource-limited platform. A big advantage of using C++ is the reduction in code size you can accomplish through the use of templates, along with the organization and modularization of a code base using classes, namespaces, and the like.

By striving for a more modular approach in your code, with clear interfaces between modules, it becomes more feasible to reuse code between projects. It also simplifies the maintenance of code by making the function of a particular section of code clearer and providing clear targets for unit and integration testing.

Summary


In this chapter, we tackled the big question of why you would wish to use C++ for embedded development. We saw that, due to the courtesy of C++'s development, it is highly optimized for resource-constrained platforms, while providing a large number of features essential to project management and organization.

The reader should, at this point, be able to describe C++'s main features and provide concrete examples of each. When writing C++ code, the reader will have a clear idea of the cost of a particular language feature, being able to reason why one implementation of a section of code is preferable to another implementation, based on both space and RAM constraints.

In the next chapter, we will take a look at the development process for embedded Linux and similar systems, based on single-board computers (SBCs) and similar.

lock icon
The rest of the chapter is locked
You have been reading a chapter from
Hands-On Embedded Programming with C++17
Published in: Jan 2019Publisher: PacktISBN-13: 9781788629300
Register for a free Packt account to unlock a world of extra content!
A free Packt account unlocks extra newsletters, articles, discounted offers, and much more. Start advancing your knowledge today.
undefined
Unlock this book and the full library FREE for 7 days
Get unlimited access to 7000+ expert-authored eBooks and videos courses covering every tech area you can think of
Renews at $15.99/month. Cancel anytime

Author (1)

author image
Maya Posch

Maya Posch is a senior C++ developer with more than 15 years of experience. Discovering the joys of programming early on, and later the joys of electronics, she has always expressed a profound interest in technology, a passion that she gladly shares with others. Describing herself as a C developer who happens to like C++ and Ada, she likes to seek the limits of what can be done with the minimum of code and hardware to accomplish everything that is cool, new, and exciting. She also enjoys FPGA development, AI, and robotics research, in addition to creative writing, music, and drawing.
Read more about Maya Posch