The difference between a macro and a function with parameters
? Macro substitution does not take up a run time, only the compile time, and the function call takes up the elapsed time (allocation Unit, reservation field, value Pass, return), so each execution is loaded so it is slower to execute.
? When defining a macro, do not type a space between the macro and its arguments, because when the macro is replaced, you inadvertently play the space as part of the macro.
? In the macro definition, each parameter is enclosed in parentheses, and the entire result is also enclosed in parentheses (for a macro of a single expression, you can use parentheses (), the compound statement defined by the macro can use {}, and theLinux kernel has a better macro definition, do{...} while (0)) to prevent problems that may occur when a macro is used in a larger expression.
? Many times, the macro expansion after the source program long, because each expansion of the program to grow, but the implementation of a bit faster (this is not absolute, when there are a lot of macro expansion, the target file is very large, run time when the system changes the page frequently, the efficiency will be low). The function call does not cause the source program to grow longer.
? When a function is called, the value of the argument expression is first calculated, and then the parameter is brought in. Using a macro with a parameter is simply a simple character substitution.
? function calls are handled at run time by the program, allocating temporary memory units, while macro expansion is done at compile time, no memory units are allocated at the time of deployment, no value is passed, and there is no concept of "return value ".
? The type is defined for the arguments and parameters in the function, and the type requirements are consistent, such as inconsistent, type conversion, and the macro does not have a type problem, the macro name is not a type, its parameters are no type, just a symbol, expand with the specified character. When a macro is defined, the string can be any type of data.
? The calling function can only get a return value, and there is a return type, and the macro has no return value and return type, but a macro can try to get several results.
? There are bugs in the function body that can break point debugging in the function body. If there is a Bug in the macro body, you cannot debug the macro at the time of execution, that is, you cannot drill into the inside of the macro.
? macros in C + + cannot access private members of an object, but member functions can.
? The definition of a macro is easy to produce two semantics, such as: definition #define S (a) (a) * (a), Code S (a++), macro expansion into (a++) * (a++) This is known to all, in different compilation environment will have different results.
The difference between inline functions and macros (advantages of inline functions)
the difference between an inline function and a macro is that a macro is replaced with a macro by a preprocessor, 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 like you would call a function, without worrying about some of the problems that might arise from dealing with macros.
We can use inline to define inline functions, but any function defined in the Description section of the class is automatically considered an inline function. (Of course, the identification of the inline function is a complex task for the compiler, and the successor may have a specific discussion)
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.
Inline functions improve execution efficiency by avoiding the overhead of being called, especially if it is optimized by the compiler by calling ("procedural Integration ").
How to choose whether to use a macro or a function: You can choose a macro in the following cases, preferably a function
2 Generally speaking, it is more appropriate to use macro to represent short expressions.
2 Consider the use of macros, or inline functions, when you consider efficiency.
2 in header File Protection (prevent duplicate include compilation), #ifdef in conditional compilation ,#if defined and assert implementation
[to] the difference between a macro definition and a function call