inline functions

Discussion in 'C++' started by niklaus@gmail.com, Aug 29, 2006.

  1. Guest

    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 ).
     
    , Aug 29, 2006
    #1
    1. Advertising

  2. 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
     
    Victor Bazarov, Aug 29, 2006
    #2
    1. Advertising

  3. Phlip Guest

    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!!!
     
    Phlip, Aug 29, 2006
    #3
  4. David Harmon Guest

    On Tue, 29 Aug 2006 19:09:52 GMT in comp.lang.c++, "Phlip"
    <> 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.
     
    David Harmon, Aug 29, 2006
    #4
  5. Rolf Magnus Guest

    Victor Bazarov wrote:

    > 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.
     
    Rolf Magnus, Aug 30, 2006
    #5
  6. Phlip Guest

    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!!!
     
    Phlip, Aug 30, 2006
    #6
  7. 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
     
    Frederick Gotham, Aug 30, 2006
    #7
  8. 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.
     
    Andreas Pagel, Sep 7, 2006
    #8
  9. Guest

    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
    "
     
    , Sep 7, 2006
    #9
  10. Earl Purple Guest

    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.
     
    Earl Purple, Sep 7, 2006
    #10
  11. 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
     
    Frederick Gotham, Sep 8, 2006
    #11
  12. 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.
     
    Andreas Pagel, Sep 8, 2006
    #12
  13. 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.
     
    Andreas Pagel, Sep 8, 2006
    #13
  14. 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
     
    Frederick Gotham, Sep 8, 2006
    #14
  15. 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.
     
    Andreas Pagel, Sep 8, 2006
    #15
  16. 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
     
    Frederick Gotham, Sep 8, 2006
    #16
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Abhi
    Replies:
    2
    Views:
    759
    E. Robert Tisdale
    Jul 3, 2003
  2. Replies:
    3
    Views:
    484
  3. Daniel Vallstrom
    Replies:
    2
    Views:
    2,009
    Kevin Bracey
    Nov 21, 2003
  4. jamihuq

    converting inline functions to C functions

    jamihuq, May 16, 2006, in forum: C Programming
    Replies:
    7
    Views:
    362
  5. Rahul
    Replies:
    3
    Views:
    476
    James Kanze
    Feb 28, 2008
Loading...

Share This Page