I haven't been able to fine a very satisfying answer to this, but, what does declaring a function as "Inline" accomplish?
Is it useful? When would I want it? I just don't get it.
Erik
I haven't been able to fine a very satisfying answer to this, but, what does declaring a function as "Inline" accomplish?
Is it useful? When would I want it? I just don't get it.
Erik
Specifying inline for a function gives the compiler a hint that this function would be a good candidate for inlining. Basically all inlining does is place the code for the function where it is called from. This might seem silly but take the following function for example:
When you call this function you have to push the integer argument on the stack, perform the operation pop the return address off, blah blah, etc. This is commonly known as "function overhead". If you inlined the function you wouldnt' have to worry about that because whenever you called MySquare it would just put (num * num) right there. Think of it kind of like a macro.Code:int MySquare( int num ) { return num * num; }
When do we need it? When a function is called frequently that has only a few lines in it. You can never inline recursive functions and there are a lot of functions that even if you specify as inline will not be inlined by the compiler. You can search google and read up on other people's explanations.
"...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers
>> You can never inline recursive functions...
Some compilers support it
gg
Okay okay, I know some compilers support it. I shouldn't have said 'never'. I was hoping no one would call me out on that.
gg indeed sir.
"...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers
My best code is written with the delete key.
Ummm... I hope all of this is true... I don't have my books with me...
Epo, there is a good reason why this is difficult for you to grasp. You are studying a high-level language, and you haven't studied Assembly language, or how microprocessors execute machine code.
Assuming that the compiler takes the "hint", or "suggestion": Inline changes the structure of the compiled machine code. It does not change what you program does. It might make your program run faster!
Normally, the machine code for a function is stored is in it's own area of memory. (A pointer-to-a-function wiill point-to that memory.) ...Something like the way you write your C++ code, with function definitions at the end of the source file, or in their own file.
So, when the program is running and the function is called, the program-flow has to "jump-to", (aka branch-to) the function. This is where the stack overhead comes-in. The information about where to come back to, etc. has to be saved on the stack.
The machine code for an inline function will be copied right in-line (in sequence) with the rest of the code. Program-flow doesn't have to jump anywhere... As-if you didn't call a function at all. ...Something like if you put all of your code in main() and didn't call any other functions.
This means that if you call your function from 10 different places in your code, the machine-code for that inline function will be repeated 10 times. Your exe file will be bigger than a non-inline version.
Last edited by DougDbug; 06-17-2004 at 07:32 PM.
I remember this this post.
Okay, thank you all so far, it's actually making more sense
Say I have three files, Other.h, Other.cpp, and Main.cpp
In Other.h I place:
In Other.cpp I place:Code:inline MySquare (int Num);
And in Main.cpp:Code:#include "Other.h" inline MySquare (int Num) { BLAH = Num*Num }
Upon the ending of such a program (I kept it as simple as I could), would BLAH = 25 then?Code:#include "Other.h" int main (void) { int BLAH; MySquare(5); return 0; }
>would BLAH = 25 then?
No, inline functions follow scope rules so BLAH would be an undeclared identifier.
My best code is written with the delete key.
Actually the keyword inline is just a type modificator as you can use:
and it will print 5 on-screen.Code:#include <stdio.h> inline int sum(int a,int b) { return a+b; } void main() { int a=2,b=3,c; c=sum(a,b); printf("%i\n",c); }
>#include <stdio.h>
This is deprecated in C++, you should use <cstdio> and account for namespace std if you want printf.
>void main()
void main is wrong in both C and C++. It always has been. main returns an int.
>Actually the keyword inline is just a type modificator
Well, technically it's a function specifier.
>and it will print 5 on-screen.
I'm not sure I understand the point of your example.
My best code is written with the delete key.
Okay, this is a tangent that has nothing to do with the original question of this thread BUT.........what's wrong with using void main (void)? If you don't care about main's return why should you have to have a return statement that does nothing?
hmm... how exactly does inlining a recursive function work?
It is not standard.what's wrong with using void main (void)? If you don't care about main's return why should you have to have a return statement that does nothing?
Other than that, people talk about the return value being used by the operating system etc.
Look up a C++ Reference and learn How To Ask Questions The Smart WayOriginally Posted by Bjarne Stroustrup (2000-10-14)
>what's wrong with using void main (void)?
You just don't know what will happen. The C++ standard simply states "It shall have a return type of int". When the standard says "shall", it means there are no exceptions and failure to meet the requirement is undefined. A program that's undefined is very Very VERY bad. Let's just say that anything is allowed to happen, including a sadistic system erasing your harddrive.
>If you don't care about main's return why should you have to have a return statement that does nothing?
That argument has no foundation anymore. Standard C++ allows one to omit the explicit return statement from main and a success value will be returned behind the scenes. At that point there's no reason to void your main, it doesn't save keystrokes and invokes undefined behavior. It buys you nothing.
My best code is written with the delete key.