Why C++ Programming Continues to Thrive in Embedded Systems


New technologies keep revolutionizing the world of embedded systems. Partnering with xBerry, we join their exciting series of articles exploring the latest technological advancements. In the blog: From Rust to Riches: Will Embedded Applications Ditch C++ for a Brighter Future?, our partners describe the powerful features that Rust brings to the industry. A bit perversely, we take an opposite stand, demonstrating why C++ programming is still very much relevant to the embedded solutions and will be for many years from now.

C++ has been enjoying ongoing popularity. At the time of writing, it ranks as the third most popular programming language in the world on the TIOBE Index (along with its predecessor, C in the second place) and fifth on the PYPL Popularity of Programming Language Index. This performance doesn’t relate just to its historical reputation. In 2022, C++ was the language with the biggest growth in TIOBE’s Index. Software engineers favor its reliability, bare-metal capability and excellent performance.  

However, as new languages are emerging on the programming landscape, many have started to wonder whether it’s time to embrace fresh alternatives such as Rust. After all, C++ has been around for almost 40 years. What is more, the recent security concerns seem to have surpassed its powerful capabilities.

As such, the question arises: has C++ become outdated? It doesn’t look like this at all. In this blog, we show what makes C++ very much alive and kicking.

What is C++? Brief history and development

C++ is a general-purpose programming language designed for high-performance applications. It’s present in areas such as game development, databases, machine learning, and of course embedded systems.

C++ was created by a Danish computer scientist, Bjarne Stroustrup, based on his experience with programming for his Ph.D. thesis. Stroustrup found Simula a convenient but a bit sluggish language to work with and wanted to add similar features to C. He chose C as it was a general-purpose, fast, portable and widely used language. C++ was released as an extension of C and was originally called “C with Classes.”

The language was first commercially used in 1985 and standardized by ISO in 1998. Modern C++ started with a major update, C++11 in 2011. Since then, the ISO committee has been releasing new standards every three years to keep up with the development trends.

Computer in the darkness with lines of C++ code on the screen

Where is C++ programming used?

Over the decades, C++ has dispersed into all different areas of computer technologies. To name a few, the language was used to create Photoshop, MySQL, Microsoft Office, Windows OS and Visual Studio. Parts of services of major tech giants such as Spotify, YouTube, Amazon and Bloomberg are written in C++.  The language also lies at the core of all modern web browsers—Chrome, Edge and Firefox.

C++ is also the primary language of the gaming industry. Engines used by leading video companies— UnityEngine, PhyreEngine and Unreal—are all based on C++.

Moreover, the language also does the heavy lifting in AI-driven applications, allowing for the creation of complex models and algorithms. Major platforms such as PyTorch and TensorFlow are written in C++.

Finally and most importantly, C++ is the language of choice for embedded systems. Even though the majority of embedded projects are written in C, companies are increasingly transitioning to C++ due to its better architectural implementation, evolving standards and richer functions. Let’s take a closer look at its powerful capabilities.

What makes C++ programming the top choice for embedded software?

Close to hardware

C++ is closer to hardware than most general-purpose programming languages, giving embedded developers more control over hardware resources and memory at the software level. We can directly access components such as sensors and actuators, while also building higher levels of abstraction, encapsulating implementation details of the hardware, operating systems, etc.

Rich C++ libraries and community

During almost four decades of its existence, C++ has gathered an enormous community and knowledge base. A prominent example is its famous Standard Template Library (STL) with built-in containers, hash maps, heaps and other features. Another famous library is Boost, which for many years has provided support for features not present in base C++. On top of that, there are numerous development tools, third-party components, open-source libraries and manuals that let us leverage what other embedded programmers have already produced to develop code quickly and efficiently.

Maturity

Over almost 40 years, open-source libraries and frameworks have already resolved the majority of software challenges that arose over time. We can benefit from an extensive knowledge base that would take decades to mature in other languages.

Speed

C++ is a perfect choice for applications that require low latency, fast execution and compilation. While there’s a common misconception that C++ produces bloat code, we can easily avoid it by disabling certain features (e.g. exceptions or runtime type information).

Constantly evolving

As we mentioned above, the ISO C++ committee releases a new language standard every 3 years. The most recent one is C++20 and C++23 will be released in December 2023. The standards do not just fix bugs but enhance functionalities, safety and ease of use that keep the language up-to-date.

Energy efficiency

According to research by Pereira et al. (2017), C++ is the third most energy-efficient programming language, after C and Rust. The study evaluated the performance of software languages using ten different problems. Although the findings might seem trivial, even the smallest efficiency gains are important for embedded systems where processing power, memory and battery life remain limited.

Memory management

C++ supports dynamic control management, which gives the programmer full flexibility to allocate and deallocate memory. The lack of a garbage collector consuming both memory and CPU cycles makes the language a particularly feasible choice for resource-constrained embedded devices.

Laptop with an illuminating screen

What about C++ programming safety concerns?

In 2022, the US National Security Agency (NSA) published a report advising organizations to switch from C/C++ to memory-safe languages. The report  uses the argument that memory safety issues account for a large portion of exploitable vulnerabilities in software. The claim has sparked fierce debates in the IT world, with companies such as Microsoft and Google announcing a transition from C++ to Rust.

But while more and more businesses are jumping on the bandwagon, we believe that simply switching to Rust isn’t a silver bullet solution. Here’s why:

Memory safety vs. safety

Firstly, the NSA report limited safety to just memory safety labeling C++ as generally “unsafe,” which led to many misconceptions in the industry questioning the language’s reliability. However, C++ is a completely type-safe and resource-safe language, and importantly, without performance loss.

C++ memory safety techniques and features

The report also pairs C++ with C, ignoring over 30 years of dramatic improvements in safety and its new standards. In fact, many memory safety issues in C++ can be easily avoided if engineers pay attention to industry best practices. Some straightforward examples include using smart pointers, avoiding using dynamic memory and heap, or following the RAII technique.

“Modern C++ since version 11-14 has smart pointers that protect memory leaks significantly better than ordinary pointers, which is a dramatic change compared to C and C++ 98. The unique_ptr retains the speed of a regular pointer and is completely safe to use. Software written this way is both fast and safe, you just need to be aware of the mechanism. It’s also perfectly integrated into the entire STL and Boost library.”

Mateusz Wańczyk, Embedded Software Engineer at Tronel

Memory is never entirely safe

As noted by the NSA itself, memory management is not entirely safe even in “memory-safe” languages. Developers can bring the same bad safety and security habits to Rust and you can still end up with memory issues and bugs.

Securing the native code

Lastly, even if businesses decide to transition to Rust, the switch won’t happen overnight. An enormous amount of C++ code will still need to be maintained. That’s why we need strategies to secure the native code. 

Open locker lying on a laptop keyboard illuminated in red and green

The NSA report did create some turmoil but also brought attention to issues that need attention. As C++ is constantly evolving, we may stay optimistic. A response statement co-authored by Bjarne Stroustrup outlining steps forward, reads “We now support the idea that the changes for safety need to be not just in tooling, but visible in the language/compiler, and library.” As such, we can expect some significant changes coming to C++ memory safety for the benefit of engineers and final clients.

Will Rust ditch C++?

C++ built the foundations of the computing industry and is still one of the most widely used programming languages with an array of applications. It remains a reliable, powerful and high-performing option for embedded systems.

Yes, C++ is complex. Yes, it is challenging. It takes longer to master than other programming languages. That’s why C++ projects require ambitious engineers who not only possess the right knowledge and experience but also are willing to learn its new standards and features to make the most of its capabilities.

The hype around Rust is gaining momentum but we believe that it has not been in production long enough to replace C++.  

But don’t just take our word for it. According to our LinkedIn poll, 72% of respondents think that C and C++ will retain their position on the embedded programming landscape.

Results of a LinkedIn poll asking: "What language will prevail in embedded systems development in 5 years?" The top answer is C++ with 45% of votes
Source: Tronel @ Linkedin

The programming industry is changing

It’s too early to assess if Rust will bring significant changes to the embedded industry, but we can be sure that other languages will continue to emerge in the future.

Gone are the days when two or three languages dominated the computing landscape. Currently, no language has a rating above 15% on the TIOBE Index. We seem to be drifting away from using a single dominant language to multiple languages to achieve development goals.  

Table with ten most popular programming languages according to TIOBE Index as of September 2023 with C++ on the third place
Ten most popular programming languages according to TIOBE Index as of September 2023

This proliferation reflects the growing complexity of modern software development needs. Programming languages are no longer one-size-fits-all tools. Instead, they’re precision instruments aimed to excel in particular niches.

For example, while C# and Java will be best for applications that operate at the highest layers of abstraction, Erlang and its successors will come down for servers. The trend towards specialization allows us to offer tailored solutions that enhance productivity and efficiency within certain domains.

As such, we can expect that newer languages will continue to emerge on the market, but C++ will stay right alongside.

Looking ahead

The programming landscape is changing and the winners are those who will be able to embrace it. And that includes both incorporating new languages and taking full advantage of new standards of well-established ones. C++ isn’t going away anytime soon.

Hungry for more embedded technology insights? Be sure to check out xBerry’s blog: From Rust to Riches: Will Embedded Applications Ditch C++ for a Brighter Future? Get ready to feel inspired and let’s continue the journey together!


By Monika Wozniak