Thread: Macros Using #s

  1. #16
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    NULL is constant, not temporary.
    Not really - NULL is a constant, but its use in general programming practice can be temporary.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  2. #17
    Registered User major_small's Avatar
    Join Date
    May 2003
    Posts
    2,787
    Quote Originally Posted by quzah
    NULL is constant, not temporary.


    Quzah.
    party pooper... if you think about it, NULL (along with all other 'constant' variables) is temporary, because it only exists as long as the program is running

    and no, I'm not talking about NULL as a universal thing, just as it applies to a C/C++ program.

    more fun:
    Code:
    #include<iostream>
    #undef NULL
    
    int main()
    {
            #ifdef NULL
            int*ptr=NULL;
            std::cout<<"NULL defined as "<<ptr<<std::endl;
            #else
            std::cout<<"NULL not defined"<<std::endl;
            #endif
    
            return 0;
    }
    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

  3. #18
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    And...aren't there rules about white space, new lines, commas, semi-colons, etc. when defining macros? Could somoene explain these a little? Isn't it that you never use semi-colons, comments, or double-quotes?
    There aren't any white space rules other than the usual. The only "newline" issue is that you escape multi-line macros. Like so:
    Code:
    #define FOO(bar) line1 \
        line2
    The last line doesn't have a \, all others do.

    A macro is simply a compile time text subsutution. The ## macros smash things together. Like so:
    Code:
    #define FOO(bar,baz) bar ## baz
    
    int var1, var2, var3
    
    FOO( var, 1 ) = 10;
    Then you do something like this:
    Code:
    #include<iostream>
    
    #define FOO(bar,baz) bar ## \
        baz
    #define BAR(foo,baz) foo # \
        baz
    
    int main( )
    {
        int var1, var2, var3;
    
        FOO( var, 1 ) = 10;
        FOO( var, 2 ) = 20;
        FOO( var, 3 ) = 30;
    
        std::cout << BAR("var",1) << " is: " << FOO( var, 1 ) << std::endl;
        std::cout << BAR("var",2) << " is: " << FOO( var, 2 ) << std::endl;
        std::cout << BAR("var",3) << " is: " << FOO( var, 3 ) << std::endl;
    
        return 0;
    }
    They're multi-line just for the hell of it. They don't need to be. The only thing about white space is you need your #define or #include or what not to start on the left margin. Some compilers will let you get away with it indented for some of the things, but IIRC, th're supposed to start on the left most character.
    Code:
    #include <GOOD>
    Code:
        #include <bad>

    Quzah.
    Last edited by quzah; 07-17-2005 at 06:41 AM.
    Hope is the first step on the road to disappointment.

  4. #19
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by laserlight
    Not really - NULL is a constant, but its use in general programming practice can be temporary.
    NULL is a constant. You've just said so. Your use of it is temporary, but NULL remains constant. There's no two ways about it.

    Waxing philisophical...


    Quzah.
    Hope is the first step on the road to disappointment.

  5. #20
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    NULL is a constant. You've just said so. Your use of it is temporary, but NULL remains constant. There's no two ways about it.
    We're just playing with language
    I didnt say NULL is a temporary, I said NULL is temporary.
    I didnt say NULL is constant, I said NULL is a constant.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  6. #21
    the hat of redundancy hat nvoigt's Avatar
    Join Date
    Aug 2001
    Location
    Hannover, Germany
    Posts
    3,130
    Macros, if used correctly and documented correctly, can save a lot of work. And no, if the macro isn't simple, it cannot be replaced by an inline function.

    Challenge 1: Write an inline function that does the same as

    Code:
    #define PRINT_NAME_AND_NUMERICAL_VALUE( name ) printf("%s is %d", #name, name );
    or

    Code:
    #define DECLARE_VARIABLE_AND_ACCESSORS( name ) int m_i##name; \
    int GetNumber##name() { return m_i##name; }\
    void SetNumber##name( int val ) { m_i##name = val; }
    Looks ugly for a little class, but write a class with 100 variables and accessors and we will see.

    Or write an inline function, that will be compiled conditionally

    Code:
    #ifdef _PERF_
    MEASURE_PERFORMANCE( statement, description ) [performance measuring and output code]
    #else
    MEASURE_PERFORMANCE( statement, description ) statement;
    #endif
    This might be the job of a profiler... but you don't want to have a profiler on your production system.

    I don't see any alternatives to Macros. You probably frown upon them while others get work done using them
    hth
    -nv

    She was so Blonde, she spent 20 minutes looking at the orange juice can because it said "Concentrate."

    When in doubt, read the FAQ.
    Then ask a smart question.

  7. #22
    Supermassive black hole cboard_member's Avatar
    Join Date
    Jul 2005
    Posts
    1,709
    Actually the first macro:

    Code:
    #define PRINT_NAME_AND_NUMERICAL_VALUE( name ) printf("%s is %d", #name, name );
    Wouldn't be to hard - not that I've tried - as you can surely use a stringstream to get the numerical value of name? You can use a stringstream to write an itos function.

    Actually I'm going to try that now, I'll post it up if I have any joy.
    Good class architecture is not like a Swiss Army Knife; it should be more like a well balanced throwing knife.

    - Mike McShaffry

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