Macro and inline functions (regular interview)

Source: Internet
Author: User


First remember the difference between the two. The difference between inline functions and macros is,

(1) macros are replaced by preprocessors, while inline functions are implemented through compiler control.

(2) inline functions can access private member functions of objects. inline functions are the most widely used in C ++ and should be used to define access functions.
(This is why the default member function of a common class is inline)

Reprinted: http://blog.csdn.net/fisher_jiang/article/details/2472210

Part 1: Macro

Why use Macros?
Because the function call must transfer the execution sequence of the program to an address stored in the memory of the function. After the program content of the function is executed, return to the place before the function is executed. This transfer operation requires that the on-site address should be stored and remembered before the transfer is executed. After the transfer, the on-site address should be restored and the operation should be continued based on the original storage address. Therefore, function calling requires a certain amount of time and space overhead, which affects its efficiency.
Macros only expand the code in the pre-processing area without additional space and time overhead. Therefore, calling a macro is more efficient than calling a function.
However, there are many unsatisfactory aspects of Macro.
1. Macros cannot access private members of objects.
2. The definition of macros is prone to ambiguity.
For example:
# Define square (x) (x * X)
We call it with a number, square (5). It seems that there is no error. The result returns 25, which is correct, but if we use squre (5 + 5) the expected result is 100, while the macro call result is (5 + 5*5 + 5) and the result is 35. This is obviously not the expected result. To avoid these errors, add brackets to macro parameters.
# Define square (x) * (x ))

Part 2: inline functions
From the above, we can see that macro has some unavoidable problems. How can we solve them?
An inline function is a function that inserts a code into the caller's code. Like the # define macro, inline functions improve execution efficiency by avoiding the overhead of calls, especially when they can be optimized by the compiler by calling ("procedural integration.
Inline functions are similar to macros, but the difference is that macros are replaced by pre-processors, while inline functions are implemented through compiler control. In addition, inline functions are real functions, but they are expanded like macros when needed. Therefore, the parameter pressure stack of the function is removed, reducing the call overhead. You can call inline functions like calling functions without worrying about macro processing issues.
Declaring an inline function looks very similar to a common function:
Void F (int I, char C );
When you define an inline function, add the inline keyword before the function definition and put the definition into the header file:
Inline void F (int I, char C)
{
//...
}
Inline functions are valid only when they are declared together with the function body.
Declarations like this inline function (int I) have no effect. The Compiler just uses the function as a general function declaration and we must define the function body.
Inline int function (int I) {return I * I ;}
This defines an inline function. We can call it as a common function. However, the execution speed is indeed faster than that of common functions.
Of course, inline functions also have some limitations. That is, the Execution Code in the function cannot be too much. If the function body of the inline function is too large, the general compiler will discard the inline method and call the function in the normal way. In this way, the efficiency of inline functions is the same as that of normal functions.
In the above two features, we can replace the pre-processing macro With 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.