tags:

views:

297

answers:

2

Why is C++ a mid-level language? It can almost do everything and the worlds most widely used operating system is written in it.

[Note: SO C++ Info page quotes Wikipedia citing C++ The Complete Reference Third Edition, by Herbert Schildt,

It is regarded as a "middle-level" language, as it comprises a combination of both high-level and low-level language features.

— Potatoswatter]

+7  A: 

"mid lanuage" is not English, so one has to guess at what you mean. If you mean, "a language of intermediate level of abstraction", that's a fair assessment, although you are correct that, compared with most other language, it stretches to cover an uncomfortably wide range of abstraction levels.

Languages that provide (first and foremost) automatic, intrinsic garbage collection, are clearly working at a higher level of abstraction -- probably too far from the machine to be fully usable for the lowest-level parts of kernels, device drivers, &c, but far more convenient and free from memory-management bugs than languages that offer no such facility (or offer it only through "allegedly smart" pointers which typically do extremely rudimentary GC -- often as rudimentary as reference counting (!), worlds away from modern GC architectures). Almost all wide-spread languages deployed after C++ have included garbage collection as part of their intrinsic characteristics.

At the other end of the spectrum, machine-specific assembly languages are clearly working at a lower level of abstraction than C++ (and indeed it's common for C++ compilers to provide, as non-standard extensions, the ability to embed assembly language in order to empower advanced programmers to make use of its facilities where warranted).

As for "the most wide world user opareting (?) system", it's becoming a hard question to decide -- more and more gadgets of all kinds, for example, are based on a Linux core, from phones (think Android) to TVs &c (I found it fun a few months ago, on unpacking and installing my newly purchased flat-screen TV, Blu-Ray DVD player, and DVR, to find out that each of them came with a DVD holding some of the sources to its software... they had to, you see, since that software is based on Linux, of course!-).

At least in the case of Linux, it's easy to check that the language at its core is not C++, but C (which is really a very different language, of course -- for example, it doesn't even offer the "smart-ish pointers" on whose basis some would argue that C++ does have GC;-). Just try confusing C and C++ in proximity to Pike or Thompson... and if you do be ready to duck fast, just in case they can grab something heavy to throw at you!-) ((I suspect other of their ex-colleagues at Bell Labs, like Ritchie, might react similarly, but I don't know that first-hand, as I do with those who are my colleagues today;-)).

Similarly for the OSs powering Apple's extremely popular products (Macs, iPhones, iPads, ...): Objective C for application programming, but C itself for the kernel &c, that is, at the very core (while Apple's OSs, per se, are not open-source, the traces of BSD and Mach in them are still quite clear and visible). I believe most other phones (Blackberries, Nokia's stuff both Symbian and Maemo, Windows Mobile's current offerings) also rely on C at the core, not C++ (this can easily be checked for the open-source ones, not so easily for the closed-source ones, but, again, the hints all point that way).

If you're thinking of Windows, I'm pretty sure (until the time of Win 2000 included at least, based on recruiting attempts Microsoft directed at me in that timeframe) the kernel was also based on C, not C++ (I don't know if it was entirely rewritten for XP, Vista, Windows 7 -- it would be astonishing, but, of course, that doesn't make it impossible;-).

Overall, it seems to me the dominating language for the cores of all widespread OS's is most likely to be C, not C++. No intrinsic reason at least a reasonable subset of C++ could not be used instead (I can program in C as well as in C++, but boy do I miss templates when I do!-), but there just doesn't seem to be enough "effective demand" to make it happen!-)

Alex Martelli
http://opensource.apple.com/source/ Darwin is a runnable OS in itself, and it's mostly C. Drivers are written in a stripped-down C++ dialect designed to act as an accelerated objective-C (NextStep used ObjC drivers).
Potatoswatter
As I can't answer ATM, I'll add a little here. With a low level language, you work almost directly with the machine. It's tedious and long-winded, but you can do anything the machine can (and will let you) do. With a high level language, you are working with abstractions that are easier/safer/portable/more productive. That can insulate you from low-level features, but mostly it's a good thing. C and C++ are both substantially undefined - ie some subset is high-level and portable, but some subset depends on the compiler and the platform it targets. I guess that makes it mid-level. cont...
Steve314
Anyway, if a language is Turing complete and has the needed I/O features, you can write any program using it irrespective of whether it is high, low or mid-level. What varies are things like efficiency, portablility, development time, maintainability etc. C++, by providing high and low level features, is a good middle-ground - though by being substantially undefined (and with optimizers getting more aggressive in exploiting undefinedness), it's getting to be a real [expletive deleted] to write cross-level code that works reliably and portably.
Steve314
Smart pointers in modern C++ are of course not providing GC, but a *superior* alternative ;) Anyway, nice to see this answer having grown showing that this can be sensibly answered while above the subjectiveness was discussed.
Georg Fritzsche
Alex Martelli
@Alex - It depends on your priorities, IMO, as I've said (or at least implied) in answers elsewhere. Using GC means not having reliable destructors, which means no RAII - no automatic cleanup of resources other than memory. BTW - one of my favorite GC strategies (one that was used for at least one Java implementation) is... reference counting. True, a very specific form of reference counting that you're not likely to see in a smart pointer implementation, but in principle (if you're willing to give up RAII) it could be done.
Steve314
@Steve, RAII can be obtained (in a _superior_ way, as it can reliably distinguish whether an exception is propagating and thus for example do a commit or rollback accordingly -- as my friend, colleague, and C++ maven Matt Austern freely admits!-) e.g. with Python's `with` statement and the attendand context managers (I believe C# currently has a similar construct, though I don't know enough modern C# to be certain!). Reference counting has bas characteristics for cache locality _and_ (differently from modern GC forms running on a dedicated thread) no guarantee of bounded work upon freeing.
Alex Martelli
@Alex - I'm aware of with - but not every resource has a single-function lifetime. Obviously C++ needs an explicit delete to get the ball rolling in such situations, but that delete is typically wrapped up in a standard container template (std::map, std::vector or whatever) anyway. In Python, discard an object from a container, and you may need explicit cleanups for non-memory resources that object owns - which tends not to be a problem in C++. It's rarely a big deal, but it happens from time to time. If an idiom could solve all cleanup problems, that would apply to ensuring delete in C++ too.
Steve314
An ex-Sony ex-colleague said the Sony Playstation OS is written in C++. Couldn't quickly verify via google. "Almost all wide-spread languages after C++... incorporate GC". So, they can't span to low level coding? The Boehm GC for C/C++ is famous - surely C++ enjoys a superset of Java/C# options, which hardly makes it lower level. What's more relevant to HL/ML/LL is operator overloading, multiple inheritance, virtual bases, templates, compile time meta-programming, generation (macros, limited), type safety. Think: boost spirit in C#...?
Tony
@Tony - the trouble with GC in C++ is that it's not a standard language feature, and is therefore a bit clunky. For example, since a GC cannot know what order to destruct objects in when they form reference cycles, it won't try. Reliable destructor calls are lost, and using a C++ GC library therefore breaks a lot of standard idioms and existing code.
Steve314
@Steve: a cyclic argument :-). GC always has issues with reference cycles. If you've coded for GC, then order of destruction shouldn't matter, and you would hope the GC would provide a switch to turn on cycle destruction in whatever abitrary order, though I can understand the safer default. More generally, the code that uses standard idioms will be using explicit deletes already - I deterministic destruction there is good. GC should only kick in for unreferenced heap. i.e. can't write RAII-style without delete, but can pick one or the other which is better than only supporting either.
Tony
@Tony - even if you're mixing GC and non-GC, assuming you're starting from scratch, I think you'll be better off using a GC language for the GC code. No library can give you a "with" construct, for example - C++ isn't designed as a GC language so you end up with the worst of both worlds.
Steve314
@Steve: what's a with construct? (I assume you don't mean the old Pascal style thing for convenient field access)
Tony
@Tony - no - it's what Alex mentioned in a comment above. It looks superficially similar, but AFAIK it's semantically unrelated. Exists in Python, C# and Java I think at a minimum. Basically, the with clause allocates a resource in a block-structured way, so when the block exits the resource is cleaned up automatically. In C++ terms, the closest thing would be a block with a local variable (representing the resource) declared at the top - exit the braces (even as a result of an exception) and the variables destructor handles the cleanup. See the Python manual for more.
Steve314
@Steve: Thanks for the insight. So client code can request some level of deterministic behaviour even from code that was written for lazy GC, but without having to trigger a full collection? Does sound beyond libraries. Never having wanted GC, I'm not sure how significant its lack is for those who do, but good to understand. Still, IMHO this is one edge-case in one issue amongst a dozen in the determination of whether C++ constitutes high level in comparison to Java or C#, and the bigger picture lies in other comparisons (e.g. Prolog, Miranda...).
Tony
@Alex: Next time i will make that smily **bold** ;) But... at least i now know i'll have to inspect pythons `with` statement later.
Georg Fritzsche
@Steve, I don't believe Java has a `with` statement like C# and Python (rather, you'd need to synthesize a similar behavior with `try`/`finally`).
Alex Martelli
@Alex - OK, my bad
Steve314
@Alex: C++ can [determine whether an exception is unwinding or not](http://codepad.org/mv51Yj1x).
Roger Pate
+2  A: 

Given the context I added to the question, which I suppose is why you asked that particular question, here, Herbert Scheidt said it is middle-level because of some kind of weird concept of taking the average of "high" and "low."

Computer languages tend to be complicated, and arranging them on any particular continuum tends to be useless. (One exception is grammar class, in which case C++ ranks towards the hairy end, being context-sensitive.)

C++ (using a recent compiler) is one of the few languages that will let you embed inline assembly language code into a first-class, strongly-typed "lambda" function. That pretty well solidifies its position in both the low-level and high-level categories.

I don't think many people would conclude that being both puts it in the middle, though.

Potatoswatter
Right here in stackoverflow, just click Questions and the "C++" button and you'll get this in the description of C++: "It is regarded as a "middle-level" language, as it comprises a combination of both high-level and low-level language features." You've put the case for how absurd this is nicely. +1 from me.
Tony
I like "hairy" as an attribute of a language. Would the other end of the spectrum be "bald"? :)
deceze
@deceze: Or "slick"… take your pick.
Potatoswatter