Inline function

Hey.Guyz.Can anyone explain to me,in brief about 'inline function',it seem to be important in class,but am figure out nothing from it.
inline is a hint to the compiler to replace the function call with the code in the function
eg:
If the compiler inlines the function -It may not do so- this code:
1
2
3
4
5
6
7
8
9
10
inline void f()
{
    std::cout << "Foo Bar";
}

int main()
{
    f();
    return 0;
}
would give the same result as
1
2
3
4
5
int main()
{
    std::cout << "Foo Bar"; // what was in the function is where the function was called
    return 0;
}


http://www.parashift.com/c++-faq-lite/inline-functions.html#faq-9.1
an inline function is a function that is expanded at the time it is invoked, instead of being called.the reason to use an inline function is efficiency. it doesnt have the overhead other types of functionns haveof taxing the CPU with a series of instructions that need to be executed to set up function calls, pushing arguements into the stack, or return from the function, compined which can cost the CPU many cycles to perform.
Please note even though class member functions defined inside a class declaration are automatically considered inline - this is no excuse to write all member functions inside the class - it is very annoying.
No-one has really noted the drawbacks of inline.

Inline functions aren't always a good thing. They can indeed make the generated code faster by removing a few instructions, namely a couple of push and pops, but they also increase executable size. They also can make things slower. If you had this function
1
2
3
4
5
6
inline void inline_function(void) {
    int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p;
    float q, r, s, t, u, v, w, x, y, z;
    struct myStruct myObject;
    char* myCString;
}

(pretend I initialized those variables), you definitely wouldn't inline that. Why? Look how many variables it allocates on the stack. That would slow things down rather than speed them up, not to mention waste memory.

OK, so in practice you usually won't have 30 variables on the stack in a single function, but you might.

Bare in mind that sometimes inlining code can actually make executable sizes smaller.

I would say that as a rule of thumb, try only to inline functions that have 3-8 lines of code in them, or which you only call once or twice. That can help to speed up your code and maybe even make your executable smaller.

Like Bazzy said, though: inline is just a hint, not an instruction. The compiler ultimately decides.
Last edited on

(pretend I initialized those variables), you definitely wouldn't inline that. Why? Look how many variables it allocates on the stack. That would slow things down rather than speed them up, not to mention waste memory.


No it wouldn't. It would still speed it up. Whether or not the function is inlined, the compiler still must generate the code
to initialize the variables.

The _only_ drawback to inlining from a runtime standpoint is that it can increase the size of the executable.
@Jsmith,
Of course; you're right, my mistake. If you call the functions, they get initialized anyway. I see.

But surely inlining can't always increase speed, otherwise you'd just inline functions left right and centre.
increased code size = increased probability of cache misses = potentially slower code.

There comes a point where larger code becomes slower code. This is partly why inline is a hint -- if you get to this point, the compiler can stop inlining your functions because doing so will make things worse.

Some other minor drawbacks to inline:

- slower recompile time. Inline functions must be #included in every cpp file that uses them, so modify the function and you have to recompile everything.

- Inline functions can't be recursed
Thanks,guyz.I get it.
increased code size = increased probability of cache misses = potentially slower code.

I knew it! Inlining functions can slow code down!
There comes a point where larger code becomes slower code. This is partly why inline is a hint -- if you get to this point, the compiler can stop inlining your functions because doing so will make things worse.
The compiler won't detect that the program is getting too big. Unless you tell it to optimize for size, obviously.

- Inline functions can't be recursed
More accurately, recursive functions can't be inlined.

EDIT: Oh, also,
1
2
3
4
5
6
inline void inline_function(void) {
    int a, b, c, d, e, f, g, h, i, j, k, l, m, n, o, p;
    float q, r, s, t, u, v, w, x, y, z;
    struct myStruct myObject;
    char* myCString;
}

Creating a new stack frame that doesn't involve call and ret is relatively cheap. One variable or thirty takes the same time.
If the above function is C, it generates almost no code. If it's C++, there's a call to a constructor that probably does nothing.
Last edited on
Topic archived. No new replies allowed.