Inline functions and macros

Source: Internet
Author: User
An inline function is also called an embedded function. Program .
Function calling requires the establishment of a stack memory environment, parameter passing, and program execution transfer, which requires some time overhead.
The Inline Function Definition in C ++ is very simple. You only need to add a keyword inline before a common function. In addition, there is no difference (including the function call method) with the ordinary function on the surface ), because of this, in the opinion of many c ++ beginners (or even some people with C ++ programming experience), inline is just a concept, in fact, this is not a thorough understanding of inline functions. Let's talk about the differences between inline functions, common functions, and macros. I believe that after reading the following sections, you must have a good understanding of the three.
The biggest difference between an inline function and a common function is the internal implementation, rather than the surface form. We know that when a common function is called, the system first needs to jump to the entry address of the function, execute the function body. After the execution is complete, return to the function calling place. The function always has only one copy, while the inline function does not need to perform an addressing process. When the inline function is executed, this function is expanded (similar to macro usage). If this inline function is called at N, there will be n Code Segment copy.
From the perspective of inline function calls, it improves the code execution efficiency because it lacks an addressing process, but this is in exchange for space.
For a function declared as inline, its code segment cannot be too long or too long. Some compilers regard it as a common function (whether the length of the function body exceeds the limit does not seem to be specified, this is indeed not a good rule. I personally think it should be determined by the logic of the function body ).
The following is an example of an inline function declaration:
Inline void setval (int A) {m_ B = };
Inline int getval () {return m_ B };
From the above example, we can see that the declaration and implementation of inline functions are usually in a file (usually in. h ).
Let's talk about the difference between inline functions and macros. In many documents, when talking about inline functions, we say that inline functions are similar to macros, but they are similar. After all, we cannot use these two functions in a different way.
The similarities between the two are that the compiler will expand its code during execution and continue the following processing after execution. The difference is that a macro is a simple text replacement, and it cannot return values, nor does it have the concept of common function parameters. inline functions have the characteristics of common functions, such as parameter lists and return values. The following is an example:

1. # define count (x) (x * X) // a macro that calculates the product.
2. inline int count (int x) {return x * x} // an inline function for calculating the product

Printf (count (3); // The result is count (3) (3*3) = 9;
Printf (count (3); // The result is count (3) {return 3*3} = 9;

The above example does not seem to indicate the difference between the two. Let's change the call in the above example and look at the result.

Printf (count (2 + 3); // The result is count (2 + 3) (2 + 3*2 + 3) = 11
Printf (count (2 + 3); // The result is count (2 + 3) {return 5*5;} = 25;

If the macro is to reach the result of the product of 25, it should be written as follows:
# Define count (x) * (x ))
Corresponding to the example above is # define count (2 + 3) (2 + 3) * (2 + 3 ))

Note:
Inline functions cannot contain complex structure control statements, such as switch and while. If an inline function has these statements, compile the function as a common function to generate the function call code.
In addition, recursive functions (self-called functions) cannot be used for inline functions.
Inline functions are only applicable to 1 ~ Five rows of small functions. For a large function that contains many statements, the overhead of function calling and return is relatively insignificant, so it is not necessary to use 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.