开发者

What is the advantage of c++ in writing device drivers? [duplicate]

开发者 https://www.devze.com 2023-02-03 12:26 出处:网络
This question already has answers here: Why are drivers and firmwares almost always written in C or ASM and not C++?
This question already has answers here: Why are drivers and firmwares almost always written in C or ASM and not C++? (15 answers) Closed 6 years ago.

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?


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:

  1. Most of the major operating systems are written in a combination of C and assembler and provide C language interfaces to the operating system.
  2. 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.


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.


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.


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.


Because C++ is the last well spread language (except C) which translates directly to machine code without too many complications.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号