LinuxQuestions.org

LinuxQuestions.org (/questions/)
-   Programming (https://www.linuxquestions.org/questions/programming-9/)
-   -   Hm... Why C instead of C++??? (https://www.linuxquestions.org/questions/programming-9/hm-why-c-instead-of-c-784432/)

kc3 01-24-2010 01:33 PM

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?

carbonfiber 01-24-2010 01:45 PM

Yes

bret381 01-24-2010 01:51 PM

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.

Mara 01-24-2010 01:54 PM

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.

nadroj 01-24-2010 03:32 PM

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.

kc3 01-24-2010 08:39 PM

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.

ta0kira 01-24-2010 10:08 PM

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

Hko 01-25-2010 05:23 AM

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.
[...]

Xyro 01-25-2010 09:43 AM

Why on earth would you build the foundation of your system with duplo blocks? It is all about fine touch.

DavidMcCann 01-25-2010 05:57 PM

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

ta0kira 01-25-2010 07:41 PM

Quote:

Originally Posted by DavidMcCann (Post 3840319)
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

Those are interesting comments; however, C++ is quite a bit different than it was 16 years ago. The horror stories sound like pre-standardization problems, not that the standards committee always does the best thing now. You also have the other extreme where people won't allow a single call to libc in their code because "it's C" (I recall a post here or elsewhere that said this was a company policy for someone.)

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

Borax_Man 01-26-2010 01:23 AM

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.

jf.argentino 01-26-2010 03:18 AM

Quote:

n 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.
Not true, at least in the Linux kernel where drivers framework, sys fs (and maybe other parts) use OOP..

carbonfiber 01-26-2010 03:22 AM

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.

jf.argentino 01-26-2010 01:50 PM

Quote:

There's always the possibility that a large portion of the OS development 'crowd' simply prefers using C and assembly in general
IMHO it's not a fashion matter, more something relative to reliability: an easy syntax (like the C syntax) conduct to reliable standard specifications (read: with least shadow corners) => easy compiler writing => less bugs in compiler / compilation behaviour more predictable...
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.