LinuxQuestions.org
Download your favorite Linux distribution at LQ ISO.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 10-05-2005, 04:11 AM   #1
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Rep: Reputation: 45
Lightbulb C vs C++ (not another flame) serious answers plz


Hi i have some questions about c and c++ i will describe my strory-experience and i want to discuss about it.... For a big period of time i was wondering why people use c and not c++ while the latter one is an aggregation of c with more and more feautures.... I have noticed that all the code for linux kernel, device drivers and networking components are written in c. Why didnt they used c++ instead? I know that in a theoritical basis this can be done but developers seem to avoid high level languages like c++ for such jobs... I need a good explanation for this... (good explanation means that i need some good arguments well supported)

Now i want also to provide my own experience... I was writing some code that was reading kernel parameters from a linux box and it was tuning some of them. I have written everything in c...... In this programme i had to create a union so as to pass many structs with one variable
union{
struct a;
struct b;
struct c;
}

When i have discussed this with one colleague at univeristy he started laughing at me saying... unions suck and it were only created in the past years for creating classes using c. Why didnt u use classes with c++... For bein really honest havent ever thought about it... WHY? Why didnt i use c++ or java or other high level language? Java as well c++ can use system calls like c... so why? But if the answer is yes u should have used them then why the system programming is usually on c? Unix network programming guid why use c and not other language?

I think the answer cant be one or simple... i feel that we use c before is a simple as it must be for low level things (kernel-system programming).. When we use c++ then the c++ compiler add a load of overhead for supporting classes,polymorf and other things...
I will stop here writing because i think your answers would be more important
Thx a lot
 
Old 10-05-2005, 07:12 AM   #2
kjordan
Member
 
Registered: Jul 2004
Distribution: LFS, I felt the itch and scratched it
Posts: 227

Rep: Reputation: 31
Here's Linus' reason for using C instead of C++.

Quote:
In fact, in Linux we did try C++ once already, back in 1992.

It sucks. Trust me - writing kernel code in C++ is a BLOODY STUPID IDEA.

The fact is, C++ compilers are not trustworthy. They were even worse in 1992, but some fundamental facts haven't changed:

* the whole C++ exception handling thing is fundamentally broken. It's _especially_ broken for kernels.
* any compiler or language that likes to hide things like memory allocations behind your back just isn't a good choice for a kernel.
* you can write object-oriented code (useful for filesystems etc) in C, _without_ the crap that is C++.

In general, I'd say that anybody who designs his kernel modules for C++ is either

* (a) looking for problems
* (b) a C++ bigot that can't see what he is writing is really just C anyway
* (c) was given an assignment in CS class to do so.

Feel free to make up (d).
And more on it here http://www.tux.org/lkml/#s15-3.

C is about, if not the closest language to machine level code besides assembly so therefore it's a lot easier to interact with the machine through it. It's all personal preference anyways. For some things C++ and Java are better, i.e. large projects where reusability with classes may be key. C is better for small projects although it's possible if you are organized enough in the way you write it it can be just as good with large projects. But C is the best for system programming since it's so close to assembly and has a very low overhead.
 
Old 10-05-2005, 10:04 AM   #3
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
One thing you need to consider with respects to GNU, Unix, and Linux software is much of it has been under development for a very long time. The Linux Kernel development, for example, started sometime around 1991. The Gnu C++ compiler was pretty terrible back in those days. In fact, the Gnu C++ compiler underwent changes which broke the ABI as as recent as the 3.0-3.1 version change if I remember correctly.

The exception model of C++ also doesn't work so well for kernels. Most people agree (you can read that lkml fallout from the above reply) that they would compile a C++ kernel with exceptions turned off. At the end of the day, data protection isn't important in the kernel (it is a see all, touch all entity anyway). You can write oo code in C, and the C compiler (even still) produces slightly smaller more optimized binaries then the C++ compiler does. So, the basic feeling is it would take an awful lot of work to re-write the Linux Kernel in C++ and the benefit would be very minimal if there is a benefit in the first place.

I can agree that I could see use in having modules written in C++ for various reasons... except to allow C++ anywhere you have to have some sort of C++ runtime support in the kernel which doesn't exist (at least not in any form as stable as the C runtime support).

The key is to use languages were they best fit. A kernel is a very low level thing and C is a pretty low level language. The code really isn't that complex (except for where the hardware spec warrants some oddities).

For something like a GUI application C++ makes a lot more sense. It is quite easy to think of those things in an object manner.

Last edited by jtshaw; 10-05-2005 at 12:29 PM.
 
Old 10-05-2005, 02:23 PM   #4
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Original Poster
Rep: Reputation: 45
So dont u think that c++ is not so good for writing modules?
Why if c++ is so good the net-snmp is written completely in c?
Apache as well , bind e.t.c
 
Old 10-05-2005, 02:36 PM   #5
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Perhaps because the people who wrote it chose to do it that way. Again, your talking about products that were first developed in a time when C++ compilers were not nearly as good as they are now. The performance they were looking for was probably not up to par in the C++ world at the time. If you have a large code base that works well already why bother throwing it away?

Not to mention the obvious: UNIX was developed in C (in fact, you could say C was developed for the creation of UNIX). Linux, BSD, etc. were trying to be as UNIX like as possible. Lots of the software was written by people who previously used UNIX, and lots of those people were C programmers. So naturally, they choose C.

C isn't a dilapidated crappy language. It is still probably the best optimized language by current compiler technology. It is a language most competent UNIX/*nix programmers know like the back of there hand... If your writing something that doesn't scream for OO design why not use C? At least 50% of the "C++" I see on a daily basis is basically just C code compiled with a C++ compiler anyway.

Last edited by jtshaw; 10-05-2005 at 02:45 PM.
 
Old 10-11-2005, 05:19 AM   #6
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Original Poster
Rep: Reputation: 45
Thx but the previous days i have also thought that mysql was written in c language... For being honest i dont know any server application (web/mail/database) being written in c++ or another object oriented language?
Why is that?
 
Old 10-11-2005, 05:47 AM   #7
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
I'm quite certain there are probably web/mail/database servers out there written in C++... (I believe litespeed the web server was written at least partially in C++).

Of course, for the most part, the same thing applies that I've already said. If somebody was starting from scratch today, they would probably be more likely to consider C++ as a viable option. However, if you are dealing with a large (and in general pretty good) code base that is already in C sticking with C makes a lot more sense.
 
Old 10-11-2005, 10:34 AM   #8
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Original Poster
Rep: Reputation: 45
Thx a lot for the answer.. plz let me reask my question. Let suppose that u are starting writing code for making a web server,mail server what language do u think that u ll use?
 
Old 10-11-2005, 11:25 AM   #9
Hivemind
Member
 
Registered: Sep 2004
Posts: 273

Rep: Reputation: 30
For any application that could be considered "shrink-wrapped" where interpreted languages are not an option, I would probably use C++. For any kernel, driver or other developement very closely interfaced with hardware, I would use pure C.
 
Old 10-11-2005, 12:34 PM   #10
jtshaw
Senior Member
 
Registered: Nov 2000
Location: Seattle, WA USA
Distribution: Ubuntu @ Home, RHEL @ Work
Posts: 3,892
Blog Entries: 1

Rep: Reputation: 67
Quote:
Originally posted by alaios
Thx a lot for the answer.. plz let me reask my question. Let suppose that u are starting writing code for making a web server,mail server what language do u think that u ll use?
If you were asking me to write a web server from scratch I'd write the thing in C.

Why?

The way I see it, a web server is a fairly basic application. It opens a socket and listens. When it gets a connection it spawns a thread. That thread receives the request messages and replies where required with the results. This can be done in a pretty simple manner and (the way I see it) doesn't benefit a whole lot from being designed in an object oriented manner.

Of course, that is just the way I see it.

Would I consider Joe wrong if he decided to use C++? No. Would Paul be wrong if he selected Java? No. Would Dave be wrong if he selected Python? No.
 
Old 10-11-2005, 01:25 PM   #11
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Original Poster
Rep: Reputation: 45
I cant understand whats the main difference between c and c++ why do u prefer c++ and not c? Where the object oriented approach is necessary and where is not?
 
Old 10-12-2005, 01:08 AM   #12
btmiller
Senior Member
 
Registered: May 2004
Location: In the DC 'burbs
Distribution: Arch, Scientific Linux, Debian, Ubuntu
Posts: 4,290

Rep: Reputation: 378Reputation: 378Reputation: 378Reputation: 378
Object oriented programming was developed, as I understand it, to address some of the complexity problems that were encountered with the first really big software projects. As others have mentioned in this thread, OOP isn't tied to any particular language. You can do OOP in C -- it's just harder since the language doesn't have any inbuilt constructs. In fact, the very first C++ compiler did nothing but translate the code to C and then invoke the C compiler.

Another reason that many people don't like C++ is that it's a big language with a lot of (arguably) ugly semantics forming a poor OO environment (people have big arguments about this, but detractors make this claim). C, by contrast, is a smaller language that is no less powerful in the hands of a good developer, which ultimately leads to better written, more understandable programs (one can argue the other way, too, that C's lack of explicit OO support makes programming large software *more* difficult and error prone).

In general, the features required by C++ make it unsuitable for writing kernel code which must execute efficiently without any library support other than that provided by the kernel itself (in other words, kiss STL goodbye unless you want to reimplement it in your kernell). That's not to say you can't write a kernel, or kernel code, in C++, but historically people have preferred not to. Part of the reason, as mentioned above, was the poor C++ compilers available at the time.
 
Old 10-12-2005, 09:10 AM   #13
sundialsvcs
LQ Guru
 
Registered: Feb 2004
Location: SE Tennessee, USA
Distribution: Gentoo, LFS
Posts: 10,659
Blog Entries: 4

Rep: Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941Reputation: 3941
I find languages like C++, Delphi and so-on to be extremely useful because they enable you to compartmentalize the code. An "object" simply consists of a hunk of data, and the code which is designed to manipulate that data, neatly bundled up into one "thing." The languages are also designed to simplify the fairly-common situations where several of the "things" are very similar to one another, "but not quite," enabling you to define them by sharing what is common between them (writing it once, maintaining it once) and specifying only the differences. Very handy stuff.

For kernel work, however, the operating-environment under which the code has to operate is substantially different. This is not "user land" anymore. A lot of the mechanisms that exist in "user land" ... mechanisms upon which a language such as C++ will rely ... do not exist in the world of the kernel. And that, I think, is the main and lasting reason why you do not see it being used there.

Personally, "I'm a lazy s-o-b and I don't deny it." If the computer can do the work for me so that I can get to the pub faster, then I figure that this is what we buy computers for. Languages like C++ and even-higher-level languages than this enable "the computer to do more work for me," and that's the way I like it.
 
Old 10-12-2005, 02:00 PM   #14
yapp
Member
 
Registered: Apr 2003
Location: Netherlands
Distribution: SuSE (before: Gentoo, Slackware)
Posts: 613

Rep: Reputation: 30
C coders write a solution. Each statement is an important step towards the solution.
C++ coders write classes to describe a problem. The interacting classes form the solution of that problem.

It's a different approach to coding, each has it's advantages.

I tent to use C++ because I like to focus on "how can part A interact with part B", and create a nice design for that, have a global overview of the program. With C code, I get the feeling I need to focus on "how can I read/copy/edit this char* safely", something I never have to with C++.
 
Old 10-12-2005, 02:06 PM   #15
alaios
Senior Member
 
Registered: Jan 2003
Location: Aachen
Distribution: Opensuse 11.2 (nice and steady)
Posts: 2,203

Original Poster
Rep: Reputation: 45
Ok.ok can u give me any examples (except from kernel code) that c language can still be helpful
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
flame warriors titanium_geek General 3 03-04-2005 10:45 AM
Which flame warrior are you? vharishankar General 7 03-03-2005 05:55 PM
cant get C++ to pause. C++ 4 dummies. NEWBIE! Plz no flame. RHLinuxGUY Programming 5 10-14-2004 11:45 AM
Why choose Mandrake (No Flame) ? svarreby Mandriva 14 04-06-2004 09:47 PM
Flame war anyone? hazza96 LQ Suggestions & Feedback 9 07-24-2001 02:44 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:49 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration