Macros and inline functions

Source: Internet
Author: User

The first part: Macro

Why use macros?

because a function call must transfer the execution order of the program to an address in memory of the function, the program content of the function is executed and then returned to the place before the function is transferred. This transfer operation requires the site to be saved and the address of the memory to be executed before it is transferred to the site, back to the scene, and continue with the original saved address. Therefore, function calls have a certain amount of time and space overhead, which will affect their efficiency. The macro simply expands the code where it is preprocessed and does not require additional space and time overhead, so calling a macro is more efficient than calling a function.

But the macro also has a lot of unsatisfactory places.

in the C language:

1, the macro prone to some of the boundary problems, resulting in two of righteousness;

in C + +:

2, the macro can not call the C + + class of private or protected members;

Let's give an example:

#define Square (x) (x*x)

We use a number to call it, like Square (5), so there's nothing wrong with it, and the result is 25, which is obviously correct, but if we use Squre (5+5) to invoke it, we expect the result to be 100, and the result of the macro call is (5+5*5+5) , the result is 35, which is obviously not the result we're going to get. One way to avoid these errors is to add parentheses to the macro parameters.

#define Square (x) ((x) * (x))

NOTE: Macros in the call place, is simply a simple code substitution, so the parameters are enclosed in parentheses, there will be no function calls that stack, stack time and space overhead, the execution is more efficient.

Part Two: inline functions

from the above explanation, you can see the macro has some unavoidable problems, for the C + + classes can not access the private or protected members, how should we solve it?

An inline function is a function that the code is inserted into at the caller's code. As with #define macros, inline functions improve execution efficiency by avoiding the overhead of being called, especially if it can be optimized by the compiler by calling ("procedural integration").

inline functions are similar to macros, except that macros are substituted by a preprocessor for macros, and inline functions are implemented through compiler control. and the inline function is the real function, but when needed, the inline function expands like a macro, so it cancels the function's argument stack and reduces the cost of the call. You can call inline functions just as you would call a function, without worrying about some of the problems that might arise from dealing with macros.

how the inline function works explains:

for any inline function, the compiler puts the declaration of the function in the symbol table (including first name, parameter type, return value type).

If the compiler does not find an inline function error, then the code of the function is also placed in the symbol table.

When an inline function is called, the compiler first checks whether the call is correct (type safety check, or automatic type conversion, of course, all functions are the same).

If correct, the code for the inline function replaces the function call directly, eliminating the overhead of the function call.

This process differs significantly from preprocessing because the preprocessor cannot perform type safety checks or automate type conversions.

If the inline function is a member function, the address of the object (this) is placed in the appropriate place, which is not the preprocessor can do.

declaring an inline function looks very similar to a normal function:

void f (int i, char c);

When you define an inline function, precede the function definition with the inline keyword, and put the definition in the header file:

inline void f (int i, char c) {

    // ...

    }

The inline function must be declared together with the function body to be valid.

declarations such as the inline function (int i) are ineffective, and the compiler simply declares the function as a normal function, and we must define the function body.

Inline int function (int i) {return i*i;}

So we can define an inline function. We can call it the same as a normal function. However, the execution speed is faster than the normal function.

of course, the inline function also has some limitations. The execution code in the function is not too much, if the function body of the inline function is too large, the general compiler will discard the inline mode and call the function in a normal way. In this way, the inline function is as efficient as the normal function execution.

with the characteristics of the two above, we can completely replace the preprocessing macro with the inline function.

Part III: summary

The advantages and disadvantages of the inline function--

Advantages:

1) inline functions, which are defined in the line, are placed in the symbol table and replaced when used (like macros), and are highly efficient.

2) The inline function of the class is also a function. The Yi device calls an inline function, first checking the parameter problem, ensuring that the call is correct, as with the real function, eliminating hidden dangers and limitations.

3) inline can be used as a member function of a class and can use the protected members and private members of the class in which it resides.

Disadvantages:

The inline function takes the cost of replication, and the activity generates overhead.

1) If the code for the function is longer, using inline will consume too much memory, which may be handled automatically by the compiler as a non-inline function.

2) If there is a loop in the function body, then executing the function code is much more expensive than the call.

The difference between inline and macro

The difference is as follows:

1) When inline Yi is expanded, macros are expanded at precompilation. The time to expand is different.

2) The compiler inline function can be embedded in the target code, and the macro is simply a simple text substitution.

3) The federation does the type, grammar check, and the macro does not have such a function.

4) macros are not functions, and inline functions are functions.

5) macro definitions handle macro parameters with caution (the general parameters are enclosed in parentheses), otherwise two semantics are easily present, and inline definitions do not appear.

Macros and inline functions

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: info-contact@alibabacloud.com 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.