C-language inline function

Source: Internet
Author: User

Original link: http://blog.csdn.net/yuan1125/article/details/6225993

1 inline is just a compiler recommendation, and the compiler does not necessarily have to expand the inline function.

For example:

inline function Address Reference

Inline is used in recursive functions

2 inline must be used for function definitions, and inline does not work for function declarations

For example:

Inline Max (int,int); No inline effect

Only inline max (int a,int b)

{

......

}

Only use it.

3 inline defines functions and macro definitions just as local files are visible. Therefore, it is recommended that the function defined by the inline be placed in the header file

4 The rules of the inline function in GCC

1. Static inline---> compiler is expanded locally.

2. Inline---> Local expansion, field-generated independent assembly code for the inline function

3. extern inline---> does not generate separate assembly code.

Feature 1. The call to this function is processed into an external reference, even if it is applied by a pointer or is recursive, and does not cause the compiler to generate a sink encoding for it.

Feature 2.extern inline functions allow the same name as an external function, meaning that it is also legal to define an extern inline function with the same names in the presence of an externally defined global library function.

The use of extern inline:

Within the C file of a library function, define a generic version of the library function foo:

MYLIB.C:

void Foo ()

{

...;

}

And then in its header file, define (note not declaration!) ) A version that implements the same Exterin inline:

Mylib.h:

extern inline Foo ()

{

...;

}

So when other files are going to use this library function, the compiler will use the extern inline version of the header file as long as the include Mylib.h, where it can be expanded inline.

In the case where it cannot be expanded (such as a function pointer reference), the compiler will refer to the MYLIB.C version of the standalone compilation.

That looks like a function that can be inline externally, so this should be the origin of the extern inline meaning of gcc.

C-language inline function (GO)

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.