macro or function

This is a discussion on macro or function within the C Programming forums, part of the General Programming Boards category; is ait better to use a macro than a function since function adds up to code size while a macros ...

  1. #1
    Registered User
    Join Date
    May 2004
    Posts
    70

    macro or function

    is ait better to use a macro than a function since function adds up to code size while a macros does not

    some one suggested using macros like
    #define READ_REG32(address) *(unsigned int *) address

    #define WRITE_REG32(address, value) *(unsigned int *) address = value

    instead of functions to read or write registers

  2. #2
    Registered User linuxdude's Avatar
    Join Date
    Mar 2003
    Location
    Louisiana
    Posts
    926
    A function generally is safer, because their is no ambiguity. You need parentheses around the basically every variable in a macro. I would suggest using a function.

  3. #3
    ---
    Join Date
    May 2004
    Posts
    1,379
    functions use more code but are faster.
    macros are obviously slower.

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    > is ait better to use a macro than a function since function adds up to code size while a macros does not
    Macros do add to the code size - a lot more than a function would for a complex macro. If you had a 10 line macro, and you used it 10 times, there would be an additional 100 lines of compiled code.

    As a function, there would be 10 lines of code, plus a further 10 to call it (20 in total).

    Technically (and superficially), macros are faster than a function because you don't actually have a function call. However, when you mix in cache and code bloat, the issue is far more complex. In addition, C99 introduces inline functions, which are as fast as macros, but with all the benefits of predictable evaluation and type checking of arguments.

    You're only going to notice the cost of that function call if the function is a) very small, and b) called millions of times.

    Take your register example - it may look nice "as is", but if you want to say validate the arguments (to prevent bad values being written to certain registers), you pretty much need a function at that point. You also need it to be a function if you're to have any real chance of profiling the code prior to optimisation.

    Personally, I go for functions for everything until I know that a macro would be an improvement.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    May 2004
    Posts
    70
    so in essence
    a macro is faster than a function
    but it does not have the checks offered by a macro

    also inline functions provide the benefits of the macro with the safety of the function

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,337
    I'm sticking to my previous comments

    While you're at it, consider this
    Code:
    #include <stdio.h>
    #undef max
    #define max(a,b) a>b?a:b
    int main ( ) {
        int a = 1, b = 1;
        char *foo = max("hello","world");
        int c = max(a++,b++);
        printf( "a=%d, b=%d, c=%d, foo=%s\n", a, b, c, foo );
        return 0;
    }
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. doubt in c parser coding
    By akshara.sinha in forum C Programming
    Replies: 4
    Last Post: 12-23-2007, 12:49 PM
  2. Troubleshooting Input Function
    By SiliconHobo in forum C Programming
    Replies: 14
    Last Post: 12-05-2007, 06:18 AM
  3. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 PM
  4. Game Pointer Trouble?
    By Drahcir in forum C Programming
    Replies: 8
    Last Post: 02-04-2006, 01:53 AM
  5. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 12:03 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21