The C ++ optimizer, as a general rule derived class constructor, should not directly assign values to a base class data member, but pass the values to the appropriate base class constructor. Otherwise, the implementation of the two classes becomes tightly coupled. tightly coupled) it is more difficult to modify or extend the implementation of the base class correctly.
The following are examples of the C ++ optimizer:
1) Hardware registers of parallel devices, such as status registers)
2) Non-automatic variables that will be accessed in an interrupt service subroutine)
3) variables shared by several tasks in multi-threaded applications
People who cannot answer this question will not be hired. I think this is the most basic problem to distinguish between C programmers and embedded system programmers. Embedded programmers often deal with hardware, interruptions, RTOS, and so on. All these require C ++ optimizers. If you do not know volatile content, it will lead to disasters. Suppose the subject correctly answers this question. Well, I doubt if this is the case.) I will go over it and check whether this guy understands the full importance of volatile.
- Better understanding of C ++ programming learning and research
- Elaborate on the relevant learning methods of C ++ for discussion and research
- Waste collection defects in c ++
- Learn and guide Visual C ++ development tools
- Introduction to function overloading in C ++
1) can a parameter be const or volatile? Explain why.
2) can a pointer be volatile? Explain why.
3); what are the following function errors:
- int square(volatile int *ptr)
- {
- return *ptr * *ptr;
- }
The answer is as follows:
1) Yes. One example is read-only status registers. It is volatile because it may be unexpectedly changed. It is const because the program should not try to modify it.
2); yes. Although this is not very common. One example is when a service subroutine repairs a pointer to a buffer.
3) This code is a little abnormal. The purpose of this Code is to return the pointer * ptr points to the square of the value. However, since * ptr points to a volatile parameter, the compiler will generate code similar to the following:
- Int square (volatile int * ptr)
- {
- Int a, B;
- A= * Ptr;
- B= * Ptr;
- Return a * B;
- }
- * The value of * ptr may be unexpectedly changed, so a and B may be different. As a result, this Code may not return the expected square value! The correct code is as follows:
- Long square (volatile int * ptr)
- {
- Int;
- A= * Ptr;
- Return a *;
- }
To facilitate the compilation of large-scale programs, the compiler generally has two phases: Compilation and linking, which allow large programs to be divided into multiple independent small modules for separate compilation. Some ready-made libraries can also be introduced. The C ++ optimizer is mainly divided into two steps: Analysis and code generation. optimization can also be performed in the middle.
The C ++ optimizer generates the target code for each file, links the program to connect the modules, explains the external references of each module, links them into some system libraries, and finally generates executable programs. To check whether the source code syntax is correct, the compiler must perform a static type check, but the external variables and function compilers used in the program are unknown and must be declared by the programmer before use. The Declaration tells the compiler that the name will be defined somewhere. It should be used as declared, and the definition will allocate memory, and the definition will also be declared. The linked program only links modules that contain the functions or variables you use.