Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
378 views
in Technique[技术] by (71.8m points)

c - What does __inline__ mean ?

I am trying to learn C. Reading through some code, I came across a line like this:

__inline__ void () ...

What does the __inline__ mean?. How does putting that word in front of a function make it different?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

__inline__ is a non-standard extension. Typically, it tells the compiler: "inline this function", but being a non-standard extension we can't say with certainty unless we know which compiler this is on.

To inline is to remove the function call and place it's contents directly where the call would be made. This often removes the overhead of calling a function. It is not always optimal, because of code bloat (code getting too big and not fitting into cache), so most compilers will ignore all inline directives and do what they feel is best. This is a good thing. We humans are very poor at that kind of stuff, and it's usually considered bad practice to tell the compiler how to do its job.

Inlining is an important optimization, especially with the presence of helper functions. Imagine a function that returned the smaller of two ints:

int min(int x, int y)
{
    return (x < y) ? x : y;
}

If I used this function in my code, it would be an enormous waste of time to actually make a function call, here. If I had:

int a = /* some calculation */;
int b = /* some other calculation */;

int minCalc = min(a, b);

And the compiler inlined that function, the code would become:

int a = /* some calculation */;
int b = /* some other calculation */;

int minCalc = (a < b) ? a : b;

Which removes the overhead of calling a function. From here, even more optimizations can be made as the compiler gets to work directly with the code that would have normally been hidden behind a function call. As you can see, if I have a big function and I force the compiler to inline it everywhere, the code size could grow very large very fast, and would actually hinder execution speed.

There is a standard inline keyword which was used to indicate to the compiler a function should be inlined, but nowadays most compilers don't even acknowledge it as a hint to inline the function.

There is an important side-effect of inline, though, and this can be useful. If a function is marked as inline, multiple definitions of the same function across multiple translation units is not an error. Instead, a single function definition is selected and the others are thrown out, and assumed to be the same (it's up to you to make sure this is actually okay!). This allows you to define a function within a header file without risking ODR violation errors.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...