473,378 Members | 1,138 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,378 software developers and data experts.

Coding embedded systems with C

I heard C is better for programming embedded systems than C++? Is this
true, if so why?

Nov 15 '05 #1
5 1835
James wrote on 26/07/05 :
I heard C is better for programming embedded systems than C++? Is this
true, if so why?


The fact is that sometimes, there is no choice. The only compiler
available for a given platform is a C-compiler. Period.

That said, if there was a choice, both of these languages could fit.
--
Emmanuel
The C-FAQ: http://www.eskimo.com/~scs/C-faq/faq.html
The C-library: http://www.dinkumware.com/refxc.html

"Clearly your code does not meet the original spec."
"You are sentenced to 30 lashes with a wet noodle."
-- Jerry Coffin in a.l.c.c++
Nov 15 '05 #2
In article <11*********************@g47g2000cwa.googlegroups. com>,
James <si******@gmail.com> wrote:
I heard C is better for programming embedded systems than C++? Is this
true, if so why?


It depends. C tends to be more conservative in its use of
memory. There are fair number of meaningful C programs that can be
written without using dynamic memory allocation (e.g., malloc).
When you get to C++ you have to be fairly careful to use
only static objects (whose memory can be allocated at compile time,
in theory) instead of the normal dynamic objects (which tend
to use dynamic memory behind the scenes.)

Also, when creating a C++ object, the work that will be involved
in running the constructors is less easily analyzable, unless
one takes care to use simplified objects and is relatively rigerous
in how one invokes the constructors. One can certainly do such things
in C++, but C++'s OOP (Object Oriented Programming) is based around
the idea that one shouldn't worry overly much about glue code and
glue objects, that one should write natural and reusable code and
let the compiler take care of conversion details.

Then there is the factor that the mandatory C++ library is *much*
bigger and more complex than the mandatory C library. Certainly
a good linker would not link in library code that wasn't needed,
but with the emphasis on templating and object reuse, the -tendancy-
in the C++ library is for high level interfaces to end up invoking
a wide variety of lower level interfaces, whereas in C there is
more of a -tendancy- to write just what is needed for the higher
level interface at hand. Of course, -tendancies- are not "proof",
so Your Milage May Vary.

With any given C++ implementation, one thing I would wonder about
is whether, when a particular object constructor is used, all the
code for the other constructors for that object type are also pulled
in -- or whether the implementation is thorough enough to do a
complete enough transitive linking to be able to prune out unused
constructor code. That would be a Quality of Implementation issue,
and could vary between releases -- and where it was present,
minor code changes could result in fairly large differences in
the code footprint.
These having been said, I'm sure there are also benefits for
using C++ in some embedded environments -- for example, when
one is writing a controller for a nuclear reactor, one would
prefer to -know- that string overflows are not possible because
one has consistantly used a length-checking "string" class instead
of char[] arrays that require more programmer effort.
--
Feep if you love VT-52's.
Nov 15 '05 #3
"Emmanuel Delahaye" <em***@YOURBRAnoos.fr> wrote in message
news:mn***********************@YOURBRAnoos.fr...
James wrote on 26/07/05 :
I heard C is better for programming embedded systems than C++? Is this
true, if so why?


The fact is that sometimes, there is no choice. The only compiler
available for a given platform is a C-compiler. Period.

That said, if there was a choice, both of these languages could fit.


C++ can be quite an overhead if used at full (inheritance with vtables,
worse if long chain of descendents; memory allocation here and there if you
create objects, etc). Actually, albeit different from what you want, this
article can be helpful to see more on C++ specifics for coding kernel,
drivers and other low level system things:
"C++ for Kernel Mode Drivers: Pros and Cons" may be useful:
<http://www.microsoft.com/whdc/driver/kernel/KMcode.mspx>

HTH
Alex
Nov 15 '05 #4
James wrote:
I heard C is better for programming embedded systems than C++? Is this
true, if so why?


In addition to what the others have said, I must argue that many (myself
included) still find it necessary to compile their code into Assembly.
That being said, I find it *much* easier to follow C code that's been
compiled into Assembly. I went nutty trying to follow what my C++
compiler optimizer had done with my C++ code. The Assembly output from
my C code, on the other hand, is commonly nearly identical to the C
code. That's reason enough for me ;-).

--
Sean
Nov 15 '05 #5

James wrote:
I heard C is better for programming embedded systems than C++? Is this
true, if so why?

That kind of depends on what you mean by "embedded".

Are you short on ROM/RAM - such as having to fit in to a 64kB address
space? Then some C++ features will be unavailable. You can probably
forget about RTTI, exceptions and STL. Dynamic memory will be a very
tight squeeze to fit in. Much of C++ will work, though. I built a
simple RTOS in C++ a while back; on a Renesas H8, the kernel and two
empty tasks weighed in at 5kB of code. Turning on exceptions swelled
the code to 300kB, due to the mass of library functions that were
linked in.

However, a cellphone with memory sizes in the 16MB range is still an
embedded system - and there all the above features can be included.

Do you have special realtime requirements, such as having to meet a
hard deadline? Many people will tell you to be wary of exceptions in
such circumstances, since the timing of stack unwinding can be hard to
predict - at least in the parts of your code that needs to meet those
deadlines; in my experience, only a very small part of the code in a
realtime system is subject to the realtime constraints.

Anyway, the bottom line is that C++ can be considered a superset of C.
For any given project, examine your constraints. Use the extra C++
features that fit within those constraints, be they memory or timing
related. Skip the C++ features that don't fit.

Nov 15 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

12
by: Bill Hanna | last post by:
C is inadequate for embedded systems for the following reasons: 1) Direct addressing of memory mapped I/O is not supported simply and easily. You have to find work-arounds that are compiler...
20
by: mohd hisham | last post by:
Write a C program to construct a queue of integers and to perform the following operations on it: a. insert b. delete c. display The program should print appropriate messages for stack overflow...
49
by: Alex Vinokur | last post by:
Are there any restrictions/problems for use of C++ STL in development in embedded systems? In particular: * Does STL require too much space/memory? * Is 'implementation of STL...
50
by: Konrad Palczynski | last post by:
I am looking for tool to validate conformity to defined coding standard. I have already found Parasoft's C++ Test, but it is quite expensive. Is there any Open Source alternative? I do not need...
20
by: Jack | last post by:
Is there a Python packaging that is specifically for embedded systems? ie, very small and configurable so the user gets to select what modules to install? For Linux-based embedded systems in...
0
by: YellowFin Announcements | last post by:
Whitepaper: "Yellowfin Reporting" enables Embedded Business Intelligence -------------------------------------------------------------------------------- Embedded reports are a standard...
7
by: Robert Seacord | last post by:
The CERT/CC has just deployed a new web site dedicated to developing secure coding standards for the C programming language, C++, and eventually other programming language. We have already...
20
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
There are a few guarantees I exploit in the C Standard. For instance, I might write (unsigned)-1 to get the maximum value for an unsigned integer. Also, I might rely on things such as: ...
30
by: =?ISO-8859-1?Q?Tom=E1s_=D3_h=C9ilidhe?= | last post by:
Let's say we had a simple function for returning the amount of days in a month: unsigned DaysInMonth(unsigned const month) { switch (month) { case 8: case 3: case 5:
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
0
by: ryjfgjl | last post by:
In our work, we often need to import Excel data into databases (such as MySQL, SQL Server, Oracle) for data analysis and processing. Usually, we use database tools like Navicat or the Excel import...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.