Thread: Help for understanding of a C code

  1. #1
    Registered User
    Join Date
    Jul 2019
    Posts
    13

    Help for understanding of a C code

    Hello All
    I am kind of beginner in C/C++ and I need to convert following code to python. Could you explain to me what this macro does

    Thnks

    Code:
    /**
    
     * Macros to check and validate a structure pointer, given that the
    
     * first entry in the structure is a magic number. ((void)(1?0:((_ptr), void(), 0)))
    
     */
    #ifdef NO_BOMBING_MACROS
    #define BU_CKMAG(_ptr, _magic, _str) BU_IGNORE((_ptr))
    #else
    #define BU_CKMAG(_ptr, _magic, _str) { \
    
    const uintptr_t _ptrval = (const uintptr_t) (_ptr);
    
    if (UNLIKELY
        ((_ptrval == 0) || (_ptrval & (sizeof(_ptrval) - 1))
         || *((const uint32_t *) (_ptr)) != (uint32_t) (_magic)))
    {
    
    bu_badmagic((const uint32_t *) (_ptr), (uint32_t) _magic, _str, __FILE__,
                __LINE__);
    
    }
    
    }
    
    #endif

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    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.

  3. #3
    Registered User
    Join Date
    Jul 2019
    Posts
    13
    Quote Originally Posted by Salem View Post
    Yes thats it

  4. #4
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    TBH the whole thing is just a hack to try and trap memory errors in C programming.

    The code will work if you assume it does nothing, which is what you get if NO_BOMBING_MACROS is defined.
    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.

  5. #5
    Registered User
    Join Date
    Jul 2019
    Posts
    13
    Quote Originally Posted by Salem View Post
    TBH the whole thing is just a hack to try and trap memory errors in C programming.

    The code will work if you assume it does nothing, which is what you get if NO_BOMBING_MACROS is defined.
    Could you explain syntax as It will be a good practice for me

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    What are you trying to achieve (or learn)?

    If you're hoping to convert all that C code (in the entire suite) to Python, you're going to be a while doing it.

    If you just want to call the code from your Python code, then do that. Python has lots of tools for wrapping up code written in other languages and making it callable from Python.

    Anyhooooo
    Code:
    if (UNLIKELY(
        (_ptrval == 0) || 
        (_ptrval & (sizeof(_ptrval) - 1)) ||
        *((const uint32_t *) (_ptr)) != (uint32_t) (_magic)))
    {
        bu_badmagic((const uint32_t *) (_ptr), (uint32_t) _magic, _str, __FILE__,  __LINE__);
    1. A GCC macro that tells the compiler that the following condition is normally expected to evaluate to false, and should generate code based on this assumption.
    2. Check whether ptrval is zero
    3. Check whether ptrval has a bit-pattern of say xx01, xx02 or xx03.
    Say sizeof(ptrval) is 4, then (sizeof(_ptrval) - 1) is 3 (aka 0x03, aka 0b00000011).
    A ptrval of 4 would be fine, whereas 5 would be bad (because 5 & 3 is 1).
    4. Check that the long stored at the dereferenced ptr isn't the magic value.

    If any of those three predicates evaluates to true (indicating something is wrong), then crash-and-burn by calling bu_badmagic
    bu_badmagic is given the __FILE__ and __LINE__ to tell the developer where the problem was identified.
    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.

  7. #7
    Registered User
    Join Date
    Jul 2019
    Posts
    13
    Is below can be written without paranthesis ? Why do we need ()
    Code:
    const  uintptr_t  _ptrval = (constuintptr_t) (_ptr);
    Last edited by Salem; 07-31-2019 at 11:47 PM. Reason: Snipped massive quote

  8. #8
    TEIAM - problem solved
    Join Date
    Apr 2012
    Location
    Melbourne Australia
    Posts
    1,907
    Keep the paranthesis for macros - for more on macros see Tutorials - C Preprocessor Tricks - Cprogramming.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help understanding this code
    By jim_0 in forum C++ Programming
    Replies: 1
    Last Post: 11-27-2013, 08:45 AM
  2. code understanding
    By elwad in forum C Programming
    Replies: 5
    Last Post: 04-18-2009, 06:57 AM
  3. Help understanding a code
    By lolguy in forum Networking/Device Communication
    Replies: 2
    Last Post: 03-15-2009, 04:13 PM
  4. help me understanding this code
    By apfelsaft in forum C Programming
    Replies: 2
    Last Post: 01-14-2004, 04:46 PM
  5. help understanding code
    By volk in forum C Programming
    Replies: 5
    Last Post: 02-08-2003, 10:50 AM

Tags for this Thread