macro definition and inline functions

Source: Internet
Author: User

1. Rules and usage resolution for macro definitions
(1) The parsing rule of a macro definition is that it is replaced by a preprocessor during the preprocessing phase, and the substitution is unchanged.
(2) The macro definition substitution is performed recursively until the replaced value itself is no longer a macro.
(3) A correct macro definition of the formula itself is divided into 3 parts: The first part is #dedine, the second part is the macro name, all the rest is the third part.
(4) A macro can take parameters, called a parameter macro. The use of a parameter macro is very similar to the parameter function, but there are some differences in usage. When defining a parameter macro, each argument must be enclosed in parentheses when it is referenced in the macro body, and the parentheses are the last, and the brackets are integral.

Macro Definition Example 1:max macro, find the larger one of 2 numbers
#define MAX (A, B) ((a) > (b))? (a): (b))
The essential:
1th: Think of using the three mesh operator to complete.
2nd: Note the use of parentheses

Macro definition example 2:sec_per_year, which defines how many seconds in a year are represented by a macro
#define SEC_PER_YEAR (365*24*60*60ul)
The essential:
1th: When a number appears directly in the program, it is the type by default is int
2nd: How many seconds a year, this number just exceeds the range of the int type storage

Difference between a parameter macro and a parameter function (macro-defined defect)
(1) The macro definition is processed during preprocessing, and the function is processed during compilation. This difference brings the real difference is: The macro definition is ultimately in the call macro where the macro body is expanded, and the function is in the call function to jump to the function to execute, after execution and then jump back.
Note: The biggest difference between macro definitions and functions is that the macro definition is expanded in place, so there is no call overhead, and the function is a jump execution and then returns, so the function has a larger invocation cost. So the advantage of a macro definition and a function is that there is no call overhead, no pass-through overhead, so the function body is very short (especially when there is only one sentence) and can be replaced with a macro definition, which is efficient.
(2) An important difference between a parameter and a parameter function is that the macro definition does not check the type of the argument, the return value does not come with a type, and the function has an explicit parameter type and a return value type. When we call the function, the compiler will do a static type check on the parameters, and if the compiler finds out that our actual arguments and parameter declarations are different, we will report a warning or an error.
Note: The programmer does not use the function to worry about type mismatch because the compiler will check if the compiler will be called; When using macros, the programmer must pay attention to the fact that the actual parameters and the macro would like to have the same type of parameter, otherwise it may compile without error but run incorrectly.
Summary: Macros and functions have their merits and demerits. In general, if the code is more functional and does not affect efficiency, but for those with only one or two sentences the cost of the function is too large, suitable for use with the parameter macro. But there are drawbacks to using a parametric macro: The parameter type is not checked.

  

2. Inline functions and inline keywords
(1) Inline functions are implemented by adding the inline keyword before the function definition.
(2) The inline function is essentially a function, so there is the advantage of the function (the inline function is the compiler is responsible for processing, the compiler can help us do the static type check of parameters); So it is almost possible to think that an inline function is a macro with parameter static type checking.
(3) When the function body within our function is very short (for example, there are only one or two sentences), we want to use the compiler's parameter type checking to debug the error, and I also want to use inline functions when there is no call overhead.

macro definition 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.