Preprocessor and sizeof()

Discussion in 'C' started by Amigo, Sep 30, 2006.

  1. Amigo

    Amigo New Member

    Joined:
    Sep 30, 2006
    Messages:
    1
    Likes Received:
    0
    Trophy Points:
    0
    Hello all!

    I started working on an embedded project a few ago on Freescale 16-bit micro with an IAR toolset. Running PolySpace for the project code highlighted amongst other things a peculiar construct in one of the compiler's header files. Here's the code snippet:

    #ifndef __SIZE_T_TYPE__
    #if sizeof((char*)0 - (char*)0) <= sizeof(int)
    #define __SIZE_T_TYPE__ unsigned int
    #else
    #define __SIZE_T_TYPE__ unsigned long
    #endif
    #endif

    I was very suspicious about this construct in the first place, but coming from a compiler vendor baffled me even more. Although I kind of understand what the author wanted, the combination of sizeof and pre-processing is bound to not comply with the ANSI C specification.

    My question is: have you ever encountered such a construct in a file delivered by a tool vendor? It definitely "works", in that the compiler does not throw errors or warnings. But because this code goes into a safety-related project I need to understand exactly what's happening in the code and why. Any thoughts appreciated.

    Cheers,
    Romeo

    PS: I was dumped in the project team just last week, so I did not have time to look through the whole of the project (documentation is
    "standard", i.e. poor)
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice