r/cpp • • Jul 13 '22

Why does Linus hate C++ ?

308 Upvotes

436 comments sorted by

View all comments

26

u/MrRubberDucky Jul 13 '22

56

u/Daniela-E Living on C++ trunk, WG21 |🇩🇪 NB Jul 13 '22 edited Jul 13 '22

He seems to be lacking a lot of knowledge about about C++ - at least in those days. And he also doesn't seem to care. And that's just bad because even a little bit of C++ without even touching the stuff he obviously is so scared of would improve kernel code a lot.

When I wrote my first Windows NT 4.0 kernel device driver in the mid 90s, I happily used C++ there because of the power of abstractions. And it didn't hurt!

7

u/tesfabpel Jul 13 '22

Wikipedia says that the core part of the kernel is written in C and Assembly (just like Linux), while the graphics subsystem is written in C++.

Anyway, just quickly thinking about it, it makes at least some sense NOT to use C++ for a critical part like the kernel since it has many footguns that you need to always make attention to: for example, when you define a class or struct you have to be careful with unary constructors, copy / move constructors and assignments operators, copies are always behind the corner (but they can be optimized by the compiler).
I think those are things that are important in a kernel and the copy-by-default semantic of C++ alongside with the copy constructor / assignment op (instead of the move-by-default in Rust or the copy-by-default of PODs in C without any copy constructor / assignment op) may create some "difficulties".

13

u/pjmlp Jul 13 '22

Since Windows Vista that the kernel has been slowly moving into C++, even if it largely C.

Nowadays there are even template libraries for kernel development like WIL.

17

u/Daniela-E Living on C++ trunk, WG21 |🇩🇪 NB Jul 13 '22

In the 90s, a company called "Bluewater Systems" or the like sold a template library for Windows NT kernel mode driver development. In my case it was a godsent because of the helpful abstractions of PCI busmaster dma operations and the time it was saving me. The proof is in the development and testing time it took to flawless operation of that PCI coprocessor board: I implemented it first for OS/2 in pure assembly (14 days total effort), then for Windows NT in C++ using this library (4 weeks total effort), and afterwards also for Linux 2.x in C (8 weeks or so total effort 😱). Linux was the worst because of the total lack of good tools and the low expressive power of C as implementation language.

10

u/SonOfMetrum Jul 13 '22

The assembly part of the NT kernel is at mostly the HAL (hardware abstraction layer) and the boot loader. Because the entire point of that thing is abstract away the hardware specific bits and thus the required assembly. The HAL is relatively speaking very small compared to the rest of the kernel.

→ More replies (1)

30

u/Daniela-E Living on C++ trunk, WG21 |🇩🇪 NB Jul 13 '22

And yet, you still can use many C++ features safely in kernel code if you know your language.

5

u/angelicosphosphoros Jul 13 '22

if you know your language

One of the main problems with C++ is this is almost impossible.

5

u/Daniela-E Living on C++ trunk, WG21 |🇩🇪 NB Jul 14 '22

Right. And you don't have to be "mr. know-it-all" to be a proficient developer. You only need to know the parts that really matter.

But if a developers happens to not even know the most basic stuff of C++ (i.e. the stuff that doesn't even come near anything like f.e. exceptions or much of the library) then it's certainly better if that person stays away from using C++ in such environments in the first place (and possibly also from every other language).

Most of the core language is perfectly usable in kernel land. Look at the work related to 'freestanding' C++. It is that subset of 'vanilla' C++ that doesn't require an operating system underneath, f.e. kernel code).

→ More replies (3)

29

u/SergiusTheBest Jul 13 '22

Actually C++ is much safer than C. A simple operation as string concatenation can easily lead to buffer overruns or memory leaks in C.

→ More replies (2)

16

u/no-sig-available Jul 13 '22

Wikipedia says that the core part of the kernel is written in C and Assembly (just like Linux), while the graphics subsystem is written in C++.

The Windoiws NT kernel was initially designed around 1990. The options were totally different then.

many footguns that you need to always make attention to:

When you write an OS kernel, we assume that you are careful and know what you are doing. :-)

Copying a POD in C++ is exactly the same as copy a POD in C. Adding member functions to the struct, instead of manually passing a pointer as the first parameter, also makes no difference (except possibly for improved readability).

If you want to make an object move-only, in C++ you can delete the copy constructor and copy assignment. In C you cannot.

-------------

Linus was obviously shown some low quality C++ code once, and decided that all C++ code is bad forever. And with that attitude, people who know how to write good C++ have not bothered to show him any of their code. So he has proven himself right.

→ More replies (3)