inline Functions in C

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • krisvamsi22
    New Member
    • Mar 2008
    • 2

    inline Functions in C

    Hi,

    I want to know about inline functions in C, why they are used? How they works? Advantages and disadvantages of using them? And if any practical examples.... I tried in Google search but couldnt find relevant data.

    Please help me by giving some explanation or link where I can get explanation.

    Thanks in advance,

    Vamsi Krishna
  • gpraghuram
    Recognized Expert Top Contributor
    • Mar 2007
    • 1275

    #2
    Originally posted by krisvamsi22
    Hi,

    I want to know about inline functions in C, why they are used? How they works? Advantages and disadvantages of using them? And if any practical examples.... I tried in Google search but couldnt find relevant data.

    Please help me by giving some explanation or link where I can get explanation.

    Thanks in advance,

    Vamsi Krishna
    if u #define a function and use it then it will be sort of inline function in C.
    But it dosent provide all benefits provided by the inline functions in c++.

    Thanks
    Raghuram

    Comment

    • Banfa
      Recognized Expert Expert
      • Feb 2006
      • 9067

      #3
      Originally posted by gpraghuram
      if u #define a function and use it then it will be sort of inline function in C.
      But it dosent provide all benefits provided by the inline functions in c++.
      The some of the more developed C compilers (mainly ones that support C++ as well) support real inlining although this is an extension of the standard.

      For instance the Microsoft C/C++ compiler supports inlining in C, but it has to be explicitly enabled and functions have to be marked with the __inline keyword, the __ indicating it is a platform extension rather than part of the C language.

      The advantage of inlining is that it makes the code quicker because it saves the function call overhead by putting the functions code directly into the code where it is called rather than actually making a function call.

      The disadvantage of function inlining is that it tends to make the code larger because of all the repeated copies of the function.

      The best candidates for function inlining are functions which are small, a few code lines (i.e. the function call overhead is a large portion of the time the function takes to run) and which are called often (the saved time is quite small so it only makes a real difference if you save it a lot of times).


      I have also used the method gpraghuram describes of replacing functions with function like macros.


      However which ever method you use it is worth profiling the application first to see where you may be able to make savings.

      Comment

      • krisvamsi22
        New Member
        • Mar 2008
        • 2

        #4
        Originally posted by Banfa
        The some of the more developed C compilers (mainly ones that support C++ as well) support real inlining although this is an extension of the standard.

        For instance the Microsoft C/C++ compiler supports inlining in C, but it has to be explicitly enabled and functions have to be marked with the __inline keyword, the __ indicating it is a platform extension rather than part of the C language.

        The advantage of inlining is that it makes the code quicker because it saves the function call overhead by putting the functions code directly into the code where it is called rather than actually making a function call.

        The disadvantage of function inlining is that it tends to make the code larger because of all the repeated copies of the function.

        The best candidates for function inlining are functions which are small, a few code lines (i.e. the function call overhead is a large portion of the time the function takes to run) and which are called often (the saved time is quite small so it only makes a real difference if you save it a lot of times).


        I have also used the method gpraghuram describes of replacing functions with function like macros.


        However which ever method you use it is worth profiling the application first to see where you may be able to make savings.

        Hi Thanks a lot for the answer, if inline functions are doing same as what a macro function is doing then what could be the difference between defining a function as a macro and defining it as __inline. What is the advantage of using inline over defining a function as macro? I have seen many functions defined as inline in Embedded device driver programing, what role it plays there?

        Thanks in advance for the help

        Comment

        • gpraghuram
          Recognized Expert Top Contributor
          • Mar 2007
          • 1275

          #5
          inline offers many advantages over macros.(To get more info on this do a saerch in the net with rem macros vs inline).
          When u say inline it is always not guaranteed whether it will be inlined and it depends purely on the compiler optimization

          Thanks
          Raghu

          Comment

          • dharanidhar
            New Member
            • Apr 2008
            • 7

            #6
            #define _INITHW_C_SRC

            does the above represent a inline function?
            If a function is defined in one of the files and we want to use it in another,is it in this case we use inline function?

            Comment

            • gpraghuram
              Recognized Expert Top Contributor
              • Mar 2007
              • 1275

              #7
              Originally posted by dharanidhar
              #define _INITHW_C_SRC

              does the above represent a inline function?
              If a function is defined in one of the files and we want to use it in another,is it in this case we use inline function?
              No...This is not a inline function.
              Inline functions are basicallu used to increase the speed that is by avoiding the function call.(you have to read the post by Banfa)

              Raghuram

              Comment

              Working...