Thread: parameterized macros vs. functions

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    10

    Smile parameterized macros vs. functions

    I've been asked the following:

    Functions can often-but not always-be written as parametrized macros. Discuss what characteristics of a function would make it unsuitable as a macro.

    I'm not really sure how to answer. I know parameterized macros can evaluate their arguments more than once, leading to unexpected results. And I know you can't have a pointer to a macro where you can have one to a pointer. Beyond that I'm confused as to what would make a function unsuitable as a macro.

    Any suggestions would be greatly appreciated! Thanks a lot

    T.

  2. #2
    Kernel hacker
    Join Date
    Jul 2007
    Location
    Farncombe, Surrey, England
    Posts
    15,677
    I'm not going to give you direct answers, but a few leading questions:

    What do you think the effect on the code size with larger macros is? What difference is there in the generated code between a macro and a function?

    How would you debug a multi-line macro solution?

    What would you do if you want to add a tracing function that shows which source file and which line you are at when the trace is output? Is there a way to do this in a macro that is better than a function?

    --
    Mats
    Compilers can produce warnings - make the compiler programmers happy: Use them!
    Please don't PM me for help - and no, I don't do help over instant messengers.

  3. #3
    Complete Beginner
    Join Date
    Feb 2009
    Posts
    312
    From the language point of view, you already mentioned the main differences between functions and macros, namely that macro arguments may get evaluated more than once and you can't have pointers to macros (because they don't have an address). A third one is that you can't "call" macros recursively and, more generally, you can't have forward references to other macros. Furthermore, macro parameters don't have a type specification and macros don't have a return value, but you can easily emulate both.

    Mats' points are mostly consequences of today's machine designs and poor optimization techniques and may be obsolete in a few (hundred) years.

    Greets,
    Philip
    All things begin as source code.
    Source code begins with an empty file.
    -- Tao Te Chip

  4. #4
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Also consider what happens if there is an error in the macro.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. calling functions within functions
    By edd1986 in forum C Programming
    Replies: 3
    Last Post: 03-29-2005, 03:35 AM
  2. Factory Functions HOWTO
    By GuardianDevil in forum Windows Programming
    Replies: 1
    Last Post: 05-01-2004, 01:41 PM
  3. Shell functions on Win XP
    By geek@02 in forum Windows Programming
    Replies: 6
    Last Post: 04-19-2004, 05:39 AM
  4. template fn replacements for msg macros
    By Ken Fitlike in forum Windows Programming
    Replies: 17
    Last Post: 10-30-2002, 07:55 AM
  5. functions - please help!!!!
    By linkies in forum C Programming
    Replies: 1
    Last Post: 08-21-2002, 07:53 AM