inline

C ++ definition format

inline definition format

1. Inline keyword is used to define a class of inline functions, introducing it the main reason is to use it Instead of the macro definition in the form of expression in C.

expressions in the form of the form:

#define expressionname (var1, var2) ((var) + (var2)) * ((var) - (var2)) < / p>

Replace this form is as follows:

1. The reason for using define this form macro definition is because the C language is a high-efficiency language. This macro defines in the form and uses a function, but it is implemented using the preprocessor, without parameter stack < B>, code Generates a series of operations such as . Therefore, the efficiency is high, which is a major reason it is used in C.

2. This macro definition is similar to a function in the form, but when using it, it is just a simple replacement in the pre-processor symbol table, so it cannot be detected by parameter validity, and cannot enjoy the C ++ compiler strict type. The advantage of checking, and the return value is also not enforced to convert to a convertible appropriate type. In this way, its use has a series of hidden dangers and limitations.

3. Introducing access control of classes and classes in C ++, so that if an operation or an expression involves a category protection member or private member, you cannot use this macro definition (because you can't put this pointer. In a suitable location).

4. The purpose of Inline is also to replace the macro definition of this expression, which eliminates the disadvantages of macro definitions, and inherits the advantages of macro definitions.

predefined

corresponds to 1-3 points above, as follows:

1. Inline defined inline functions, the code of the function is placed in the symbol table, directly replacing it directly (like macro is launched), no call overhead, efficiency is also high.

2. Obviously, the inline function of the class is also a real function. When calling an inline function, the compiler will first check the type of the parameters, and ensure that the call is correct. Then make a series of related inspections, just like any of the true functions. This eliminates its hidden danger and limitations.

3. Inline can be used as a member of a class, of course, can use the protection member and private member of the class.

When using the inline function:

First, you can use the inline function to fully replace the macro definition of the form of the form.

It should be noted that the inline function is generally only very simple in the function content. This is because the code of the inline function will be expanded in any place to call it. If the function is too complex, the evil consequences of code expansion is likely to increase the benefits of efficiency. The most important use place for inline functions is the access function for class.

Function and Precautions

(1) The function of the inline function

● For inline functions, C ++ may be directly used in the function code The call of the function is called the inline deployment of the function body.

● For small functions of only a few statements, the call to the function is often much larger than the code that is related to the code, the code is often much better than the code of the correspondence itself. Therefore, for such simple, frequent use frequent small functions, the inline function can improve operational efficiency.

(2) caution inline function

inline

Inline is the price of code expansion, and only saves the overhead of the function call, thereby increasing the efficiency of the function. If the time of the function is executed, it will be less efficient than the overhead of the function call. On the other hand, each inline function is called to copy the code, which will increase the total amount of the total code amount to consume more memory space. The following cases should not use inline:

1) If the code in the function body is relatively long, the use of inline will result in higher memory consumption.

2) If a loop occurs in the function body, the time to perform the code in the function is larger than the overhead of the function.

Inline function and general function difference

1) The inline modifier is more than one inline modifier than the general function.

2) Inline function is to copy "inlaid" to the main function, which is to place the code of the inline function directly on the inner function, which is different from the general function, the main function When the general function is called, it is an entry address that the instruction jumps to the called function. After executing the called function, the instructions continue to perform the following code on the main function; because the inline function is the code of the function Placed directly in the position of the function, so there is no command jump, and the instruction is executed in order.

3) The general function code segment is only one copy, placed in some position in the memory, when the program is called, the instruction jumps; when the next program calls it, the instruction is jumped Come over; the inline function is a few inline functions in the program, and the code of the inline function will copy several copies of the corresponding location

4) Inline function is generally in the head file Definition, and general functions declare in header files in CPP.

Usage

inline:

1. This function is defined in the class:

Class ClassName {

.....

....

int getWidth () {RETURN M_LPICWIDTH;}; // If directly defined in the class, do not need to be modified with inline, the compiler is automated to the inline function

... // This method is in "C ++ Primer" The

....

}

2 is mentioned. Inline keywords before class:
 class account {public: Account (double initial_balance) {balance = initial_balance;} // and 1 same double getBalance (); // Declare Double in class Double Deposit (double amount); private: double balance;}; inline Double account :: getBalance () {return balance;} // Add Inline Key Double Double Account :: Deposit (Double amount);} inline double account :: withdraw (double amount) {return (balance - = amount);} 

There are also some rules to pay attention: < / p>

1. Inline shows that it is just a suggestion for the compiler, and the compiler can choose to ignore this suggestion. For example, you specify a function of up to 1000 multi-line functions as inline, the compiler ignores this inline, restore this function into a normal function.

2. When calling the inline function, to ensure that the definition of the inline function allows the compiler to "see" to, that is, the definition of the inline function is in the header file, which is different from the usual function definition. But if you are used to defining a function in a CPP file, or if you want the head file to be more concise, you can do this:

 // Someinline.h in 
 #ifndef someinline_h # # Define Someinline_hTYPE EXAMPLE (VOID); // ........ Declaration of other functions #include "someinlie.cpp" // Source file suffix name with the compiler # include " Someinline.h "inline type example (void) {// ..........} // ......................................................... P> The above method is universal, effective, can be used with confidence, do not have to worry that the CPP file containing the CPP file in the header file will cause compilation errors.

Inline Hook

principle

Inline-hook By modifying the process of function, jumps to the custom process, then jump back to the original function to ensure the system Do not collapse, implement the tampering of code logic. As shown in Figure 1:

First replace the target instruction to the jump instruction, the jump address is a compilet code we have written by it. This assembly code first execute the code specified by the user, such as modifying the register. Value, then execute the original instruction 2 replaced, and finally jump back to the original instruction 3, the restore the normal operation of the program.

flow

(1) Constructs the jump instruction.

(2) Find the Hook function address in memory and save the top 5 bytes at the HOOK location.

(3) Write the constructed jump instruction to write the location of the HOOK.

(4) will be transferred to our process when executed by the Hook position.

(5) If you want to perform the original process, cancel the hook, that is, restore the modified byte.

(6) Execute the original process.

(7) Continue Hook to live in the original position.

Related Articles
TOP