| Topic: |
DEVELOP > c-Plus-Plus |
| User: |
"" |
| Date: |
29 Aug 2006 01:41:27 PM |
| Object: |
inline functions |
Hi,
I have a doubt regarding inline functions.
1) When does the inline of function happen. During the preprocessing
stage or just before the object code is produced.
Can we see the source code after we have function inline just like cpp
abc.C shows me the expanded macros on linux.
2) Is it the source code that is inlined or the assembly code (object
code).
3) If i have a function like this
inline int add(int a,int b)
{
return a++ + ++b;
}
int main()
{
int k ,m,n;
m=3;
n=4;
k= add(m,n);
}
does something like this happen ? * i am not sure about this *
int main()
{
int k ;
m=3;
n=4;
tmp1=m;
tmp2=n;
k=( tmp1++ + ++tmp2);
}
How do i see something like this (i mean expanded version of inline
function ).
.
|
|
| User: "Victor Bazarov" |
|
| Title: Re: inline functions |
29 Aug 2006 02:05:47 PM |
|
|
wrote:
1) When does the inline of function happen. During the preprocessing
stage or just before the object code is produced.
In between.
Can we see the source code after we have function inline just like cpp
abc.C shows me the expanded macros on linux.
No, inlining is not macro substitution. It's done during code generation
so you should be able to see the results in the assembly listing if your
compiler is capable of producing one.
2) Is it the source code that is inlined or the assembly code (object
code).
Machine code.
3) If i have a function like this
inline int add(int a,int b)
{
return a++ + ++b;
}
int main()
{
int k ,m,n;
m=3;
n=4;
k= add(m,n);
}
does something like this happen ? * i am not sure about this *
int main()
{
int k ;
m=3;
n=4;
tmp1=m;
tmp2=n;
k=( tmp1++ + ++tmp2);
Something like this, yes.
}
How do i see something like this (i mean expanded version of inline
function ).
You need to look at the assembly listing.
V
--
Please remove capital 'A's when replying by e-mail
I do not respond to top-posted replies, please don't ask
.
|
|
|
| User: "Rolf Magnus" |
|
| Title: Re: inline functions |
30 Aug 2006 08:40:28 AM |
|
|
Victor Bazarov wrote:
niklaus@gmail.com wrote:
1) When does the inline of function happen. During the preprocessing
stage or just before the object code is produced.
In between.
Can we see the source code after we have function inline just like cpp
abc.C shows me the expanded macros on linux.
No, inlining is not macro substitution. It's done during code generation
so you should be able to see the results in the assembly listing if your
compiler is capable of producing one.
2) Is it the source code that is inlined or the assembly code (object
code).
Machine code.
It's probably somewhere in between (as you mentioned in 1), in an
intermediate compiler-internal representation. That way, the inlined code
can benefit from the optimizations that are done before code generation.
.
|
|
|
|
|
| User: "Phlip" |
|
| Title: Re: inline functions |
29 Aug 2006 02:09:52 PM |
|
|
niklaus wrote:
I have a doubt regarding inline functions.
1) When does the inline of function happen. During the preprocessing
stage or just before the object code is produced.
'inline' does not mean opcodes are inserted in-line with their calling
functions.
It only means that a compiler may see the inline function's body many times
in each "translation unit". Otherwise, the compiler must only see one body
for each function. (This is why template functions and class member
functions defined inside the function get an automatic 'inline'.)
Once the compiler knows all the functions, it is free to inline and
out-of-line them as it likes, based on its optimization system. Some
out-of-line functions might be inlined, and some inline functions might be
out-of-lined. The compiler can do anything it likes so long as the resulting
opcodes' behaviors are indistinguishable from C++'s language definition.
You should _never_ make something inline just because you guess it must be
faster. Google "premature optimization". Some teams write everything
out-of-line, as a rule, until profiling reveals something should be inline.
However, you will still get better performance by profiling and changing
entire algorithms, not just by tweaking small details.
Can we see the source code after we have function inline just like cpp
abc.C shows me the expanded macros on linux.
Inline are not macros, which are a text substitution mechanism added
literally on top of the C++ language.
You may disassemble the output, and whether you get inlines where you expect
may even depend on your compiler settings.
2) Is it the source code that is inlined or the assembly code (object
code).
Object code.
3) If i have a function like this
inline int add(int a,int b)
{
return a++ + ++b;
}
int main()
{
int k ,m,n;
m=3;
n=4;
k= add(m,n);
}
does something like this happen ? * i am not sure about this *
int main()
{
int k ;
m=3;
n=4;
tmp1=m;
tmp2=n;
k=( tmp1++ + ++tmp2);
}
Maybe (modulo a couple errors in your code). What's more important is you
can't tell what happened to add(). Maybe it went away, or maybe it's there.
How do i see something like this (i mean expanded version of inline
function ).
You never can. You can only disassemble your code, or run a debugger on it.
(And compiling for debugging often changes the optimizations. This is
entirely platform specific, so it's off-topic here.)
If you are curious, read a book like /Inside the C++ Object Model/ by
Lippman. However, if you are new to programming, you should instead be
researching details like /Design Patterns/ or unit testing. Those will make
you a faster programmer.
C++ programs are already very fast. An inline here and there will not make a
program faster.
--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
.
|
|
|
| User: "David Harmon" |
|
| Title: Re: inline functions |
29 Aug 2006 05:58:11 PM |
|
|
On Tue, 29 Aug 2006 19:09:52 GMT in comp.lang.c++, "Phlip"
<phlipcpp@yahoo.com> wrote,
It only means that a compiler may see the inline function's body many times
in each "translation unit". Otherwise, the compiler must only see one body
for each function.
Well, _once_ in each translation unit; many times over the entire
program.
.
|
|
|
| User: "Phlip" |
|
| Title: Re: inline functions |
30 Aug 2006 09:18:34 AM |
|
|
David Harmon wrote:
It only means that a compiler may see the inline function's body many
times
in each "translation unit". Otherwise, the compiler must only see one body
for each function.
Well, _once_ in each translation unit; many times over the entire
program.
g++ concurs; thanks.
My prose style was relaxed, but it did indeed reveal I didn't know that. I
never needed to learn it because I always follow immaculate coding styles.
--
Phlip
http://c2.com/cgi/wiki?ZeekLand <-- NOT a blog!!!
.
|
|
|
|
|
| User: "Frederick Gotham" |
|
| Title: Re: inline functions |
30 Aug 2006 12:24:43 PM |
|
|
Phlip posted:
You should _never_ make something inline just because you guess it must
be faster.
Not even the following?
unsigned inline Square(unsigned const i)
{
return i*i;
}
Granted, no competant programmer would use this function, but you get the
idea.
--
Frederick Gotham
.
|
|
|
| User: "Andreas Pagel" |
|
| Title: Re: inline functions |
07 Sep 2006 08:07:19 AM |
|
|
Frederick Gotham wrote:
unsigned inline Square(unsigned const i)
{
return i*i;
}
Granted, no competant programmer would use this function,
You've got me curious there - why do you say that? I've used exactly such a
function (except for making the argument a template rather than unsigned).
Andreas.
.
|
|
|
| User: "Frederick Gotham" |
|
| Title: Re: inline functions |
08 Sep 2006 09:45:01 AM |
|
|
Andreas Pagel posted:
unsigned inline Square(unsigned const i)
{
return i*i;
}
Granted, no competant programmer would use this function,
You've got me curious there - why do you say that? I've used exactly
such a function (except for making the argument a template rather than
I rather simply write:
x*x
--
Frederick Gotham
.
|
|
|
| User: "Andreas Pagel" |
|
| Title: Re: inline functions |
08 Sep 2006 10:52:13 AM |
|
|
Frederick Gotham wrote:
Andreas Pagel posted:
unsigned inline Square(unsigned const i)
{
return i*i;
}
Granted, no competant programmer would use this function,
You've got me curious there - why do you say that? I've used exactly
such a function (except for making the argument a template rather than
I rather simply write:
x*x
Ok, but sometimes you might want to square a value that isn't available in a
variable. What about Square(a++)? You'd have to write a++ * a, already it
looks a little messier. What about Square((a+b+c+d+e)/5)? Now you'd have
to repeat a long expression.
Or what about Square(fn(...))? Now you'd need to introduce a temporary
variable.
Andreas.
.
|
|
|
| User: "Frederick Gotham" |
|
| Title: Re: inline functions |
08 Sep 2006 11:12:28 AM |
|
|
Andreas Pagel posted:
Ok, but sometimes you might want to square a value that isn't available
in a variable. What about Square(a++)? You'd have to write a++ * a,
already it looks a little messier. What about Square((a+b+c+d+e)/5)?
Now you'd have to repeat a long expression.
Or what about Square(fn(...))? Now you'd need to introduce a temporary
variable.
Oh I see your point, I hadn't thought of that. In such cases, I'd probably
do:
template<class T>
T Square(T const a, T const b)
{
return a * b;
}
(By the way,
a++ * a
invokes undefined behaviour.)
--
Frederick Gotham
.
|
|
|
| User: "Andreas Pagel" |
|
| Title: Re: inline functions |
08 Sep 2006 02:06:24 PM |
|
|
Frederick Gotham wrote:
Andreas Pagel posted:
Ok, but sometimes you might want to square a value that isn't available
in a variable. What about Square(a++)? You'd have to write a++ * a,
already it looks a little messier. What about Square((a+b+c+d+e)/5)?
Now you'd have to repeat a long expression.
Or what about Square(fn(...))? Now you'd need to introduce a temporary
variable.
Oh I see your point, I hadn't thought of that.
Fair enough. I was surprised by your initial comment and wondered if there
was some obscure point I'd overlooked...
In such cases, I'd probably
do:
template<class T>
T Square(T const a, T const b)
{
return a * b;
}
Except that isn't a square function any more, but a generic multiplication.
I can't see that that buys you anything over just writing "a * b".
(Regarding the undefined behaviour - I noticed my error soon after posting.)
Andreas.
.
|
|
|
| User: "Frederick Gotham" |
|
| Title: Re: inline functions |
08 Sep 2006 02:24:30 PM |
|
|
Andreas Pagel posted:
Except that isn't a square function any more, but a generic
multiplication. I can't see that that buys you anything over just
writing "a * b".
I wrote quickly and sloppily... should have written:
template<class T>
T Square(T const val)
{
return val*val;
}
--
Frederick Gotham
.
|
|
|
|
|
|
| User: "Andreas Pagel" |
|
| Title: Re: inline functions |
08 Sep 2006 11:11:49 AM |
|
|
I wrote:
What about Square(a++)? You'd have to write a++ * a, already it
looks a little messier.
Of course, not only messier, but wrong. No need for a bunch of follow-ups
pointing this out...
Andreas.
.
|
|
|
|
|
|
|
| User: "" |
|
| Title: Re: inline functions |
07 Sep 2006 09:30:30 AM |
|
|
Frederick Gotham wrote:
Phlip posted:
You should _never_ make something inline just because you guess it must
be faster.
Not even the following?
unsigned inline Square(unsigned const i)
{
return i*i;
}
Granted, no competant programmer would use this function, but you get the
idea.
Indeed. It takes 7 extra characters, modern compilers can still inline
it, and there
is reason from profiling to suggest you should actually inline it.
Now, if a method ends up implicit inline (defined in class definition)
I don't care.
The added advantage of saying to maintainters "just a trivial accessor
method,
the real logic is in Foo::Bar()" is usually bigger than the performance
advantage.
Regards,
Michiel Salters
"
.
|
|
|
| User: "Earl Purple" |
|
| Title: Re: inline functions |
07 Sep 2006 11:42:20 AM |
|
|
wrote:
Not even the following?
unsigned inline Square(unsigned const i)
{
return i*i;
}
Granted, no competant programmer would use this function, but you get the
idea.
Indeed. It takes 7 extra characters, modern compilers can still inline
it, and there
is reason from profiling to suggest you should actually inline it.
You would inline it so you can stick it in a utilities header somewhere
and include that header without having to link against something.
Of course you would probably make it a template, in which case you
would not need to use the inline keyword.
.
|
|
|
|
|
|
|

|
Related Articles |
|
|