Limitations of C++ in Embedded Systems


Working on Embedded Systems can be exciting but also tasking. As most know the main language used for embedded solutions is the C programming language. The are many reasons for this:

  1. Close to the hardware; concepts like memory address, pointer, jump etc. have not been abstracted away.
  2. No garbage collection; to prevent slow down during runtime.
  3. Lightweight Standard libraries; this ensures compiled binaries are small in size since embedded systems usually deal with limited memory
  4. Open source extensible and flexible toolchain like gcc and clang.
  5. many more.

Now we come to C++, a superset of C with new and extended features like OOP (Object Oriented Programming), Templates, Generics, and a useful collection of standard libraries. For application running on large systems like server, desktops or mobile this is a bless. But for limited embedded systems this can become a problem.

  1. OOP might be overkill for simple low level application. Making application complicated.
  2. Some C++ features like templates affect performance of application.
  3. Extensive usage of STL can lead to more larger app.
  4. Mixing C++ and C code can be erroneous as both languages treat certain types differently.
  5. C and C++ object code (assembly code) are different and need to explicitly differentiated before compiling.
// A Simple C calculator
#include <stdio.h>

int arithmetics(int a, int b, char op){
    switch (op)
    {
    case '+':
        return a+b;
        break;
    case '*':
        return a*b;
        break;
    case '-':
        return a-b;
        break;
    case '/':
        return a/b;
        break;
    
    default:
        break;
    }

    return 0;
}

int main(){
    printf("1 + 3=%d\n",arithmetics(1,3,'+'));
    printf("1 * 3=%d\n",arithmetics(1,3,'*'));
    printf("1 - 3=%d\n",arithmetics(1,3,'-'));
    printf("3 / 2=%d\n",arithmetics(3,2,'/'));
} 

Code 1.0: A Simple C Calculator

// A simple C++ Calculator
#include <iostream>

using namespace std;
class arithmetics
{
private:
    /* data */
    int a;
    int b;
public:
    arithmetics(int a, int b);
    ~arithmetics();
    int add();
    int  sub();
    int mul();
    int div();
};

arithmetics::arithmetics(int a, int b)
{
    this->a=a;
    this->b=b;
}

arithmetics::~arithmetics()
{
}

int arithmetics::add(){
    return a+b;
}

int arithmetics::mul(){
    return a*b;
}
int arithmetics::div(){
    return a/b;
}
int arithmetics::sub(){
    return a-b;
}
    

int main(){
    arithmetics a(6,2);
    cout<<"6 + 2="<<a.add()<<"\n";
    cout<<"6 * 2="<<a.mul()<<"\n";
    cout<<"6 - 2="<<a.sub()<<"\n";
    cout<<"6 / 2="<<a.div()<<"\n";
}

Code 1.1: A simple C++ Calculator using OOP

As seen above the C code is 31 lines long while the C++ code is 50 lines long. The C++ executable is also slightly larger even for simple code.

This is one of many difference between both languages, I will be going deeper in subsequent articles. Thanks for the read.


Leave a Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.