Thread: macros

  1. #1
    Registered User
    Join Date
    Sep 2004
    Posts
    153

    macros

    Hey everyone,
    So I came across this piece of code that I found very very interesting:
    Code:
    #define oldmax(x, y) ((x)>(y)?(x):(y))
    do people still do this type of thing? You could easily just create a function that has the same effect, but curiously does anyone still use macros in this way? Just wondering - Chap

  2. #2
    Cheesy Poofs! PJYelton's Avatar
    Join Date
    Sep 2002
    Location
    Boulder
    Posts
    1,728
    Have you seen the code submitted in an obfuscated code contest?

    I do see stuff like that every once in awhile in people programs but it makes me cringe every time.

  3. #3
    Registered User
    Join Date
    Sep 2004
    Posts
    153
    lol Yelton...I went to the link you put up and all I have to say is damn....that is some of the most ridiculous stuff I have ever seen haha.

  4. #4
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by Chaplin27
    lol Yelton...I went to the link you put up and all I have to say is damn....that is some of the most ridiculous stuff I have ever seen haha.
    you should actually give jessycat's program a run - it really is great work...

    I would avoid macros as much as possible. yes, you can do that in a function, and yes, you should do that in a function.
    Join is in our Unofficial Cprog IRC channel
    Server: irc.phoenixradio.org
    Channel: #Tech


    Team Cprog Folding@Home: Team #43476
    Download it Here
    Detailed Stats Here
    More Detailed Stats
    52 Members so far, are YOU a member?
    Current team score: 1223226 (ranked 374 of 45152)

    The CBoard team is doing better than 99.16% of the other teams
    Top 5 Members: Xterria(518175), pianorain(118517), Bennet(64957), JaWiB(55610), alphaoide(44374)

    Last Updated on: Wed, 30 Aug, 2006 @ 2:30 PM EDT

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > do people still do this type of thing?
    It's pointless in C++, since an inline function is just as quick, and is way safer than a macro.

    In C, macros are sometimes used to replace functions on the pretence that it saves "a function call". Mostly this seems to be premature optimisation.
    However, C99 introduces the inline keyword as well, so hopefully such things will diminish in use.

    As written, that macro + this code probably compiles, though what answer you get is a matter of luck
    Code:
    char *foo = oldmax( "hello", "world" );
    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.

  6. #6
    Politics&Cpp geek Da-Nuka's Avatar
    Join Date
    Oct 2004
    Posts
    104

    #define ints

    How about defining integers?
    In a big program, would you do this:
    Code:
    #define ID_EXIT 100012
    //...a LOT of codes, functions, integers... goes here
    //and finaly:
    case ID_EXIT:
    Or would you avoid that, since its a macro, and do like this:

    Code:
    case 100012:
    Or would you go for a other solution?

  7. #7
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Quote Originally Posted by Da-Nuka
    How about defining integers?
    In a big program, would you do this:
    Code:
    #define ID_EXIT 100012
    //...a LOT of codes, functions, integers... goes here
    //and finaly:
    case ID_EXIT:
    Or would you avoid that, since its a macro, and do like this:

    Code:
    case 100012:
    Or would you go for a other solution?
    const unsigned ID_EXIT = 100012;
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  8. #8
    Politics&Cpp geek Da-Nuka's Avatar
    Join Date
    Oct 2004
    Posts
    104
    Hmm..what is best if you only care about app-size and memory-usage?

    #define,
    or const int ?
    Last edited by Da-Nuka; 03-02-2005 at 01:14 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Order of execution of preprocessor macros
    By DL1 in forum C Programming
    Replies: 2
    Last Post: 04-02-2009, 06:52 PM
  2. Pre-processor macros causing segfaults
    By nempo in forum C++ Programming
    Replies: 6
    Last Post: 02-10-2009, 02:35 AM
  3. Macros inside of macros
    By Chewie8 in forum C Programming
    Replies: 2
    Last Post: 02-24-2008, 03:51 AM
  4. Macros vs Inline Functions
    By vb.bajpai in forum C Programming
    Replies: 4
    Last Post: 08-02-2007, 11:51 AM
  5. template fn replacements for msg macros
    By Ken Fitlike in forum Windows Programming
    Replies: 17
    Last Post: 10-30-2002, 07:55 AM