Inline restrained functions

Source: Internet
Author: User
Macro and inline functions (regular interview)
Favorites

Part 1: Macro
Why use Macros?
Because the function call must transfer the execution sequence of the program to the memory stored in 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 site be saved and the address for execution should be remembered before the transfer, and the domain name should be restored after the transfer.
Field, and continue to execute according to the original save 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)
Me
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) for calling, 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, I.
Is to 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.
Internal
The difference between a function and a macro is that a macro is replaced by a Preprocessor, while an inline function is implemented by a compiler. In addition, the inline function is a real function, but when necessary
The function is expanded like a macro. 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.

**************************************** **************************************** **************************************** *********

From the perspective of inline, placing it in function declaration should also be useless.
Of: inline will only affect the function in Translation
Unit (which can be simply understood as a C source code file) does not work as long as the inline attribute is out of this range. Therefore, the inline keyword should not appear in the function declaration.
Does not have any effect, and sometimes may cause compilation errors. (when sys/compiler. H is included, the inline keyword in the declaration cannot be compiled.
);

The Inline keyword is only recommended for the compiler to perform inline expansion, rather than forcing. In the GCC compiler, if the compilation optimization is set to-O0, even inline functions will not be expanded inline unless forced inline (_ attribute _ (always_inline) is set ))) attribute.

1. GCC inline
GCC has made its own extensions to the inline of C language, and its behavior is quite different from that in the c99 standard.

1.1. Static inline
Static gcc
Inline definition is easy to understand: You can regard it as a static function with the inline attribute added. Most of the functions are the same as normal static functions.
However, when calling such a function, GCC will compile the code at its call without generating an independent assembly code for the function. Except for the following situations:
When the function address is used. For example, the function is indirectly called through the function pointer. In this case, you have to generate an independent assembly code for the static inline function, otherwise it does not have its own address.
In other situations, for example, the function itself has the behavior of Recursion Calling itself.
The static inline function is the same as the static function. Its definition range is local, that is, multiple definitions with the same name can be defined in the Program (as long as they are not in the same file ).
Note:
The static inline of GCC is consistent with the static inline of c99. Therefore, this definition can be safely used without compatibility issues.
Key points:
Compared with static functions, static inline of GCC only recommends that the compiler conduct Inline expansion during calling;
GCC will not specifically generate Independent Assembly codes for the static inline function unless it is necessary to generate the code (such as using function pointers and recursive calls );
The static inline function of GCC can only work within the file range. 1.3. extern inline
Static gcc
Inline and inline are both well understood: they all seem to have added inline attributes to common functions. But this extern
Inline cannot be taken for granted as an extern Function + inline attribute. In fact, the GCC extern
Inline is odd: an extern
Inline functions will only be inline, but will never generate Independent Assembly codes! Even using pointer applications or recursive calls won't let the compiler generate assembly code for it. In this case
The call is processed as an external reference. In addition, extern
The inline function can have the same name as the external function. That is, if an external-defined global library function exists, an extern with the same name can be defined.
The inline function is also valid. The following example describes the features of extern inline:
Foo. C:
Extern inline
Int Foo (int)
{
Return (-);
}
Void func1 ()
{
...;
A = Foo (a); ①
P_foo = Foo; ②
B = p_foo (B); ③
}
In
In this file, GCC will not generate the assembly code of the foo function. In the call point ① In func1, the compiler will inline compile the foo function defined above, which is similar to the normal
Inline Function. This is because such a call can be used for inline processing. In section ②, the address of the foo function is referenced. But note: the compiler will never be extern
The inline function generates Independent Assembly codes! Therefore, when a function address is required to be unavailable, the compiler has to process it as an external reference and link it to the external Foo function (
Write external function address ). In this case, if the global Foo function is not defined externally, an undefined Foo function error will be generated during the link.
Assume that a global function foo is also defined in another file:
Foo2.c:
Int Foo (int)
{
Return ();
}
That
In the above example, the next reference to the foo function address will be directed to the foo function defined in foo2.c during the link. That is to say: ① the result of calling the foo function is
A =-A, because it inline the foo function in Foo. C; and ③ The call result is B = B, because it actually calls the foo function in foo2.c!
Extern
The use of inline is very strange and rare, but it still has practical value. First, it can behave like a macro, and extern can be used in the file
The definition of the inline version replaces the library function of the external definition (the premise is that the file cannot be called inline); Second: it allows a library function to be inline whenever possible.
. For example:
In the c file of a library function, define a common version of library function libfunc:
Lib. C:
Void libfunc ()
{
...;
}
Then define it in the header file (note that it is not a declaration !) A version that implements the same exterin inline:
Lib. h:
Extern inline libfunc ()
{
...;
}
That
When other files need to use this library function, as long as Lib. H is included, the compiler will use extern in the header file where Inline expansion can be performed.
Inline version. When expansion fails (function pointer reference, etc.), the compiler will reference the self-compiled normal version in Lib. C. That is, it seems to be an external service.
So this should be the origin of the meaning of GCC's extern inline.
However, note that this is a cost: the implementation of global functions in the C file must be the same as extern in the header file.
The implementation of the inline version is exactly the same. Otherwise, the function performance will be inconsistent during direct and indirect calls in the previous example.
Important
Gcc
The use of the extern inline function is quite strange, and the scope of use is also very narrow: there is almost no need to use it. In c99, there is no extern
Therefore, we do not recommend that you use extern inline unless you understand the meaning of this usage and have enough reason to use it!
Key Point: GCC will never generate Independent Assembly codes for functions of extern inline.
The extern inline function can have the same name as the global function. It can replace the externally defined global function within the file range.
The application scope of the extern inline function is very narrow and the behavior is strange. It is not recommended to use

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.