Author Topic: Which is better for embedded work: C or C++?  (Read 31524 times)

0 Members and 1 Guest are viewing this topic.

Offline Kremmen

  • Super Contributor
  • ***
  • Posts: 1283
  • Country: fi
Re: Which is better for embedded work: C or C++?
« Reply #125 on: July 07, 2013, 08:31:09 pm »

hi Kremmen

i apologize if i came across as speaking down on you or anyone here. point taken. i don't know everything,
but i do program professionally and it puts bread on my families table, and there are things that i do take seriously and that is programming. :)
Accepted. Just wanted you to remember that there are others who do it as their day job as well. Myself, i have started professional programming -78 with minicomputers and embedded systems using various assemblers, PL/M, Fortran and what have you. When Nokia Data began OO software development in the last half of the 80's we started with Ada and C++. Ada was later dropped but C++ remained until i left the company in 2000. After that it has been Java and C#, although these days i manage SW development, not doing actual programing for my paycheck any more. But C++ is still fun as a hobby. My opinion there must be clear: that it is no worse than C and far better in many respects. But i am willing to discuss the issue.

Nothing sings like a kilovolt.
Dr W. Bishop
 

Offline diyaudio

  • Frequent Contributor
  • **
  • Posts: 671
  • Country: za
Re: Which is better for embedded work: C or C++?
« Reply #126 on: July 07, 2013, 08:58:11 pm »

hi Kremmen

i apologize if i came across as speaking down on you or anyone here. point taken. i don't know everything,
but i do program professionally and it puts bread on my families table, and there are things that i do take seriously and that is programming. :)

Accepted. Just wanted you to remember that there are others who do it as their day job as well. Myself, i have started professional programming -78 with minicomputers and embedded systems using various assemblers, PL/M, Fortran and what have you. When Nokia Data began OO software development in the last half of the 80's we started with Ada and C++. Ada was later dropped but C++ remained until i left the company in 2000. After that it has been Java and C#, although these days i manage SW development, not doing actual programing for my paycheck any more. But C++ is still fun as a hobby. My opinion there must be clear: that it is no worse than C and far better in many respects. But i am willing to discuss the issue.
:-+

 

Offline WarSim

  • Frequent Contributor
  • **
  • Posts: 514
Re: Which is better for embedded work: C or C++?
« Reply #127 on: July 07, 2013, 10:22:01 pm »
Just to scare a few of us. 

Punch cards! 

Vacuums down!

I do amuse myself.  :) 
 

Offline C

  • Super Contributor
  • ***
  • Posts: 1345
  • Country: us
Re: Which is better for embedded work: C or C++?
« Reply #128 on: July 07, 2013, 11:38:49 pm »
WarSim,
That's the fancy stuff, try
paper tape
live hot patch code
and finger bone boot code (why waste money for a boot rom when it's expected to run 24:365)

C

 

Offline WarSim

  • Frequent Contributor
  • **
  • Posts: 514
Re: Which is better for embedded work: C or C++?
« Reply #129 on: July 08, 2013, 01:44:44 am »
Before my time thank goodness. 

For things like that I would have to refer to my fathers experience with the huge mechanical computers and the building sized vacume tube computers. 

Heard about them but I was too young to see any of them when they still worked. 

 

Offline cthree

  • Frequent Contributor
  • **
  • Posts: 258
  • Country: ca
Re: Which is better for embedded work: C or C++?
« Reply #130 on: July 08, 2013, 03:42:19 am »
I have been reading and enjoying this forum for some time now.
...
Normally I do not respond
...
I can not and will not say on what nor where
...
In short I know allot, and love learning much more.

NSA? Did I blow your cover? Am I in trouble?

[note to self: Book flight to Boliva ASAP]
 

Offline WarSim

  • Frequent Contributor
  • **
  • Posts: 514
Which is better for embedded work: C or C++?
« Reply #131 on: July 08, 2013, 03:54:46 am »
No I'm not in trouble yet.  :)

But you can stop guessing.  :)
 

Offline PuterGeek

  • Regular Contributor
  • *
  • Posts: 88
  • Country: us
    • SolutionsPLUS engineering
Re: Which is better for embedded work: C or C++?
« Reply #132 on: July 08, 2013, 05:20:07 am »
Just to bring us back on topic, which I assume to be bare metal (or RTOS at most), other than what has already been posted, what objective information exists to help choose between C and C++?

If you are using an 8051 or small PIC you have no choice; if you don't want to write assembly code you are using C (although there is/was a C++ compiler for PIC as well as some Basic and Forth stuff). I'm not sure about any other 8-bit MCUs so chime in with your knowledge.

A C++ compiler is available for most 16-bit processors; chime in if you know of one lacking a compiler. I believe all 32-bit processors have C++ compilers; again, chime in if I'm wrong.

Changing the question slightly, is anyone aware of any reason to NOT use a C++ compiler for a C code?
I know the C++ compiler treats C code differently in same areas but I am not aware of any of these to be show stoppers.

-Joe
 

Offline WarSim

  • Frequent Contributor
  • **
  • Posts: 514
Which is better for embedded work: C or C++?
« Reply #133 on: July 08, 2013, 06:08:59 am »
So far for 8,16,32 I have found only C compilers I am willing to use. 
Found several poor Basic, Pascal, C++, C compilers. 
Resolved to just use MPLabX and PICs until there is another option to consider. 
The XC compilers are not brilliant but are usable. 

If you do discover a C++ compiler that you would consider good.
Please let me know, I would like to try one out. 

Sorry, you are my canary on this one. 
 

Offline Kremmen

  • Super Contributor
  • ***
  • Posts: 1283
  • Country: fi
Re: Which is better for embedded work: C or C++?
« Reply #134 on: July 08, 2013, 07:11:48 am »
Just to scare a few of us. 

Punch cards! 

Vacuums down!

I do amuse myself.  :) 

WarSim,
That's the fancy stuff, try
paper tape
live hot patch code
and finger bone boot code (why waste money for a boot rom when it's expected to run 24:365)

C



Been there, done that.

Can't recall the 4 digit product code of the IBM card punch unit any more, but we had one on those in the CS lab when i was studying. Fortunately i didn't have to use it much, but some anyway.

Was a time i remembered the Alpha 1200 boot loader code and could toggle it in from the front panel without checking paper. That was able to read in the OS loader from paper tape and that in turn loaded the OS from the huge 5MB Hawk hard disk. A marvel of miniaturization that, weighing just 40 kilos. The machine only got core memory, boot ROMs weren't invented yet.

Also there was a time i knew the opcodes of the 8085 processor by heart. That was invaluable when you had to do a hot patch out in the field with an UV lamp and keypad EEPROM programmer as your only development tools, and paper printouts as the only documentation.

So, not scared but not missing those days that much either.

Scarier words:

Drum memory!

Vacuum tubes!
Nothing sings like a kilovolt.
Dr W. Bishop
 

Offline Kremmen

  • Super Contributor
  • ***
  • Posts: 1283
  • Country: fi
Re: Which is better for embedded work: C or C++?
« Reply #135 on: July 08, 2013, 07:14:44 am »
So far for 8,16,32 I have found only C compilers I am willing to use. 
Found several poor Basic, Pascal, C++, C compilers. 
Resolved to just use MPLabX and PICs until there is another option to consider. 
The XC compilers are not brilliant but are usable. 

If you do discover a C++ compiler that you would consider good.
Please let me know, I would like to try one out. 

Sorry, you are my canary on this one.

GCC. Or does it have to be available for a specific processor?
Nothing sings like a kilovolt.
Dr W. Bishop
 

Online legacy

  • Super Contributor
  • ***
  • Posts: 4344
  • Country: ch
Re: Which is better for embedded work: C or C++?
« Reply #136 on: July 08, 2013, 01:48:56 pm »
Found several poor Basic, Pascal, C++, C compilers. 

me, too, i'd like to have a pretty pascal compiler for 68K
 

Offline WarSim

  • Frequent Contributor
  • **
  • Posts: 514
Which is better for embedded work: C or C++?
« Reply #137 on: July 08, 2013, 03:17:10 pm »
True GCC can be targeted to micro-controllers, Atmel for one. 
Actually I think I just forgot about that option. 

A while back when I was new to this micro controller thing I researched many options. 
And for a matrix of disqualifying reasons ended up only using microchips.   
I will skip the reasons, for brevity. 
Unfortunately some of the reasons of not using Atmel are still valid. 

So I would have to change my statement to a C++ compiler for MPLabX for now. 

 

Offline WarSim

  • Frequent Contributor
  • **
  • Posts: 514
Which is better for embedded work: C or C++?
« Reply #138 on: July 08, 2013, 03:28:38 pm »
Haven't had a project requiring a full microprocessor for a long time, even then I was able to mash it into a newly released micro controller. 
But as far as Pascal for processors there has been a few acceptable compilers.  Even CodeWarrior had a Pascal Compiler that had a bunch of noncompliant add-ons for their embedded processor options.  Although their embedded processor options should have been embedded OS Options.  No idea about today now. 
Delphi was PASCAL and it is still alive last I checked.  Wouldn't it be interesting if it could me targeted to a microprocessor.  Shudder.
 

Offline WarSim

  • Frequent Contributor
  • **
  • Posts: 514
Which is better for embedded work: C or C++?
« Reply #139 on: July 08, 2013, 03:35:34 pm »
<10 years ago I was still maintaining a computer that used core memory. 
Thankfully I personally never had to restring any, others I worked with had the pleasure though. 
I sure hope by now they have moved to some thing else only 20 years old by now. 
I'm not there anymore, so I don't know. 
 

Offline cthree

  • Frequent Contributor
  • **
  • Posts: 258
  • Country: ca
Re: Which is better for embedded work: C or C++?
« Reply #140 on: July 08, 2013, 03:42:34 pm »
Just to bring us back on topic, which I assume to be bare metal (or RTOS at most), other than what has already been posted, what objective information exists to help choose between C and C++?

There is none. It comes down to the quality of the tool chain at compiling code efficiently for size and how much your application would benefit from the additional abstraction offered by OO over straight C. The answer is, it depends. To say absolutely that one is always better is to commit to using the wrong tool at least some of the time for the sake of consistency all of the time. Use both and don't make that compromise.

That's how pretty much all successful embedded software works. C for the procedural and simple state machine and driver logic, assembly for fine tuning hardware performance, C++ or other OO (ObjectiveC, Java) for the application logic, and probably using the same tool chain at least for C rooted languages. Writing an ISR in C++ is batty. It's possible, just as it is possible to write an http server is assembly, but if you do then the guy who takes over your code someday is going to wonder wtf you were thinking and probably have to rewrite it so it makes so sense.

C++ to me makes sense when you are building an abstract framework which sits atop bare metal and is largely insulated from it, ie when the hardware is abstracted. For me, unless I'm going to abstract the hardware I prefer to code to bare metal in C. Some people like assembler for some things, I think the use for it is extremely narrow, like using binary instead of hexadecimal. Again, just me, everyone will have their own views and I would not imply they are wrong in any way, just different. I don't think the language has anything to do with writing clear, maintainable and reliable code. That comes down to the skill and discipline of the person writing it.
 

Offline cthree

  • Frequent Contributor
  • **
  • Posts: 258
  • Country: ca
Re: Which is better for embedded work: C or C++?
« Reply #141 on: July 08, 2013, 03:52:41 pm »
So far for 8,16,32 I have found only C compilers I am willing to use. 
Found several poor Basic, Pascal, C++, C compilers. 
Resolved to just use MPLabX and PICs until there is another option to consider. 
The XC compilers are not brilliant but are usable. 

If you do discover a C++ compiler that you would consider good.
Please let me know, I would like to try one out. 

Sorry, you are my canary on this one.

The defacto is IAR but their business model absolutely sucks ass, truly appalling. On the AVR platform their compiled code size is about 40% less than GCC's. if you need to cram big code into a little part it may be the only way though I'd rather use a bigger part, kinda depends on the situation. PiC has their own tools too but I've not used PIC so have nothing of merit to say about their tool chain.

As an indie, IAR quoted me $3600 if the 8 bit AVR toolkit and wanted the same again for every other chip, even the 32 bit AVR. Leaves a bad taste in my mouth. That's like 2 and a half mortgage payments for someone like me. They showed no interest in budging off that price either. They would rather have me walk away and not come back, for something which costs them absolutely zero dollars. Assholes.
 

Offline diyaudio

  • Frequent Contributor
  • **
  • Posts: 671
  • Country: za
Re: Which is better for embedded work: C or C++?
« Reply #142 on: July 08, 2013, 05:04:10 pm »
Quote
C++ to me makes sense when you are building an abstract framework which sits atop bare metal and is largely insulated from it, ie when the hardware is abstracted. For me, unless I'm going to abstract the hardware I prefer to code to bare metal in C. Some people like assembler for some things, I think the use for it is extremely narrow, like using binary instead of hexadecimal. Again, just me, everyone will have their own views and I would not imply they are wrong in any way, just different. I don't think the language has anything to do with writing clear, maintainable and reliable code. That comes down to the skill and discipline of the person writing it.

I think you hit the nail on this one, I support your view

I work at a higher level than C and doing C# most of the time im actually writing a lot of abstractions using patterns and  trying to apply them in large solutions where repeatable processes takes place and the need for abstartion is obvious (only when it applies and it’s tough to apply a pattern correctly to a problem i use it wisely as much as i can), however with that said vie been moving clients a lot and the worse thing is the miss use of a language the more options a developer has more abusive the solution becomes and the more muddy the intent is.
 

Offline WarSim

  • Frequent Contributor
  • **
  • Posts: 514
Which is better for embedded work: C or C++?
« Reply #143 on: July 08, 2013, 09:05:41 pm »
I believe we have been saying the same thing different ways, for the sake of the nay sayers. 
Makes me wonder did we come to consensus by attrition.  :)
 

Offline cthree

  • Frequent Contributor
  • **
  • Posts: 258
  • Country: ca
Re: Which is better for embedded work: C or C++?
« Reply #144 on: July 08, 2013, 11:01:19 pm »
Haha, don't worry, it won't last.  :-DD
 

Offline nctnico

  • Super Contributor
  • ***
  • Posts: 17971
  • Country: nl
    • NCT Developments
Re: Which is better for embedded work: C or C++?
« Reply #145 on: July 09, 2013, 11:11:19 am »
I'm also sceptic. Usually there is a difference of a few percent between good compilers. But each compiler has its own settings to produce compact code and people doing comparisons often know only one compiler well. Another interesting fact is that the license for every commercial compiler prohibits the user from publishing benchmark tests against other compilers.
« Last Edit: July 09, 2013, 11:15:09 am by nctnico »
There are small lies, big lies and then there is what is on the screen of your oscilloscope.
 

Offline AlfBaz

  • Super Contributor
  • ***
  • Posts: 2009
  • Country: au
Re: Which is better for embedded work: C or C++?
« Reply #146 on: July 09, 2013, 02:02:04 pm »
40%... That is a lot and if true what are the trade offs? Speed, code construct constraints (eg don't ever call a function)
 

Offline WarSim

  • Frequent Contributor
  • **
  • Posts: 514
Which is better for embedded work: C or C++?
« Reply #147 on: July 09, 2013, 06:17:16 pm »
40%... That is a lot and if true what are the trade offs? Speed, code construct constraints (eg don't ever call a function)

Unfortunately a unilateral trade off statement is not possible.  As I said before a compiler is a reductive algorithm.  And such algorithms are greatly effected by many things like the programming "style".  Hence why several other programers are talking about knowing the compiler well. 
A compiler needs to balance many factors and this balance varies between compilers.  Yes there are switches to turn some branches on and off but the core algorithm that allows those branches is still there.  For example the banner specs, small code, efficient code ,fast code, load respecting, algorithm respecting, etc.  No compiler can do all thing in every situation.

That is why you have a few general options:
1: Know and use a selection of compilers and use the best compiler you know for task at hand. 
2: Know a few compilers and many reductive algorithms and use the closest compiler and take over where you know the compiler will not make the required choices. 
3: Forget about using compilers a just use low level code. 

Newer programers usually start with one compiler and seek out a "better" compiler when the first fails them.  They naturally go down the option 1 path.  Many programers manage to stay with option one for a very long time.  But at some time a programer will be pushed into option 2.  Hopefully by this time the programer know allot about how their various compilers work to make the transition smoother.  And some opt for option 3 and they become "specialist".  These skill are needed in the profession, just not required for everyone. 

In short there is no "best" compiler, just like there is no best language.

just use the one that irritates you the least, and learn all you can about it.  So if you do decide to change to a new compiler/language for the task, you can make a more educated decision if and where to move to.  Programming is not plug and play. 
 

Offline HackedFridgeMagnet

  • Super Contributor
  • ***
  • Posts: 1938
  • Country: au
Re: Which is better for embedded work: C or C++?
« Reply #148 on: July 10, 2013, 07:47:32 am »
I too have heard a figure like 40% too, IIRC that figure was for C++ and a lot of that was to do with strings and/or other related libraries.
It would be interesting to know what the case is, and how it could be reduced.
 

Offline gnif

  • Administrator
  • *****
  • Posts: 1116
  • Country: au
Re: Which is better for embedded work: C or C++?
« Reply #149 on: July 10, 2013, 08:12:51 am »
The defacto is IAR but their business model absolutely sucks ass, truly appalling. On the AVR platform their compiled code size is about 40% less than GCC's. if you need to cram big code into a little part it may be the only way though I'd rather use a bigger part, kinda depends on the situation. PiC has their own tools too but I've not used PIC so have nothing of merit to say about their tool chain.

As an indie, IAR quoted me $3600 if the 8 bit AVR toolkit and wanted the same again for every other chip, even the 32 bit AVR. Leaves a bad taste in my mouth. That's like 2 and a half mortgage payments for someone like me. They showed no interest in budging off that price either. They would rather have me walk away and not come back, for something which costs them absolutely zero dollars. Assholes.

40%, is it really that much compared to GCC with -Os? Even so it doesn't seem worth the money. Upgrading to the next flash memory size up micro costs very little and you usually get more RAM/EEPROM too.

This is usually because of a poor linker script that is leaving behind all the additional sections normally found in an ELF, if you have a proper linker script then this size comes right down. If you use ulibc instead of libc it drops also. There are quite a few things you can do to create a tiny GCC binary.

There also are compile/linker options that ensure that dead code is pruned from the final binary that are not enabled by default in most dev environments, -Os can only do so much, you have to learn the compiler/linker if you really want to get the size down.
HostFission - Full Server Monitoring and Management Solutions.
https://hostfission.com/
https://twitter.com/HostFission

I volunteer my time to manage this server, if you would like to support this work I have a patreon here:
https://www.patreon.com/gnif
 


Share me

Digg  Facebook  SlashDot  Delicious  Technorati  Twitter  Google  Yahoo
Smf