As I know in order to write device drivers people usually use c++ or assembly? The choice of assembly is clear for me. But why c++? I guess it is possible to do in java (for example), or in other high level language as well. So why c++ is so common? Is there a specific feature of C++ programming language that is necessary for writing drivers?
-
"It's not C or 'Assembler'" -- although perhaps the first is a disadvantage ;-) Most drivers tend to be C, I'd imagine. Especially in say, the Linux kernel. – Jan 11 '11 at 18:41
-
If you think about it, does it really make sense to run a hardware driver inside a Java Virtual Machine? – David Rodríguez - dribeas Jan 11 '11 at 19:20
5 Answers
I would say that C is much more commonly used as a language for device drivers (over C++).
There are a couple of reasons for this:
- Most of the major operating systems are written in a combination of C and assembler and provide C language interfaces to the operating system.
- Writing a device driver usually involves communicating with a device which involves direct memory manipulation of registers and the like. C and C++ allow you to do this natively.
Edit - Example of Direct Memory Manipulation:
Let me make up some fictitious device. Say it has a register, that when you write certain values onto it, makes a light on the device turn on. This register is 32 bits wide. The top 31 bits say how bright to make the light, the lowest bit turns it on and off.
When you plug the device into the computer, the operating system assigns that register a particular memory location, (let's say 0x00FF00FF00 on a 32 bit OS). In order to turn the light on, the device driver would do something like this:
int* lightRegister = 0x00FF00FF00; // You would normally ask the OS for this address, not hardcode it.
int brightnessValue = 256; // Only Even numbers, the low bit must be zero.
*lightRegister = brightnessValue | 1; //Turn it on.
*lightRegister = 0; // Turn it off.
Higher level languages like Java, don't generally let you write into some random memory location like this.

- 28,667
- 7
- 60
- 71
-
What you mean by saying direct memory manipulation, and how it is done in C++? – Narek Jan 11 '11 at 18:49
-
-
There is no a reason to use C, unless you don't have a reasonably good C++ compiler. One can still limit himself to a proper subset, that will be both as efficient as the best C code but provide higher level abstractions as well. B.t.w. are there still C-only compilers? I think in most cases it's C++ compiler that compiles C code. – Gene Bushuyev Jan 11 '11 at 19:18
-
Thanks for clarification. I was suspecting this kind of answer, but the problem is that from literature I kow that to the pointer you can assign 0, NULL or address of the object of the same kind. Is it wrong? And about your axample how could you know what address with OS assign to the register that you need? – Narek Jan 11 '11 at 19:21
-
@Gene, I agree, there is no reason not to use C++ (I have written device drivers using C++), I was just noting that most drivers are written in C and not C++. I have no real proof of this, but it is my experience. – zdan Jan 11 '11 at 19:28
-
@Narek, you normally ask the OS for the address. Actually, the OS assigns a block of memory to a device and you would need to know the offset into the block that your register resides. – zdan Jan 11 '11 at 19:31
-
And what about "Thanks for clarification. I was suspecting this kind of answer, but the problem is that from literature I kow that to the pointer you can assign 0, NULL or address of the object of the same kind. Is it wrong?" ? – Narek Jan 11 '11 at 19:34
-
@Narek: Yes, you assign a NULL (i.e. 0) pointer. Usually that means "I haven't assigned this yet, because I don't know what to set it too yet". If you do that, then you need to add checks before you try to dereference the pointer. – zdan Jan 11 '11 at 19:43
-
@zdan No, no I mean is it legal to assign a number other than 0 or NULL (which is the same!). – Narek Jan 11 '11 at 21:42
-
1@Narek, the compiler will allow you to assign any value to a pointer. It is your responsibility to make sure that it is pointing to something valid. – zdan Jan 11 '11 at 22:20
There is a good discussion on the pros and cons of using C++ for device drivers in Windows here: C++ for Kernel Mode Drivers: Pros and Cons
A lot of the C++ vs. C discussion is "religious" and a question of personal preference. In general working at a lower level gives you better control over things like memory and performance but may also require more time duplicating facilities that may be available in higher level languages. Making those trade-offs and choosing the right tool for the right problem is part of what software development is about.
There is no fundamental reason why you couldn't use Java for writing a device driver. It's more of a question of run-time support for the language, the libraries and the kind of work you need to get done inside the driver (interrupt handlers, I/O etc.). Performance may also be an issue. You would need to develop a very large amount of infrastructure to do that. While Java doesn't offer low level facilities (such as pointers) these could be replaced with calls to a native function.

- 8,331
- 2
- 26
- 36
C++ can produce very efficient assembly code AND provide higher-level constructs such as templates, RAII and OOP. Assembly is much too slow to write by hand for modern programs and does't provide many things that modern programmers expect, like OOP, whereas Java would be MUCH too slow to write a device driver in.

- 144,682
- 38
- 256
- 465
-
If Java is so slow, then why C++ does not "defeat" Java at all and forever? – Narek Jan 11 '11 at 18:48
-
-
@DeadMG : Java isn't that slow. The real problem would be that Java runs against a VM, not the underlying hardware. – John Percival Hackworth Jan 11 '11 at 18:52
-
What you mean? As I know one of the reasons that Java is slow is that it is being translated into a bytecode that JVM executes. SO why thanks to JVM? – Narek Jan 11 '11 at 18:53
-
@Narek : I was answering your question : "why c++ does not "defeat" Java at all and forever?". I was not speaking about the speed of Java. – Xavier V. Jan 11 '11 at 18:55
-
@John Percival Hackworth does C++ run on hardware? It runs on OS, isn't it? – Narek Jan 11 '11 at 18:55
-
@Narek: They're not even remotely the same. Even the startup costs of the JVM exceed the performance limits for most device drivers. There's no such thing as defeating a language - they all have their pros and cons. C++'s pros just happen to be the right pros for device driving. – Puppy Jan 11 '11 at 18:59
-
@Narek -- there is no one size fit all in programming. On the other hand, there are people who can only ride a bike and those who can pilot a plane, you wouldn't expect the first category to automatically want to convert to the second no matter how many advantages it could bring. – Gene Bushuyev Jan 11 '11 at 19:10
-
@Narek: Ignoring .net, a C++ program is compiled to the hardware it is running on. There is an abstraction layer provided by the OS most programs run against, but for a driver the code runs against the hardware directly to provide the abstraction layer. – John Percival Hackworth Jan 11 '11 at 20:02
-
@Narek - there are many variables in the choice of implementation language. execution speed is only one, and comparatively rarely is it actually the most important. Some people say that 90% of developers time is spent _reading_ code thats already there. bytecode languages require an interpreter. compiled ones don't. some languages have defined memory deallocation semantics (e.g. c++, you know when memory will be released, java not)... its just horses for courses. btw: because of dynamic reoptimisation & JIT, surprisingly sometimes bytecode languages can be much faster than,say, c++ – time4tea Jan 11 '11 at 20:30
-
@time4tea: In theory. However, I have never once seen a single benchmark that shows that happens in reality- after all, you have to pay the cost of reoptimising and it has to happen in real-time while the program is running, whereas C++'s static optimization can take hours at compile-time. Every single performance analysis I've ever seen suggests that reasonably optimized code in both languages concerned leaves C and C++ as faster than .NET or Java by a factor of about 1.5 or more. – Puppy Jan 11 '11 at 21:06
I don't know why? and whether it's actually true, but I would guess, C++ combination of efficiency and high level of abstraction makes it a very good candidate for tasks that require performance and can benefit from high level abstractions.

- 5,512
- 20
- 19
Because C++ is the last well spread language (except C) which translates directly to machine code without too many complications.

- 55,948
- 11
- 128
- 197