When to use macros.

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • prashant.khade1623@gmail.com

    When to use macros.

    HI All,

    I know the clear distinction between macro and function.

    I know that macro will speed up the program and using function will
    reduce the size.

    But how do we know when to use macro and function. ?
  • Ian Collins

    #2
    Re: When to use macros.

    prashant.khade1 623@gmail.com wrote:
    HI All,
    >
    I know the clear distinction between macro and function.
    >
    I know that macro will speed up the program and using function will
    reduce the size.
    >
    But how do we know when to use macro and function. ?
    Use a function when you can, use a macro when all else fails. Modern
    compilers do a good job of inlining short functions, so function like
    macros are only really useful to implement a poor man's function
    overloading.

    --
    Ian Collins.

    Comment

    • Richard Heathfield

      #3
      Re: When to use macros.

      prashant.khade1 623@gmail.com said:
      HI All,
      >
      I know the clear distinction between macro and function.
      (I presume you are talking about function-like macros.)
      >
      I know that macro will speed up the program and using function will
      reduce the size.
      Insofar as this is true, it is because macros are expanded inline
      (increasing the size of the program with every expansion, but eliminating
      the overhead of a function call). In C99, functions can be inlined too (at
      the compiler's discretion).
      >
      But how do we know when to use macro and function. ?
      If in doubt, use a function.

      Macros can be powerful and convenient, but they can't do type-checking and
      they run the risk of performing side-effects more than once.

      Don't be religious about it - as I said, macros /can/ be very powerful when
      properly used - but it is wisest to favour functions over macros in normal
      usage. This will tend to lead to fewer bugs and cleaner code.

      --
      Richard Heathfield <http://www.cpax.org.uk >
      Email: -http://www. +rjh@
      Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
      "Usenet is a strange place" - dmr 29 July 1999

      Comment

      • Ben Bacarisse

        #4
        Re: When to use macros.

        Richard Heathfield <rjh@see.sig.in validwrites:
        <snip>
        >I know that macro will speed up the program and using function will
        >reduce the size.
        >
        Insofar as this is true, it is because macros are expanded inline
        (increasing the size of the program with every expansion, but eliminating
        the overhead of a function call). In C99, functions can be inlined too (at
        the compiler's discretion).
        Can't functions be inlined in C90 (at the compiler's discretion)?

        --
        Ben.

        Comment

        • Richard Heathfield

          #5
          Re: When to use macros.

          Ben Bacarisse said:
          Richard Heathfield <rjh@see.sig.in validwrites:
          ><snip>
          >>I know that macro will speed up the program and using function will
          >>reduce the size.
          >>
          >Insofar as this is true, it is because macros are expanded inline
          >(increasing the size of the program with every expansion, but
          >eliminating the overhead of a function call). In C99, functions can be
          >inlined too (at the compiler's discretion).
          >
          Can't functions be inlined in C90 (at the compiler's discretion)?
          Um, yes, of course. What I ought to have said is: "In C99, you can request
          that functions be inlined".

          --
          Richard Heathfield <http://www.cpax.org.uk >
          Email: -http://www. +rjh@
          Google users: <http://www.cpax.org.uk/prg/writings/googly.php>
          "Usenet is a strange place" - dmr 29 July 1999

          Comment

          • Default User

            #6
            Re: When to use macros.

            Ian Collins wrote:
            prashant.khade1 623@gmail.com wrote:
            HI All,

            I know the clear distinction between macro and function.

            I know that macro will speed up the program and using function will
            reduce the size.

            But how do we know when to use macro and function. ?
            >
            Use a function when you can, use a macro when all else fails. Modern
            compilers do a good job of inlining short functions, so function like
            macros are only really useful to implement a poor man's function
            overloading.

            They are also useful for switchable behavior. As an example, you can
            have extra diagnostic "functions" embedded for development, then turn
            them off for release code. Naturally, you could do the same thing with
            #if blocks around real function calls, but that tends to look a bit
            more confusing.



            Brian

            Comment

            • Herbert Rosenau

              #7
              Re: When to use macros.

              On Sat, 12 Apr 2008 07:19:40 UTC, "prashant.khade 1623@gmail.com"
              <prashant.khade 1623@gmail.comw rote:
              HI All,
              >
              I know the clear distinction between macro and function.
              >
              I know that macro will speed up the program and using function will
              reduce the size.
              >
              But how do we know when to use macro and function. ?
              On modern compilers a function that can be inlined is to prefere over
              a macro because it adds type security.

              As a macro is only text replacement it can very useful when the only
              difference between different functions is only a single statement or
              different types on the same functionality.

              A macro with parameters fails miserably on having usage of side
              effects like pre/post increment on its opreands, an inlided function
              avoids that.

              I prefere functions over macros. On other hand I write a macro instead
              of a function when I have to do the same little piece of code with
              different data types.

              --
              Tschau/Bye
              Herbert

              Visit http://www.ecomstation.de the home of german eComStation
              eComStation 1.2R Deutsch ist da!

              Comment

              Working...