EmbeddedRelated.com
Forums
Memfault Beyond the Launch

C vs C++ in Embedded Systems?

Started by Mayank Kaushik January 28, 2005
Hi,

How do C and C++ compare vis-a-vis Embedded Systems? And how about with
respect to any kind of hardware-level programming? Just asking out of
curiosity, so plz dont qualify your answer!

Regards
Mayank

In article <1106934623.176433.166230@z14g2000cwz.googlegroups.com>,
Mayank Kaushik <prehistorictoad2k@yahoo.com> writes
>Hi, > >How do C and C++ compare vis-a-vis Embedded Systems? And how about with >respect to any kind of hardware-level programming? Just asking out of >curiosity, so plz dont qualify your answer!
It's quite simple. There are no (or virtually no) C++ compilers for 8 bit systems. 16/32/64 bit systems do have C++ What do you mean how do they compare? In what sense? /\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\ \/\/\/\/\ Chris Hills Staffs England /\/\/\/\/\ /\/\/ chris@phaedsys.org www.phaedsys.org \/\/ \/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/\/
On 2005-01-28, Mayank Kaushik <prehistorictoad2k@yahoo.com> wrote:

> How do C and C++ compare vis-a-vis Embedded Systems?
IMO, nobody should write a line of source code for an embedded system unless they know ahead of time what code (more or less) the compiler is going to generate. That's possible with C. The C++ language is so unbelievably complex and "feature" ridden, that nobody ever quite knows what's going to happen when they write a line of code. IOW, I don't approve of C++ for embedde use. Or for non-embedded use, actually. -- Grant Edwards grante Yow! Not SENSUOUS... only at "FROLICSOME"... and in visi.com need of DENTAL WORK... in PAIN!!!
"Mayank Kaushik" <prehistorictoad2k@yahoo.com> wrote in message 
news:1106934623.176433.166230@z14g2000cwz.googlegroups.com...
> Hi, > > How do C and C++ compare vis-a-vis Embedded Systems? And how about with > respect to any kind of hardware-level programming? Just asking out of > curiosity, so plz dont qualify your answer! > > Regards > Mayank >
I prefer C++ when I have the resources because I can use the STL and have all the other benefits of object oriented programming. You can always drop back into C if you want to. However on Linux I find that using C++ libraries makes programs 10 times bigger and compile 10 times slower. On small processors you don't have the option of C++. Peter
[original question pasted to the bottom for humor value]

"Grant Edwards" <grante@visi.com> wrote in message
news:41fa8e85$0$11509$a1866201@visi.com...
> > IMO, nobody should write a line of source code for an embedded > system unless they know ahead of time what code (more or less) > the compiler is going to generate. That's possible with C.
I agree 1000% (not a typo)
> The C++ language is so unbelievably complex and "feature" > ridden, that nobody ever quite knows what's going to happen > when they write a line of code.
Not true for me, but I don't use exceptions, templates, or iostreams. Heck, I don't even use heap if I can avoid it. The compiler (MSC++ 8.0 that came with MSVC 1.5 or there abouts) has been generating perfectly fine code for my 80C186 projects since 1995. But, that's just my experience. Everyone should use the toolset that works for them.
> On 2005-01-28, Mayank Kaushik <prehistorictoad2k@yahoo.com> wrote: > > > How do C and C++ compare vis-a-vis Embedded Systems?
I guess it *has* been at least a month since the last C vs C++ religious battles were waged here ;-) Is it just me, or does the email address "prehistoric toad 2k" sound like a troll? Bob
"Mayank Kaushik" <prehistorictoad2k@yahoo.com> wrote in message
news:1106934623.176433.166230@z14g2000cwz.googlegroups.com...
> Hi, > > How do C and C++ compare vis-a-vis Embedded Systems? And how about with > respect to any kind of hardware-level programming? Just asking out of > curiosity, so plz dont qualify your answer! > > Regards > Mayank >
C++ is a very sharp knife.
"Chris Hills" <chris@phaedsys.org> schreef in bericht
news:UNSnQKCzNo+BFAck@phaedsys.demon.co.uk...
> In article <1106934623.176433.166230@z14g2000cwz.googlegroups.com>, > Mayank Kaushik <prehistorictoad2k@yahoo.com> writes > >Hi, > > > >How do C and C++ compare vis-a-vis Embedded Systems? And how about with > >respect to any kind of hardware-level programming? Just asking out of > >curiosity, so plz dont qualify your answer! > > It's quite simple. There are no (or virtually no) C++ compilers for 8 > bit systems.
Not true! Take a peek at e.g. the Metrowerks CodeWarrior for HC08. BCNU Waldemar
"Mayank Kaushik" <prehistorictoad2k@yahoo.com> wrote in message 
news:1106934623.176433.166230@z14g2000cwz.googlegroups.com...
> Hi, > > How do C and C++ compare vis-a-vis Embedded Systems? And how about with > respect to any kind of hardware-level programming? Just asking out of > curiosity, so plz dont qualify your answer! > > Regards > Mayank >
C is more popular and more 'streamlined' (doesn't contain anything fancy) for smaller embedded systems. It can't be used for true object-oriented programming, but who needs OO on 8-bit processors ? Designing something like an RTOS requires OO, presumably, but an RTOS on an 8-bit processor ? Isn't that a bit like overshoot ? C++ is sometimes so loaded with different features and possibilities that it's just too huge for it's task. I prefer using C or assembly on an embedded system, and C++ when I'm designing applications for Windows or Linux. The only place where I need C++ on an embedded system is when I'm doing something for Windows Embedded or Linux Embedded. Like they say, "C++ makes it harder to shoot yourself in the foot, but when you do, you'll blow off the entire leg." Me, for one, am not interested in losing my foot when creating a simple frequency doubler for a Intel MCS96.. I'll just use C or assembly :D -Antti Keskinen
Grant Edwards wrote:

> On 2005-01-28, Mayank Kaushik <prehistorictoad2k@yahoo.com> wrote: > > >>How do C and C++ compare vis-a-vis Embedded Systems? > > > IMO, nobody should write a line of source code for an embedded > system unless they know ahead of time what code (more or less) > the compiler is going to generate. That's possible with C. > > The C++ language is so unbelievably complex and "feature" > ridden, that nobody ever quite knows what's going to happen > when they write a line of code. > > IOW, I don't approve of C++ for embedde use. Or for > non-embedded use, actually. >
Yes on paying attention to compiler output, no on C++ paranoia. As long as you avoid the really dippy language features and don't touch the C++ standard libraries with a 10-foot pole you'll do OK. Most compilers only pull in the extra baggage for a feature if you use it, so if you don't use the feature you're OK. And if you have a compiler that _does_ pull in the baggage then don't use it! I've been programming headless embedded systems in C++ for eight years now with great success. It enables faster programming on large applications, easier factoring of jobs among a team, and significantly easier design-for-reuse. It _does_ require careful programming, and an idiot at the keyboard will bring you down faster than anything, but you get that with any programming language. -- Tim Wescott Wescott Design Services http://www.wescottdesign.com
I have used C++ in a number of embedded projects.  This was back in the
mid-90s, and we had some rules to stop programmers going overboard with
language features to the extent that others could not determine what a
line of code did by simply reading it.

1. Don't use exceptions or templates - these are "heavy weight"
features which take a lot of code and/or runtime.
2. Don't use operator overloading:  a = b - c should not invoke 100,000
lines of code.
3. Don't use polymorphic functions.  With implicit type conversion it's
hard to figure out which out of the 3 versions of a function will be
called, given a specific line of code.

We mainly used C++ for its class hierarchies, virtual functions, etc.
for all the normal object-oriented advantages, but we restricted our
use to make sure we never lost track of what was a function call vs.
what was an instrinsic machine operation.

We were very successful.  Bringing C programmers up to speed with this
"reduced" C++ was very fast, and we made sure that C++ experts did not
write code that was too clever for the masses.


I guess the bottom line is that, like all language choices, "it
depends".  It depends on tool support, personal preference, even
political will.

C++ can do everything C can do... only with uglier (mangled) function
names :-)


Memfault Beyond the Launch