473,520 Members | 2,710 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C is better than C++ for small projects, right?

You can scroll down to the last line of this post in order to find the
question I really want to discuss.

I was just pondering the competing applicability of C vs. C++ in certain
problem domains. Then I saw this as Stroustrup's FAQ of the day:

http://www.research.att.com/~bs/bs_faq.html#C-is-better
Q:"C is better than C++ for small projects, right?"

A:"Not in my opinion. I never saw a project for which C was better than C++
for any reason but the lack of a good C++ compiler."
What I was thinking about was not the size of the project, but rather the
level of the project in terms of abstraction. Here are some products I
know of which are written in C, and I wonder if there is any compelling
reason that C++ could not be used, or any significant advantage to using
C++.

DBus, OpenSSH, the Linux kernel, OpenGL, Emacs (first implemented in C by
James Gosling).

If I were asked what the biggest problem with using C++ for a project is, I
would say the difficulty in learning C++. Linus Torvalds has used some
strong words to state his opinion on using C++ in the Kernel.

<quote>
On Tue, 20 Jan 2004, Robin Rosenberg wrote:

This is the "We've always used COBOL^H^H^H^H" argument.


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).

Linus
</quote>

OTOH, we have this more scientific sounding assessment suggesting there /is/
merit to using C++ in the kernel:

http://netlab.ru.is/exception/LinuxCXX.shtml
My own opinion is that I think C code is ugly. That really is a bit more
reasoned than it may sound. A good example is OpenGL and its hundreds of
flags used as parameters to functions all in one flat namespace. I will
however, concede that C++ programmers often fail to make effective use of
namespaces, especially when communicating with humans. I am at fault in
the area.

A good example of what I mean here is the common practice of talking about
"global" functions in the std namespace. There are many times when text,
or code could be made more clear by being explicit about, e.g., which
getline is being described or invoked. IOW, rather than writing getline in
a tutorial, or other context where the meaning may not be clear it may be
more effective to write std::getline. I sometimes get the sense that C++
programmers feel mildly embarrassed about namespaces. Many also fail to
see namespaces as a means of human-to-human communication. They think of
them exclusively as a means of avoiding naming collisions.

I do wonder if the abstractness of C++ does (or can) make C++ more difficult
to work with when it comes to code that's close to the metal. That is,
code which depends closely on the exact memory addresses being accessed,
and order of machine instruction invocation?

Is C++ a good language for writing device drivers and other OS components?

--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Jul 23 '05 #1
4 3092
* Steven T. Hatton:

Is C++ a good language for writing device drivers
C is, and C++ incorporates essentially all of C while
adding little or no overhead, and much better abstraction. But
that assumes a reasonable interpretation of the question. The
reasonable interpretation: we're talking about specific implementations
that provide the necessary functionality (types, linkage, and so on),
because standard C and standard C++ only allow _programs_ to be
created, and in that perspective it really boils down to QOI.

and other OS components?


A good part of Windows is, AFAIK, implemented in C++.

--
A: Because it messes up the order in which people normally read text.
Q: Why is it such a bad thing?
A: Top-posting.
Q: What is the most annoying thing on usenet and in e-mail?
Jul 23 '05 #2
Alf P. Steinbach wrote:
* Steven T. Hatton:

Is C++ a good language for writing device drivers


C is, and C++ incorporates essentially all of C while
adding little or no overhead, and much better abstraction. But
that assumes a reasonable interpretation of the question. The
reasonable interpretation: we're talking about specific implementations
that provide the necessary functionality (types, linkage, and so on),
because standard C and standard C++ only allow _programs_ to be
created, and in that perspective it really boils down to QOI.


It's conceivable that a compiler built strictly for C might use different
strategies more suited to low-level programs than a C++ compiler. But the
question of using C verses C++ is really a question of whether the C++
specific parts of C++ are useful or harmful in implementing low-level
components.

Another example I could have mentioned is sun's implementation of Java.
Much of that code is written in C. That is, it would compile with C
compiler.
and other OS components?


A good part of Windows is, AFAIK, implemented in C++.


Windows NT started out being written in C with special OO constructs. Where
it's gone since, I don't know. As to whether Windows is an example of the
right way to do things, that is a mater of opinion.

--
If our hypothesis is about anything and not about some one or more
particular things, then our deductions constitute mathematics. Thus
mathematics may be defined as the subject in which we never know what we
are talking about, nor whether what we are saying is true.-Bertrand Russell
Jul 23 '05 #3
A good part of Windows is, AFAIK, implemented in C++.


I would say only Windows GDI, GDI+, and COM related portions (OLE, MTS,
DCOM, etc) are written in C++. Although, they use the same compiler for both
C and C++ source.

ben
Jul 23 '05 #4
Steven T. Hatton wrote:
DBus, OpenSSH, the Linux kernel, OpenGL, Emacs (first implemented in C by
James Gosling).
They lack a good C++ compiler. The authors of those systems started before
C++ had grown up and standardized. And we still don't have a
standards-compliant compiler.

The answer to your Subject line is that C++ is always better for small
projects with small markets, because when you use the Standard Library
correctly (and a little Boost) you can write code that looks just like it
was written in a Very High Level Language.
- 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++.
Linus likes to know what opcodes he's getting. But I don't see Bjarne with a
gun to his head forcing him to use those language features.

C++, ultimately, is nothing but C with the ability to call statically typed
virtual functions. If you don't need all the extra syntactic sugar and
exception-handling salt, you are free not to use them.

So Linus's argument is just "lack of a good C++ compiler".

A good counter example here is an ORB, such as COM/ActiveX. MS maps
published object methods onto their implementation of the C++ vtable system.
Because MS knows what opcodes they are getting (and because C++ provides
vtables robustly in a way that hides the fragile details), we have an aspect
of C++ pushed down to the kernel level. (ActiveX is indeed bloatware, but
you don't need to use all the extra stuff if you don't want it. IUnknown and
IDispatch are enough...)

So in between snarling and foaming at the mouth, Linus needs to confess how
ORBs on Linux typically work...
Is C++ a good language for writing device drivers and other OS components?


Those in love with C are really enamored of portable assembly. They like
knowing what opcodes they get at each juncture.

C++ has a different set of tricks and traps, but fundamentally it's also
portable assembly.

--
Phlip
http://www.c2.com/cgi/wiki?ZeekLand
Jul 23 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

220
18813
by: Brandon J. Van Every | last post by:
What's better about Ruby than Python? I'm sure there's something. What is it? This is not a troll. I'm language shopping and I want people's answers. I don't know beans about Ruby or have any preconceived ideas about it. I have noticed, however, that every programmer I talk to who's aware of Python is also talking about Ruby. So it...
1
1571
by: broebel | last post by:
I'm just wondering, people tell me that helping to debug someone elses program is a really good way to learn to understand the language it's programmed in. My question is: from wich point of can someone start to try to help a programmer by debugging or testing a program. If indeed you already need a profound knowledge of the language...
5
1712
by: Hugo Elias | last post by:
Hi all, I have an idea for a better IDE. Though I don't have the skills required to write such a thing, if anyone's looking for a killer app, maybe this is it. If I'm typing at a rate of 10 characters per second (which is *very* fast for sustained code writing), then my computer is executing around 200,000,000 instructions as it waits...
19
4067
by: James Fortune | last post by:
I have a lot of respect for David Fenton and Allen Browne, but I don't understand why people who know how to write code to completely replace a front end do not write something that will automate the code that implements managing unbound controls on forms given the superior performance of unbound controls in a client/server environment. I can...
1
2741
by: Xiangliang Meng | last post by:
Hi, all. Recently, I find there is a way in our project to maintain a global set in many files by using preprocessing directives. I'm wondering if we could find a better method for this. Many colors are referred in different subsystems in our projects. They are defined as enumeration constants and a single color must be the same value...
11
2096
by: Daylor | last post by:
hi. im using option strict on. im doing in ,from the simple reason ,to be warn when there are implict conversion like string to int ,int to string. BUT. the price ,(now i see ), is very bad. if i have class CCar and interface ICar when im doing this : ICar = new CCar
22
2686
by: JoeC | last post by:
I am working on another game project and it is comming along. It is an improvment over a previous version I wrote. I am trying to write better programs and often wonder how to get better at programming. I tend to learn what is useful and gets the job done. I am always curious if there is some techique I don't know. I read books and study...
169
8956
by: JohnQ | last post by:
(The "C++ Grammer" thread in comp.lang.c++.moderated prompted this post). It would be more than a little bit nice if C++ was much "cleaner" (less complex) so that it wasn't a major world wide untaking to create a toolchain for it. Way back when, there used to be something called "Small C". I wonder if the creator(s) of that would want to...
6
1323
by: Christopher Vogt | last post by:
Hej everybody, I built something for myself that might help some of you as well. Looking at a couple of PHP template engines made me think. I have two main requirements for a presentation layer framework: - use PHP as the template language - effective XSS prevention without betting on discipline Plain PHP only satisfies the first. I...
0
7299
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7438
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7602
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7163
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
5740
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then...
1
5125
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3282
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
1
836
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
0
506
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.