EmbeddedRelated.com
Forums

C or EC++ should I use?

Started by bigningbo February 3, 2004
Hi,

We have started a new project which we will use IAR C/EC++ compiler 
for MSP430. I wonder which langugue is best to use, C or EC++? If I 
choice EC++, how is the code size and the performance compare to C? 
Could you please give me some comments/advices? Thanks!

Best Regards,
BigNingbo



Beginning Microcontrollers with the MSP430

> We have started a new project which we will use
IAR C/EC++ compiler
> for MSP430. I wonder which langugue is best to use, C or EC++? If I
> choice EC++, how is the code size and the performance compare to C?
> Could you please give me some comments/advices? Thanks!

Hi!

This is a tricky question to answer.  C++ (or EC++) versus C has
spawned a lot of disussions over the years, when it comes to general
programming -- however in that area C++ seems like the clear winner.

When it comes to embedded applications I believe that this is just a
matter of time before most non-trivial applications are written in C++
or EC++.  However there are a number of reasons why this has not yet
happend.  The main reason is that there are very few reference
applications out there, so there is no way to study and be inspred by
a well-designed C++ embedded application.  Also, the applications tend
to be very small, which means that the advantages you gain from C++ is
slightly less that in larger applications.

Over to the issue.  What are the advantages of using C++?

The main feature C++ it is possible to modulate applications using
"object orientation", a fancy term that means that you can define
objects -- things -- with a well-defined interface.

When it comes to embedded applications some concrete object
immediately spring to mind.  For example, it is possible to
encapsulate an I/O port and all code that controls it in one class.

Other examples taken from typical embedded systems are state-machines
where each state and/or event could be modulated in an object oriented
manner.

Also, C++ makes it easier to build up your private stock of resuable
modules.

In C people often use macros to write efficient code.  In C++ provides
a better mechanism: inlined functions and class methods.  The big
advantage is that it is possible for the compiler to perform
type-checking, something that could save you a lot of trouble.  The
same is true for defines symbols, in C++ it is possible to define
global constants with a type.

There are other neat features of C++, for example overloading where it
is possible to define functions with the same name but with different
types.  For example, instead of having -- say -- "send_alpha" and
"send_beta" -- you could name them both "send".  This makes
the code
easier to read and you deoesn't have to recompile every time you
picked the wrong one.

Another feature of C++ is the ability to override plain operators like
"+".  If you have a class that should act like a normal datatype --
for example a fixed point class -- then you could define a "fix" class
and then use the numbers just like plain "int":s, for example:

  #include "fix.h"

  fix doCalc(fix a, fix b, fix c)
  {
    if (a > b)
    { return a + c; }
    else
    { return (a - b) * c; }
  }

In the above example authors of the class "fix" has provided operators
for ">", "+", "-", and "*".


Of course, there are some downsides to using C++.  It requires high
level of understanding of the language, for example one should know
the basics on how copy contructors versus assigment operators work in
order to be able to write classes whose objects could be assigned to
each other.

When it comes to embedded systems there are some additional issues
that must be considered.  One thing that emebdded systems are bad at
handling is the heap -- if a lot of allocation and deallocation are
performed then the heap could become fragmented, which is fatal to a
system that should live for a long time.  This means that embedded
systems should try to void using the plain "operator new" system of
C++.  Instead objects should be placed on the stack or in global
memory, or you can override the "new" operator and provide a a so
called "pool" of equally sized block which doesn't have the
problem
with fragmentation.


So, how should you avoid using the pitfalls of C++, mainly large code?
By using EC++ (embedded C++) you have already avoided most of the
problems.  EC++ simply doesn't support some of the features that would
silently add a lot of code like run-time type information and
exceptions.

EC++ also comes with a more slim-lined library.  The library in
"real"
C++ -- for example the string class string -- is written is a generic
faishon so that it could be a string of anything.  In EC++ it is a
string of char:s, period.

    -- Anders Lindgren, IAR Systems
-- 
Disclaimer: Opinions expressed in this posting are strictly my own and
not necessarily those of my employer.

Hi,

Which msp430 are you going to use? I think it is almost impossible
to use C++ with a 256 bytes RAM microcontroller. However ... maybe a
"hello world" application can be build with it ;-)

Sipke


>>> "bigningbo"
<al.lu@al.l...> 02/03 3:59 PM >>>
Hi,

We have started a new project which we will use IAR C/EC++ compiler 
for MSP430. I wonder which langugue is best to use, C or EC++? If I 
choice EC++, how is the code size and the performance compare to C? 
Could you please give me some comments/advices? Thanks!

Best Regards,
BigNingbo



. 

 

 




Actually, you shouldn't do a "Hello World" application in
C++. You 
should create a "Hello" class, that you could reuse on Earth, the 
Moon, and Mars...


Michel

--- In msp430@msp4..., "Sipke de Leeuw" <sleeuw@v...> wrote:
> Hi,
> 
> Which msp430 are you going to use? I think it is almost impossible
> to use C++ with a 256 bytes RAM microcontroller. However ... maybe a
> "hello world" application can be build with it ;-)
> 
> Sipke
> 
> 
> >>> "bigningbo" <al.lu@t...> 02/03 3:59 PM
>>>
> Hi,
> 
> We have started a new project which we will use IAR C/EC++ compiler 
> for MSP430. I wonder which langugue is best to use, C or EC++? If I 
> choice EC++, how is the code size and the performance compare to C? 
> Could you please give me some comments/advices? Thanks!
> 
> Best Regards,
> BigNingbo
> 
> 
> 
> . 
> 
>  
> 
> 


Hi,

Thanks very much for your valuable comments! In our project we have 
decided to use MSP430F135 with 512B RAM. It is too bad that EC++ 
limits by memory. I really want to use the features EC++ have. To 
embedded systems you don't need C++, can I say that? 

Best Regards,
BigNingbo


"bigningbo" <al.lu@al.l...> writes:

> Thanks very much for your valuable comments! In
our project we have
> decided to use MSP430F135 with 512B RAM. It is too bad that EC++
> limits by memory. I really want to use the features EC++ have. To
> embedded systems you don't need C++, can I say that?

You can write an EC++ program that doesn't consume more memory then a
classical C program, as long as you understand how the underlying
mechanisms work and avoid the expensive ones.

    -- Anders
-- 
Disclaimer: Opinions expressed in this posting are strictly my own and
not necessarily those of my employer.

Maybe that was the reason of the problems with the little Mars-car :)
But seriously, I agree that C++ can be used in small embedded systems
but I don't recommend it. I saw lot of commercial projects fail by trying
to use C++, wthout enough C++ knowledge.
And ... object oriented modules can be implented in C to.

>>> "michelqv"
<michel@mich...> 02/04 3:22 PM >>>
Actually, you shouldn't do a "Hello World" application in
C++. You 
should create a "Hello" class, that you could reuse on Earth, the 
Moon, and Mars...


Michel

--- In msp430@msp4..., "Sipke de Leeuw" <sleeuw@v...> wrote:
> Hi,
> 
> Which msp430 are you going to use? I think it is almost impossible
> to use C++ with a 256 bytes RAM microcontroller. However ... maybe a
> "hello world" application can be build with it ;-)
> 
> Sipke
> 
> 
> >>> "bigningbo" <al.lu@t...> 02/03 3:59 PM
>>>
> Hi,
> 
> We have started a new project which we will use IAR C/EC++ compiler 
> for MSP430. I wonder which langugue is best to use, C or EC++? If I 
> choice EC++, how is the code size and the performance compare to C? 
> Could you please give me some comments/advices? Thanks!
> 
> Best Regards,
> BigNingbo
> 
> 
> 
> . 
> 
>  
> 
>  


. 

 

 




Sipke de Leeuw wrote:
> Maybe that was the reason of the problems with the
little Mars-car :)
> But seriously, I agree that C++ can be used in small embedded systems
> but I don't recommend it. I saw lot of commercial projects fail by
trying
> to use C++, wthout enough C++ knowledge.
> And ... object oriented modules can be implented in C to.

Or assembler for that matter.

Al


> 
> 
>>>>"michelqv" <michel@mich...> 02/04 3:22 PM
>>>
> 
> Actually, you shouldn't do a "Hello World" application in
C++. You 
> should create a "Hello" class, that you could reuse on Earth, the

> Moon, and Mars...
> 
> 
> Michel
> 
> --- In msp430@msp4..., "Sipke de Leeuw" <sleeuw@v...>
wrote:
> 
>>Hi,
>>
>>Which msp430 are you going to use? I think it is almost impossible
>>to use C++ with a 256 bytes RAM microcontroller. However ... maybe a
>>"hello world" application can be build with it ;-)
>>
>>Sipke
>>
>>
>>
>>>>>"bigningbo" <al.lu@t...> 02/03 3:59 PM
>>>
>>
>>Hi,
>>
>>We have started a new project which we will use IAR C/EC++ compiler 
>>for MSP430. I wonder which langugue is best to use, C or EC++? If I 
>>choice EC++, how is the code size and the performance compare to C? 
>>Could you please give me some comments/advices? Thanks!
>>
>>Best Regards,
>>BigNingbo
>>
>>
>>
>>. 
>>
>> 
>>
>> 
> 
> 
> 
> . 
> 
>  
> 
>  
> 
> 
> 
> 
> .
> 
>  
> 
>  
> 
> 
> 


If you have to ask, you probably want to use C.  Using plain C will 
make it easier to migrate to another compiler when you get tired of 
IAR as well.

--- In msp430@msp4..., "bigningbo" <al.lu@t...> wrote:
> Hi,
> 
> We have started a new project which we will use IAR C/EC++ compiler 
> for MSP430. I wonder which langugue is best to use, C or EC++? If I 
> choice EC++, how is the code size and the performance compare to C? 
> Could you please give me some comments/advices? Thanks!
> 
> Best Regards,
> BigNingbo