Hm... Why C instead of C++???
Okay, so I've been programming PHP for years, programmed Perl for a little while and even did some C++ a while back. Lately I've been freshening up on C++ and learning more about it though. My main question is though, why is C more common for operating systems than C++??? Is it really just for simplicity sake? Such as not including features you don't need? Or is there another reason for it?
|
Yes
|
from what I understand about Linux anyway is that it was originally developed on an i386 in C simply because g++ was not available to Linus when he wrote the kernel. The kernel was supposedly recompiled at some point using g++ and actually ran slower so they've just stuck with C. as far as other OS's not sure for there reasons. However there are OS's written in C++ although not very popular.
|
There is more than on reason. The first one is that the OS needs to provide the infrastructure for the applications. C runtime environment is small and simple. The one for C++ is much bigger and complicated. It would be also harder to debug at the system level. It also adds some (small, but still) overhead in times of the instructions executed.
The second argument is that there's the custom. You simply write OSes in C. |
To reiterate, or paraphrase, the lower the level is that your working with, the lower the language you should use. For example, devices drivers are usually written in assembly or C. Operating systems (and other kernel-mode software) are usually written in C. C is a subset of C++, so that C++ would be considered a "higher-level" language compared to C. The lower the level of programming language you use, (generally) the more power and control you have of the environment.
|
Hm... Never thought about that, just going over C++ is seems to have a lot of very useful features. Thanks guys, aside from here I never was able to find an actually good answer to that.
|
C was created to make Unix more maintainable. C++ was created for a higher level of abstraction so that more abstract designs could be implemented. A lot of C must be ignored to create an operating system, as would be the case with C++, because of the nature of linking. For this reason, a lot of higher-level things in C++ aren't available to someone trying to write a kernel. Function overloading and namespaces complicate linking in C++ quite a bit, which by themselves prevent taking full advantage of dynamic linking with C++. Because of its origins, the POSIX standard specifies a standard C interface; therefore, the vast majority of *nix programs are written in C, which makes it sort of a catch 22.
Kevin Barry |
There's an interesting entry in the Linux Kernel Mailing List FAQ about this subject.
This includes the following thoughtful explanation from Linus himself: [...] It sucks. Trust me - writing kernel code in C++ is a BLOODY STUPID IDEA. [...] |
Why on earth would you build the foundation of your system with duplo blocks? It is all about fine touch.
|
There's a (very brief) critique of C++ by a man who spent 5 years writing compilers for it:
http://fontforge.sourceforge.net/faqFS.html |
Quote:
C++ is designed in a way that makes translating it to C the logical thing to do. For example, it makes a lot of sense to take member functions, add an argument for this, and treat them like global functions. For virtual functions, it makes sense to create a static table of pointers to such functions for each class, then give each instantiation a pointer to the appropriate table. Destructors are implicitly called when a stack variable goes out of scope; therefore, the compiler implicitly inserts a call to the destructor at the point where an object would be popped off the stack. All of these things add hidden code that by definition isn't accessible (nor is it visible) to the program, which is a problem when you need control over everything that goes on. Kevin Barry |
In short, C++'s main difference is the object oriented approach to programming. This approach is useful in some situations, where data structures and processes can easily be abstracted to 'objects'. However, not all programming projects require this approach. Particularly for lower level OS programming, device drivers, etc.
For those that don't need that approach, C++ doesn't offer much new or useful. The structures it introduces can get in the way, especially when you need lower level control over the program, which is what you are looking for programming OS's. C programs never let the programmer stray too far from the nuts and bolts, and C code doesn't abstract what is happening in the code, the way that C++ does. |
Quote:
|
There's always the possibility that a large portion of the OS development 'crowd' simply prefers using C and assembly in general, just like many userspace developers prefer C over C++. Especially if we are refering to hobby projects.
|
Quote:
Add to this the history of UNIX that is hardly connected with the C language. And the availability of a C compiler on any hardware you can encounter, and in the embedded computing world it's a must... So a long list of advantages of C over _ANY_ other programming languages for the low-level programming world... |
All times are GMT -5. The time now is 07:09 PM. |