About the inline in C

Source: Internet
Author: User

In C, in order to solve some of the frequently called small functions of large consumption of stack space or called stack memory, especially the introduction of the inline modifier, expressed as an inline function.
Stack space refers to the local data that is placed in the program is the memory space of the function data, in the system, the stack space is limited, if the frequent use of a large amount of space caused by insufficient stack of program error, the function of the dead loop recursive call the end result is to cause the stack memory space exhaustion.
Let's take a look at an example

[C-sharp]View PlainCopy
  1. #include <stdio.h>
  2. function defined as inline: inline function
  3. Inline char* dbtest (int a) {
  4. return (i% 2 > 0)?  " odd": "occasional";
  5. }
  6. int main ()
  7. {
  8. int i = 0;
  9. For (i=1; i <; i++) {
  10. printf ("i:%d parity:%s/n", I, dbtest (i));
  11. }
  12. }

The above example is the use of the standard inline function, the benefits of using the inline modification we do not see the surface, in fact, the internal work is in each for the inside of the loop of any call dbtest (i) to replace the place (i%2>0)? " Odd ":" Even "this avoids the frequent calls to the function of the stack memory duplication of the cost of opening up.
The use of inline is limited, and inline is only suitable for the simple use of the number of the code in the body, not to include complex structure control statements such as while, switch, and cannot inline the function itself cannot be a direct recursive function (itself also calls its own function).
Add:

The inline function is just a recommendation to the compiler's recommendations, so the final can be really inline, see the meaning of the compiler, if it thinks that the function is not complex, can be expanded at the point of call, it will be really inline, not to say that the inline will be inline, the declaration of inline is only a suggestion.
Second, because the inline function is to be expanded at the point of invocation, the compiler must everywhere define the inline function, otherwise it becomes a call to a non-inline function. Therefore, this requires that each file that invokes the inline function has the definition of the inline function.

Therefore, it is appropriate to put inline functions in the header file, saving you the hassle of implementing one for each file. And so the declaration is consistent with the definition, in fact, if the inline function is implemented once in each file, then it is best to ensure that each definition is the same, otherwise it will cause undefined behavior, that is, If not all the definitions in each file are the same, then the compiler expands which one depends on the specific compiler. Therefore, it is best to place the inline function definition in the header file.
The member functions in a class are inline by default, and if the function is given within the class when the class is defined, it is certainly best. If the member function definition is not given in the class and you want to inline the function, then add inline to the class, otherwise it is not considered inline.


For convenience, the inline function is defined directly when it is declared, and placed in the header file. So that other files contain the header file, and in each file there is a definition of the inline function. It can be inline.

About inline in C

Related Article

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.