Chapter 2 basics-5th. From code programs

Source: Internet
Author: User
[Back To Directory]

Vernacular C ++

Chapter 2. Basics

There is always some knowledge. After years, we will feel the power of it.


. SlaveCodeToProgram

This is a line of code:

Cout <"Hello world! "<Endl;

How does it become a program to play "Hello World" on the screen?

From a line of text code to a hardware action, we cannot review this process in detail, but the important step must be understood:

The program code written in advanced languages must be "converted" into machine languages before it can be executed by machines-Why do computers only know machine languages? We have learned before: because the machine language is only 0 and 1, it can be directly converted into hardware status changes, such as power-on and power-off ).

[Important]: Can we directly operate the hardware of programs we write?

PC (PC) programs that can directly operate on hardware have been a long time ago. In fact, on a PC, only the "Operating System" can be used to directly operate the hardware. Other applications usually need to use the programming interface (API) of the operating system ), or access the hardware under the "supervision" of the operating system.


SetSource codeTo "machine language". Currently, there are three common methods: compilation, translation, and virtual machine.

5.1.1. Compile

First, a program called "compiler" is required. Then, the Code Compiled by (programmer) is handed over to the compiler for compilation, and a new application is obtained for the compilation result.

The result is that new applications can run directly without the compiler or the source code, so they can deal with the operating system.

Figure 5-1 compile the code into a program that can directly access the operating system or even hardware


If you use natural language translation work as a metaphor, A Compilation Program is equivalent to a "Translation": If you want to deal with a British customer, you have written your words in Chinese, then ask a Translator (compiler) to translate it into English documents and hand it over to the customer (operating system ).

    • Disadvantages

First, if you change the language, we may need another interpreter-for another operating system, for example, a compiler.

2. What is worse than the translation in reality is that if we change the operating system, we must have the operating system at hand, to compile programs that can run on the operating system.

3. If you only want to translate it into English at the beginning, you may have used too many English allusions. In this way, it is a beautiful expression in English.Article; Some statements may become awkward when translated to French, or even some typical
Therefore, it cannot be translated at all-C ++ programs that can be compiled in Windows may not be compiled in Linux, especially when you did not think of cross-platform problems at the beginning.

4. in the initial communication, the efficiency was very poor, because every time you fully expressed it on paper and handed it over to the translation, the program could not write 100% correctly at once, for compiled programs
Debugging efficiency is relatively poor. It is often necessary to repeatedly modify the program, compile the program, and check whether the result is correct. The compiler will pick out your syntax errors strictly, however, it is almost impossible to identify content problems for you directly.

    • Advantages

First, after the negotiation is completed, your customer can directly read the documents at any time and place without having to assign a translation to him frequently, your program can run independently without a compiler.

Second, since your customers can directly read the documents, this is usually the most efficient method-yes, the compiled program does not need to be explained every time, so in terms of operational efficiency, has inherent advantages.

3. You can use the story that your target customer understands to add wonderful articles. For example, if you want to promote a hero to an American customer, you can cite Washington, France, and Napoleon. In addition, because
It's a translation. You have time and energy to optimize your documents. Here we mean: Because you know in advance which operating system program you want to translate, therefore, you can find out the functions of the target operating system.

5.1.2. Explanation

First, a program called an interpreter is required. Then, the compiled code is handed over to the interpreter for explanation. Based on the needs of the Code, the interpreter deals with the operating system.

The result is: in this case, the interpreter and the code are combined and called as a program, but we know that there is no new program that can actually be generated by itself.

Figure 5-2 The program is interpreted and executed and can only deal with the operating system through the interpreter


For example
If we use natural language translation work as a metaphor, the interpretation program is equivalent to an on-site "interpreter": When you talk about the previous section, the interpreter immediately translates it to the customer. However, the worst thing is that in this process, only note
Your oral content does not record the translated content (including the customer's own), so every time the customer wants to know what you said, you must find the translation and re-translate it.

    • Disadvantages

1. The customer must always carry the translation. In good cases, multiple documents can be used with one translation. In the worst case, each document must be assigned with one translation, the customer is surrounded by a bunch of translators (not bodyguards) and tired.
This is often the case when you write a K "program" with dozens of megabytes of "interpreter" during installation, your users are also annoying.

Second, the operation efficiency is too low, too obvious, there is no need to compare again.

Third, interpretation, compared with translation, the accuracy will be reduced. You may not be able to find a problem with the previous sentence until it is very late. Compared with the strict syntax requirements of the compiler, the explanatory program tends to have loose syntax requirements, this is sometimes a good thing, but it may also be a bad thing. (Of course, there are also programs that are both translated and strictly checked by syntax ).

Fourth, because there is always one translation in the middle, when there is a specific need, it will often feel like itching in the barrier, failed-the interpreter may have configured 100 functions in advance, but when you need 101st functions, you will find it very difficult to implement.

    • Advantages

First, because it is an interpreter, I simply say a large segment before translation. In the case of difficulty, I would like to say a sentence for translation and then listen to the customer's feedback, this communication process is smooth-the interpreter program is often more efficient in the initial debugging stage.

Second, you do not need to compile on a specific operating system in advance. This means that the explanatory program has the advantage of cross-platform. In addition, some interpreted languages are usually packaged or tailored in terms of syntax implementation, library function implementation, and an interpreter customized for different operating systems, therefore, most interpreted languages have good cross-platform capabilities.

5.1.3. Virtual Machine

There are two types of virtual machines in the component industry: one is virtual machines for users (persons), such as VMware software, which can be simulated into two computers on one machine, the "two computers" can run at the same time, and
You can also use different operating systems. Another type of virtual machine is what we are talking about at this time. It only simulates a machine for the "program". Its purpose is to allow the program to ignore what hardware it runs on.
Operating System.

[Tips]: Can I coordinate "Compilation, explanation, and virtual machine?

It is not reasonable to coordinate "virtual machines" with "Compilation and interpretation. Because in the "virtual machine" world, it can also contain "compiled" program and "interpreted" program. However, if you only consider how the final program runs, the virtual machine can be an independent method.


We can see that the "virtual machine" adds an isolation layer between the program and the operating system. In the isolation layer, the code can be compiled into a program using the "compile" method. But it is only a program compiled into a "virtual machine.

Figure 5-3 The code is "compiled" into a "program" in the Virtual Machine"


The "program" in a virtual machine can only directly access virtual machine resources. It is a virtual machine that really deals with the operating system or hardware. Therefore, the virtual machine is considered as a stronger "interpreter ", there is nothing wrong with it. In turn, some of the previous typical interpreted languages, such as Python, are sometimes treated as a "Virtual Machine" as their interpreters become more and more powerful ".

Intuitively, the existence of virtual machines will inevitably lead to slower running of the program, which is indeed a long time performance of virtual machines. However, with the advancement of virtual machine technology, the speed of the program is also increasing.

Another intuition is that the existence of virtual machines greatly enhances the cross-platform running capability of programs. This is also one of the important purposes of using virtual machines. Java's slogan is: "One-time writing, running everywhere ".

The advantages and disadvantages of virtual machines are not listed.

[Tips]: Do programs on virtual machines "write once and run everywhere?

Because there are still some differences between virtual machines on different operating systems or virtual machines developed by different manufacturers, these will bring some relatively hidden issues during cross-platform running of programs, therefore, it is also common to perform system test run, debugging, and modify part of the code.

On the other hand, virtual machines like. Net do not actively implement cross-platform implementation because of their different commercial interests.


[Back To Directory]

Vernacular C ++

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.