CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 11 of 11
  1. #1
    Join Date
    Jul 2002
    Location
    Denmark
    Posts
    24

    Strange macro, valid?

    In a header I was looking at, I saw this...
    Code:
    #define MMM_X(x)_Z(z)	(0x0C + (((x)-1)*3 + ((z)-1))*4)
    ... is that valid? I don't recall seeing a macro like that before.

    (Name changed to protect the innocent!!!)
    Last edited by Exit; September 1st, 2007 at 05:35 AM.

  2. #2
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Strange macro, valid?

    That looks wrong to me. Where did you find it?
    My hobby projects:
    www.rclsoftware.org.uk

  3. #3
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: Strange macro, valid?

    looks fine for me

  4. #4
    Join Date
    Jul 2002
    Location
    Denmark
    Posts
    24

    Re: Strange macro, valid?

    Where did you find it?
    In a buildroot kernel header for a KS8695 chip.

  5. #5
    Join Date
    Sep 2005
    Location
    New Delhi, India
    Posts
    332

    Re: Strange macro, valid?

    Quote Originally Posted by Mitsukai
    looks fine for me
    Why does it appear fine to you
    Preprocessing directive are of the form
    Code:
    # define identifier replacement-list new-line
    which defines an object-like macro that causes each subsequent instance of the macro name.

    or

    Code:
    # define identifier lparen identifier-listopt ) replacement-list new-line
    which defines a function-like macro with parameters, similar syntactically to a function call.
    Last edited by sunnypalsingh; September 1st, 2007 at 05:15 AM.
    Appreciate others by rating good posts

    "Only buy something that you'd be perfectly happy to hold if the market shut down for 10 years." - Warren Buffett

  6. #6
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: Strange macro, valid?

    the macro is FINE

    the information by the OP is NOT

    Code:
    #define _Z(z) (z)+
    #define MMM_X(x)_Z(z)	(0x0C + (((x)-1)*3 + ((z)-1))*4)
    
    int main()
    {
       int z = 5;
       MMM_X(5);
    }
    note _Z could also be a type or function!

    Code:
    Your Comeau C/C++ test results are as follows:
    
    Comeau C/C++ 4.3.9 (Mar 27 2007 17:24:47) for ONLINE_EVALUATION_BETA1
    Copyright 1988-2007 Comeau Computing.  All rights reserved.
    MODE:strict errors C++ noC++0x_extensions
    
    "ComeauTest.c", line 8: warning: expression has no effect
         MMM_X(5);
         ^
    
    
    In strict mode, without -tused, Compile succeeded (but remember, the Comeau online compiler does not link).
    Compiled with C++0x extensions DISabled.
    Last edited by Mitsukai; September 1st, 2007 at 06:08 AM.

  7. #7
    Join Date
    Jul 2002
    Location
    Denmark
    Posts
    24

    Re: Strange macro, valid?

    the information by the OP is NOT
    What, pray tell, is wrong with the information in the OP? The macro appears in the header as is, (with a different name), and is the only macro in the header. All the other #defines are constant base addresses, register offsets and bit masks.
    Last edited by Exit; September 1st, 2007 at 06:52 AM. Reason: Spelling mistake.

  8. #8
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: Strange macro, valid?

    this is not a compilable example so the information by OP is not enough

  9. #9
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Strange macro, valid?

    Quote Originally Posted by Mitsukai
    the macro is FINE
    It looks wrong to me.

    Can you think of a sensible use which does not result in a 'redundant operation' warning ?
    My hobby projects:
    www.rclsoftware.org.uk

  10. #10
    Join Date
    Aug 2005
    Location
    Netherlands, The
    Posts
    2,184

    Re: Strange macro, valid?

    redundant operation?

    just put int x = infront of the macro call

    or make _Z a function or type

  11. #11
    Join Date
    Apr 2004
    Location
    England, Europe
    Posts
    2,492

    Re: Strange macro, valid?

    Quote Originally Posted by Mitsukai
    redundant operation?

    just put int x = infront of the macro call
    Good point.


    Nevertheless, the macro still looks wrong to me.
    My hobby projects:
    www.rclsoftware.org.uk

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured