User: Password:
|
|
Subscribe / Log in / New account

C++ problem

C++ problem

Posted Mar 27, 2008 16:16 UTC (Thu) by ikm (subscriber, #493)
In reply to: whatever by elanthis
Parent article: Striking gold in binutils

In its essence, C++ just takes all the common concepts pretty much any complex C program uses
and makes it part of a language itself, offloading the associated implementation costs from
the shoulders of the programmer to the compiler itself. C people do the same C++ inheritance
(in form of one structure having another one as the first member, and sometimes even having a
macro to do upcasts, like e.g. offsetof macro in linux kernel), have the same member functions
(which are usual functions that take the associated structure pointer as the first parameter),
virtual functions (in form of the structures having pointers to functions, which in essence is
making vtables by hand), same constructor and destructor functions which are to be carefully
called every time the associated structure is created or deleted; instead of namespaces they
put long common prefixes.

If we drop here other C++'s concepts, namely templates and exceptions/rtti, we see that C++ is
just automating things that were to be done same way manually, saving people from doing
lengthy and error-prone routine they'd have to go into in bare C. The end result is supposed
to be exactly the same, since the compiler pretty much does exactly what a human would do, it
just does that very pragmatically and systematically.

I have to admit that when I first saw linux kernel sources, I was amazed to see very strict
C++ code carefully and duly implemented in bare C. From there, it was quite easy to feel the
actual problem C++ has. It is not performance at all, or compatibility problems, or whatever
else people usually like to attribute here — it's just the complexity and the associated
incorrect uses and/or abuses of power, resulting in bloat, inefficiencies and incorrect
runtime behavior. To program in C++, one have to actually understand all the background work
the compiler does, e.g., calling constructors/destructors in the right places and in the right
order, building vtables and using them, passsing 'this' pointer to member functions, adjusting
pointers when doing casts and so on. Templates and STL further complicate that by emitting
large amounts of code at compile time and making heavy use of dynamic memory at runtime, with
no apparent notice to the unwary programmer.

In essence, C++ has some cost other than just an increased compile time. Thing is, this cost
is not technical. If linux kernel were to be implemented in C++, it would have had much less
developers, or would otherwise be turned into dysfunctional bloatware in no time.

I myself would never start a project in bare C if I wouldn't have to. I even program for
microcontrollers in C++ if there is a capable compiler available (i.e. a gcc port). But I
understand that this approach does not always work for all people. It's simply just too
complex, and therefore harder to do right — many people would seem to sacrifice other
resources to make it simpler, and in case of C that would be doing everything manually, by
hand.


(Log in to post comments)


Copyright © 2017, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds