|
There is the myth that using C++ per se makes applications and libraries slow and bloated. Avoiding bloat is of special importance for embedded devices, which usually have limited amounts of CPU power and memory.
On www.linuxdevices.com I found a short article on this topic: C vs. C++ on Embedded Devices.
Let's get down to the statements being made there:
Some things the C++ developer must be aware of:
- run time type information and dynamic_cast<> add memory and performance overhead, so consider avoiding it
- creating a variable means that its ctor, i.e. a function will be called
- AFAIK exceptions still add some overhead using gcc, so it should be considered to avoid them
- a class without virtual functions is the same as a struct and some functions, memory wise, no additional overhead here, except the implicit this pointer for each member method.
- be aware how template code generation works. You can reduce this to a minimum if you want to.
And there are also advantages of using C++ for embedded projects:
- global/static objects will be initialized automatically, when the application starts since then their ctors will be called automatically
- init-functions can't be forgotten, since ctors are called automatically
- you have stricter type checking
- you can write more expressive and concise code, since the language gives you more power
- use const extensively to make semantics clearer
So, that's about it.
IOW I don't see any inherent disadvantages of C++ compared to C. It boils down to "Know your tools". The best tools are the ones you know best. If you know C++, you know where code is called and where memory is required and can avoid it if neccessary. Additionally you get all the advantages of C++
Alex
|
Comments
That's not all
OOP, even used sparingly, promotes encapsulation, which is a big plus over straight C. You mentioned being able to work faster because the language is more expressive, this also means that you as a programmer are cheaper in the long run. And the reduction in cost of development might well outway the extra cost of more memory...
God, Not This Again
If you want to write fast, efficient and native software but that software is too complex in structure (i.e. inherently object-oriented) and at too high a level then the only language you can consider is C++. You only need to look at KDE to see that wisdom. C++ isn't perfect, but it's the best fit there is for that problem scenario. As people are trying to do more and more in that kind of area, especially on embedded devices, C++ inevitably becomes a better fit for that kind of problem. Sounds like someone just doesn't want to go anywhere near C++ at all costs.
Why do people think there's such a debate going on around Gnome about higher level languages, technology and software??!! Arrrghhhh.
Why do people still harp on OO?
For some reason people still see OO as the thing that C++ tries to achieve. This puzzles me because the best code that I have ever seen comes from Boost; whose code is OO only if you squint your eyes -- or even better, gouge them out. It's not OO, not really functional (too many side-effects), but hugely pragmatic and beautifully designed. Also fast -- a decent compiler like gcc can optimize away the template instances, and later gcc (3.4 onwards) allows control over symbol visibility leading to code that's just as small as hand-coded C.