Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

C vs C++ in embedded Linux

I am developing an application for embedded Linux (ARM). It will execute 500 times/sec, therefore speed is important. I would prefer to use C++ but I am afraid it will be slower than C even if I avoid fancy features like virtual functions. Is there a reason to use C or it's just as fine to write in C++?

like image 927
Gregory Khrapunovich Avatar asked Feb 28 '11 19:02

Gregory Khrapunovich


People also ask

Is Embedded C and C difference?

It is a structural and general purpose programming language used by the developers to build desktop-based applications. Embedded C is generally used to develop microcontroller-based applications. C is a high-level programming language. Embedded C is just the extension variant of the C language.

Which is better C or Embedded C?

C is generally used for desktop computers, while embedded C is for microcontroller based applications. C can use the resources of a desktop PC like memory, OS, etc. While, embedded C has to use with the limited resources, such as RAM, ROM, I/Os on an embedded processor.

What is Embedded C in Linux?

Embedded C programming Basics Use following command to get a list of installed compilers. dpkg --list | grep compiler. The GNU Compiler Collection (GCC) is a compiler system produced by the GNU Project supporting various programming languages. It comes inbuilt in most of the open source Operating Systems.

Is C used in embedded systems?

Although not originally designed for embedded software development, the C language allows a range of programming styles from high-level application code down to direct low-level manipulation of hardware registers. As a result, C has become the most popular programming language for embedded systems today.


2 Answers

C++ in general suffers no run time penalty over C - (except for a few things like RTTI).

Except in a few odd circumstances the compiler should be able to determine which virtual function to call at compile time and so add no overhead.

Edit: Ok with such a variety of compilers, CPUs, runtime libs, OSes there are some features of C++ that might create slower code, there are some features that might create faster code.

But can we all agree that C++ isn't automatically excluded from embedded use anymore ?

like image 111
Martin Beckett Avatar answered Sep 22 '22 01:09

Martin Beckett


In C++ you have things like template metaprogramming that resolve in compile time several situations where C or any other procedural programming language would have to do in runtime.

I should say more. Template metaprogramming and some class inheritance tricks are really amazing. It can save you a lot of processing time that you'd spend otherwise by "ifing" and "switching".

This means that C++ can be actually faster than C if well conducted.

Obviously you can program "in C" using C++ and you'd have no penalty at all. If you're not too fond of C++ I'd advise you to do a "C on C++" or "C with C++ extensions" just to take vantage of C++ improvements, but the real advantage you'll have is by programming the C++ way. There you will see that C++ is, good part of times, or faster or cleaner than C or, at least as fast as.

Have no fear. Face C++. After the stdc++ (against libc) there will be almost no overhead in code size. If your application is from median to high in size, it will be diluted.

I use C++ from simple 8-bit ATmega to Marvell's ARM9, passing through AVR32 UC3 and Cortex-M3 and always find it profitable.

If you need specific advise in a given situation, feel free to ask.

like image 37
j4x Avatar answered Sep 20 '22 01:09

j4x