Inline Functions?

Discussion in 'C Programming' started by raashid bhatt, Aug 19, 2008.

  1. hi
    by the definition of inline functions
    "In computer science, an inline function is a programming language
    construct used to suggest to a compiler that a particular function be
    subjected to in-line expansion; that is, it suggests that the compiler
    insert the complete body of the function in every context where that
    function is used."



    ========================================
    int a;

    inline void func()
    {
    a = 50;
    }
    void __cdecl main()
    {

    func();
    func();

    }

    ===========================================

    so the complete body of function func() should actually be placed from
    where it is called ... ie this code is similar to

    ============================================
    int a;

    void __cdecl main()
    {

    a = 50;
    }
    ============================================

    but when i compile it i get undesired results

    PUSH EBP
    MOV EBP,ESP
    CALL FUNC
    ; THE FUNCTION SHOULD NOT BE CALLED RATHER THE WHOLE BODY OF FUNCTION
    SHOUDL BE PLACED HERE
    CALL FUNC // SAME HERE
    POP EBP
    RETN

    if i made func as inline then why is it called why not the body of
    func is placed at every place it called.

    Raashid Bhatt (a.k.a rash0r)
     
    raashid bhatt, Aug 19, 2008
    #1
    1. Advertising

  2. raashid bhatt

    santosh Guest

    raashid bhatt wrote:

    > hi
    > by the definition of inline functions
    > "In computer science, an inline function is a programming language
    > construct used to suggest to a compiler that a particular function be
    > subjected to in-line expansion; that is, it suggests that the compiler
    > insert the complete body of the function in every context where that
    > function is used."
    >
    >
    >
    > ========================================
    > int a;
    >
    > inline void func()
    > {
    > a = 50;
    > }
    > void __cdecl main()
    > {
    >
    > func();
    > func();
    >
    > }
    >
    > ===========================================
    >
    > so the complete body of function func() should actually be placed from
    > where it is called ... ie this code is similar to
    >
    > ============================================
    > int a;
    >
    > void __cdecl main()
    > {
    >
    > a = 50;
    > }
    > ============================================
    >
    > but when i compile it i get undesired results
    >
    > PUSH EBP
    > MOV EBP,ESP
    > CALL FUNC
    > ; THE FUNCTION SHOULD NOT BE CALLED RATHER THE WHOLE BODY OF FUNCTION
    > SHOUDL BE PLACED HERE
    > CALL FUNC // SAME HERE
    > POP EBP
    > RETN
    >
    > if i made func as inline then why is it called why not the body of
    > func is placed at every place it called.
    >
    > Raashid Bhatt (a.k.a rash0r)


    That's because inline (in C) is just a *suggestion* to the compiler to
    make calls to the decorated function as fast as possible. It is not
    mandatory that the compiler take your hint. Try turning on
    optimisations. Many compilers have specific switches for enabling
    inlining and customising the inlining behaviour. Reasearch your
    compiler documentation.

    PS. Drop the _cdecl attribute from the definition of main and change
    it's return type to int. _cdecl is not Standard and in most cases
    unnecessary as well.
     
    santosh, Aug 19, 2008
    #2
    1. Advertising

  3. raashid bhatt

    Luks Guest

    On 19 ago, 02:40, raashid bhatt <> wrote:
    > hi
    > by the definition of inline functions
    > "In computer science, an inline function is a programming language
    > construct used to suggest to a compiler that a particular function be
    > subjected to in-line expansion; that is, it suggests that the compiler
    > insert the complete body of the function in every context where that
    > function is used."
    >
    > ========================================
    > int a;
    >
    > inline void func()
    > {
    >         a = 50;}
    >
    > void __cdecl main()
    > {
    >
    >         func();
    >         func();
    >
    > }
    >
    > ===========================================
    >
    > so the complete body of function func() should actually be placed from
    > where it is called ... ie this code is similar to
    >
    > ============================================
    > int a;
    >
    > void __cdecl main()
    > {
    >
    >         a = 50;}
    >
    > ============================================
    >
    > but when i compile it i get undesired results
    >
    > PUSH EBP
    > MOV EBP,ESP
    > CALL FUNC
    > ; THE FUNCTION SHOULD NOT BE CALLED RATHER THE WHOLE BODY OF FUNCTION
    > SHOUDL BE PLACED HERE
    > CALL FUNC    // SAME HERE
    > POP EBP
    > RETN
    >
    > if i made func as inline then why is it called why not the body of
    > func is placed at every place it called.
    >
    > Raashid Bhatt (a.k.a rash0r)


    Maybe inline functions only suggests to the compiler that desired
    behavior. It's a matter of compiler implementation, i believe
     
    Luks, Aug 19, 2008
    #3
  4. raashid bhatt <> writes:
    > by the definition of inline functions
    > "In computer science, an inline function is a programming language
    > construct used to suggest to a compiler that a particular function be
    > subjected to in-line expansion; that is, it suggests that the compiler
    > insert the complete body of the function in every context where that
    > function is used."
    >
    >
    >
    > ========================================
    > int a;
    >
    > inline void func()
    > {
    > a = 50;
    > }
    > void __cdecl main()
    > {
    >
    > func();
    > func();
    >
    > }
    >
    > ===========================================
    >
    > so the complete body of function func() should actually be placed from
    > where it is called ... ie this code is similar to
    >
    > ============================================
    > int a;
    >
    > void __cdecl main()
    > {
    >
    > a = 50;
    > }
    > ============================================
    >
    > but when i compile it i get undesired results
    >
    > PUSH EBP
    > MOV EBP,ESP
    > CALL FUNC
    > ; THE FUNCTION SHOULD NOT BE CALLED RATHER THE WHOLE BODY OF FUNCTION
    > SHOUDL BE PLACED HERE
    > CALL FUNC // SAME HERE
    > POP EBP
    > RETN
    >
    > if i made func as inline then why is it called why not the body of
    > func is placed at every place it called.


    A C implementation isn't required to generate the machine code you
    expect it to generate. In fact, it isn't (as far as the C standard is
    concerned) required to generate code at all. Quoting the standard
    (C99 5.1.2.3p5):

    The least requirements on a conforming implementation are:

    -- At sequence points, volatile objects are stable in the sense
    that previous accesses are complete and subsequent accesses
    have not yet occurred.

    -- At program termination, all data written into files shall be
    identical to the result that execution of the program according
    to the abstract semantics would have produced.

    -- The input and output dynamics of interactive devices shall take
    place as specified in 7.19.3. The intent of these requirements
    is that unbuffered or line-buffered output appear as soon as
    possible, to ensure that prompting messages actually appear
    prior to a program waiting for input.

    In short, the *visible behavior* of a program must be as defined by
    the standard. Since your program has no visible behavior, it's
    equivalent to

    int main(void) { return 0; }

    and a compiler could, and quite possibly would, generate equivalent
    code for it.

    This ignores the error of declaring main as "void __cdecl main()"; it
    should be "int main(void)".

    As for whether the code for the function is actually inlined at the
    point of the call, the standard specifically doesn't require that; the
    "inline" keyword merely "suggests that calls to the function be as
    fast as possible". One typical way to satisfy that suggestion is, as
    the word suggests, to generate inline code rather than a function
    call. (It's likely that your compiler will do this only if you use
    some option to request optimization.) But ignoring the suggestion is
    acceptable.

    Examining the assembly code generated for a C program can be useful,
    but its usefulness is strictly limited. The test of whether a C
    compiler works properly is whether the program behaves correctly.
    Generating assembly code (or machine code) is merely a means to that
    end. C is not a language for specifying machine code; it's a language
    for specifying program behavior.

    --
    Keith Thompson (The_Other_Keith) <http://www.ghoti.net/~kst>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
     
    Keith Thompson, Aug 19, 2008
    #4
    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:
    742
    E. Robert Tisdale
    Jul 3, 2003
  2. Replies:
    3
    Views:
    461
  3. Daniel Vallstrom
    Replies:
    2
    Views:
    1,896
    Kevin Bracey
    Nov 21, 2003
  4. jamihuq

    converting inline functions to C functions

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

Share This Page