Inline functions and macro definitions

Source: Internet
Author: User
Tags function definition

Inline functions can speed up the program as compared to normal functions, because no interrupt calls are required, and inline functions can be embedded directly into the target code at compile time, and the macro is simply a replacement. Inline functions do parameter type checking, which is a disadvantage of inline functions compared to macro definitions. Inline refers to the embed code, which is where the code is written directly to it, rather than jumping. For short code, inline increases the amount of space consumed in exchange for efficiency, which is exactly the same as the macro definition, but inline is more secure without any additional cost compared to the macro, and if an inline function is required, it needs to be based on the actual situation.

The general inline function is used only for the following situations:

(1) A function is repeatedly called;

(2) The function has only a few simple lines, and the function does not contain a for, a while, a switch statement.

Generally speaking, we do not need to write a small program to define the inline, but if you want to complete a project, when a simple function is called multiple times, you should consider using inline. Macro definition in C is extremely important, unless you have to do so, and in C + + is much less used, the first rule about macros is never to use it, unless you have to do so. Almost every macro indicates a flaw in the programming language, in the program, or in the programmer, because it will re-manipulate the body before the compiler sees the body of the program. Macros are also a major headache for many programming tools. So, if you use macros, you should be prepared to get fewer services from a variety of tools, such as troubleshooting systems, cross-referencing systems, contour programs, and so on.

The macro definition is a simple substitute for no validation at the code, whereas inline functions directly insert code into the call, reducing the resource consumption at the time of the normal function call. The macro definition is not a function, but the pre-processing phase is compiled before compiling, replacing the macro body with the strings in the program. The keyword inline must be placed with the function definition body to make the function inline, and simply placing the inline on the function declaration does not have any effect, as the function foo () shown below cannot be an inline function:

inline void foo (int x,int y);
void foo (int x,int y) {}
The function foo () of the following style becomes an inline function:

void foo (int x,int y);
inline void foo (int x,int y) {}
So inline is a "keyword for implementation", not a "keyword for declaration." Inline can improve the efficiency of function execution, so why not all functions are defined as inline functions. Inline is at the expense of code duplication, which simply eliminates the overhead of function calls, thus improving the efficiency of function execution. If the execution of the code in the function body time, compared to the cost of function calls, then the efficiency of the harvest will be very small, on the other hand, every call to the inline function to copy code, the total code of the program to increase the amount of memory space consumption.

There are cases where inline functions are not appropriate:

1, if the code in the function body is longer, the use of inline will result in a higher memory consumption cost, and 2, if a loop occurs in the function body, the time to execute the function body code is greater than the cost of the function call. Inline should also not appear in the declaration of a function.


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.