Thread: some codes need explnation

  1. #1
    Registered User
    Join Date
    May 2020
    Posts
    6

    Question some codes need explnation

    Hi,

    The below code is a part of project (opensource code), kindly I have 3 Questions:

    1) why do they write a (\) at end of each line?
    2) what does while(0) mean?
    3) they use expr as a parameter without any declaration of it, then when they call this function such as:
    vlr = csmap->vlr;
    ogs_assert(val);
    the question is: can we consider a expr as a type of variable like (int, double..)
    Code:
    #define ogs_assert(expr) \
    
    do {
    
      if (ogs_likely(expr));
    
    
    
    
      else {
    
    
    
    
        ogs_fatal("%s: Assertion `%s' failed.", OGS_FUNC,
    #expr); \
    
                  ogs_abort();}
    
    
    
    
                  } while (0)
    Last edited by Salem; 06-01-2020 at 04:59 AM. Reason: Removed crayola

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,926
    You need to realise that this is the definition of a function-style macro. Thus, it needs to be on one logical line only. To spread it across multiple physical lines for readability, the backslashes are placed at the end of each physical line so that it "escapes" the newline, so you end up with one logical line from many physical lines.

    The do while (0) technique is basically a way to group the statements of a function-style macro that consists of many statements in a self-contained way. If you just use braces, a typo error can then result in errors that are harder to diagnose.

    The use of expr without a declaration is simply because this is a function-style macro, not a function. You'll need to read up on macros to understand in greater depth. As the abbreviation implies, expr can be an expression, not just a variable name. It could also be a type name, but then it would be misnamed.
    Last edited by laserlight; 06-01-2020 at 03:43 AM.
    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

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    424
    3) they use expr as a parameter without any declaration of it, then when they call this function such as:
    vlr = csmap->vlr;
    ogs_assert(val);
    the question is: can we consider a expr as a type of variable like (int, double..)

    The C preprocessor - all the #directives - is a word processing program. So #include means "paste text from file x here", #if means "cut this text out if the condition is false".

    #define means "sustitute this text with this text". However #define is quite powerful, as it allows for arguments. Macro arguments are variables in a sense, and they are usually but not always C variables at the point the macro is invoked. However they are processed as text. In the example you give, "expr" is a piece of text. It is then pasted into the condition of an if statement. So, as the name implies,
    the macro will only work properly if expr is an expression.

    Later on it is stringized - converted from text to a C string, the equivalent broadly speaking of wrapping quotation marks round it. This is for passing to an output function that takes printf-like arguments (almost certainly, we can't see the code, but this will be what ogs_fatal() does).
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  4. #4
    Registered User
    Join Date
    May 2020
    Posts
    6
    many thanks dear,

    please could explain this macro:

    #define ogs_likely(x) __builtin_expect (!!(x), 1)

  5. #5
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    27,926
    That is a simple function-style macro. What are you having trouble understanding?

    If the difficulty in understanding lies with __builtin_expect rather than the macro feature, then read the manual: GCC manual page on Other Built-in Functions Provided by GCC
    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

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. some codes need explnation
    By Mhdalf in forum C Programming
    Replies: 5
    Last Post: 05-21-2020, 05:42 AM
  2. Replies: 7
    Last Post: 07-31-2017, 10:14 AM
  3. Validate area codes within a list of area codes
    By Staja24 in forum C Programming
    Replies: 9
    Last Post: 05-06-2015, 09:28 PM
  4. codes
    By IfYouSaySo in forum A Brief History of Cprogramming.com
    Replies: 34
    Last Post: 11-18-2005, 03:09 PM
  5. converting scan codes to ascii codes
    By stupid_mutt in forum C Programming
    Replies: 11
    Last Post: 01-25-2002, 04:06 PM

Tags for this Thread