Function inlining in C (not C99)

Discussion in 'C Programming' started by Nikos Chantziaras, Aug 12, 2003.

  1. What's the best way to allow the compiler to inline a function in C? (Not
    C99, which already supports the 'inline' keyword.)

    The way I do it, is to define the functions in question as 'static' in a
    header file:

    static void foo()
    {
    /* ... */
    }

    Almost every compiler these days will inline the function when compiling
    with optimizations turned on (GCC does, I think). Anyway, can something go
    wrong with this approach? And if yes, is there a better way to achieve
    this?


    --
    Niko <realnc--(AT)--lycos--(DOT)--de>
    http://members.lycos.co.uk/realnc
    Nikos Chantziaras, Aug 12, 2003
    #1
    1. Advertising

  2. Nikos Chantziaras

    Mark Mynsted Guest

    >>>>> "Nikos" == Nikos Chantziaras <> writes:

    Nikos> What's the best way to allow the compiler to inline a function in C? (Not
    Nikos> C99, which already supports the 'inline' keyword.)

    The best way would be found by reading the documentation that
    accompanies your compiler. It may be something a simple as
    -finline-functions or not. I do not believe there is a portable
    answer to your question.

    Nikos> The way I do it, is to define the functions in question as 'static' in a
    Nikos> header file:

    This MAY make your functions have more potential to be inlined by your
    compiler, but it may not. It depends on your compiler. The static
    keyword for functions has other ramifications. To net this out, make
    functions static where they should/can be static, but not because they
    will be more likely to be inlined.

    Nikos> Almost every compiler these days will inline the function when compiling
    Nikos> with optimizations turned on (GCC does, I think). Anyway, can something go
    Nikos> wrong with this approach?

    Yes. If there were no trade-offs why would it be an option? There
    are other ways to improve the performance of your code. Maybe you
    need a better algorithm vs. inlining... There are many other ways to
    improve performance where it is needed.



    --
    -MM
    I rarely read email from this address /"\
    because of spam. \ / ASCII Ribbon Campaign
    I MAY see it if you put #NOTSPAM# X Against HTML Mail
    in the subject line. / \
    Mark Mynsted, Aug 12, 2003
    #2
    1. Advertising

  3. Nikos Chantziaras

    Dan Pop Guest

    In <bhbhdt$vtiv8$-berlin.de> "Nikos Chantziaras" <> writes:

    >What's the best way to allow the compiler to inline a function in C? (Not
    >C99, which already supports the 'inline' keyword.)
    >
    >The way I do it, is to define the functions in question as 'static' in a
    >header file:
    >
    > static void foo()
    > {
    > /* ... */
    > }
    >
    >Almost every compiler these days will inline the function when compiling
    >with optimizations turned on (GCC does, I think). Anyway, can something go
    >wrong with this approach? And if yes, is there a better way to achieve
    >this?


    The only reliable way of achieving what you want is to use macros instead
    of functions (where possible).

    Dan
    --
    Dan Pop
    DESY Zeuthen, RZ group
    Email:
    Dan Pop, Aug 13, 2003
    #3
  4. Nikos Chantziaras

    Malcolm Guest

    "Nikos Chantziaras" <> wrote in message
    >
    > What's the best way to allow the compiler to inline a function in C?
    > (Not C99, which already supports the 'inline' keyword.)
    >

    Compile under C++.
    Malcolm, Aug 13, 2003
    #4
  5. Nikos Chantziaras

    Chris Dollin Guest

    Chris Dollin, Aug 14, 2003
    #5
  6. Nikos Chantziaras

    Malcolm Guest

    "Don Porges" <> wrote in message
    >

    [ inline by declaring static in header ]
    >
    > On downside is that if you port to a compiler that doesn't do the
    > inlining, you end up with a copy of foo()'s code in every .c module that
    > includes that .h. Multiply by the number of functions you've done this >

    to.
    >

    Since you expect the function to be inlined it can't be too big.
    Anyway, code size is seldom much of a problem these days.
    Malcolm, Aug 14, 2003
    #6
    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. Ronald Fischer
    Replies:
    3
    Views:
    563
    Roedy Green
    Jul 20, 2004
  2. Alex
    Replies:
    6
    Views:
    409
  3. Replies:
    3
    Views:
    3,646
    Chris Torek
    Feb 20, 2006
  4. Replies:
    3
    Views:
    581
    Keith Thompson
    Mar 31, 2007
  5. Yakov
    Replies:
    8
    Views:
    305
    Mark McIntyre
    Jun 26, 2007
Loading...

Share This Page