Preprocessor and sizeof()
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)
|