Search code examples
c++cembeddedc89

Is there any reason to use C instead of C++ for embedded development?


Question

I have two compilers on my hardware C++ and C89

I'm thinking about using C++ with classes but without polymorphism (to avoid vtables). The main reasons I’d like to use C++ are:

  • I prefer to use “inline” functions instead of macro definitions.
  • I’d like to use namespaces as I prefixes clutter the code.
  • I see C++ a bit type safer mainly because of templates, and verbose casting.
  • I really like overloaded functions and constructors (used for automatic casting).

Do you see any reason to stick with C89 when developing for very limited hardware (4kb of RAM)?

Conclusion

Thank you for your answers, they were really helpful!

I thought the subject through and I will stick with C mainly because:

  1. It is easier to predict actual code in C and this is really important if you have only 4kb of ram.
  2. My team consists mainly of C developers, so advanced C++ features won't be frequently used.
  3. I've found a way to inline functions in my C compiler (C89).

It is hard to accept one answer as you provided so many good answers. Unfortunately I can't create a wiki and accept it, so I will choose one answer that made me think most.


Solution

  • Two reasons for using C over C++:

    1. For a lot of embedded processors, either there is no C++ compiler, or you have to pay extra for it.
    2. My experience is that a signficant proportion of embedded software engineers have little or no experience of C++ -- either because of (1), or because it tends not to be taught on electronic engineeering degrees -- and so it would be better to stick with what they know.

    Also, the original question, and a number of comments, mention the 4 Kb of RAM. For a typical embedded processor, the amount of RAM is (mostly) unrelated to the code size, as the code is stored, and run from, flash.

    Certainly, the amount of code storage space is something to bear in mind, but as new, more capacious, processors appear on the market, it's less of an issue than it used to be for all but the most cost-sensitive projects.

    On the use of a subset of C++ for use with embedded systems: there is now a MISRA C++ standard, which may be worth a look.

    EDIT: See also this question, which led to a debate about C vs C++ for embedded systems.