I am using C51 compiler V7.50 I don't know if this 'works' on other versions however I was able to crash the compiler by this 'silly' macro usage
// causes compiler to crash #define WORD_SIZE(N) (sizeof((N))/sizeof(uint16_t)) // doesn't cause compiler to crash #define WORD_SIZE(N) (sizeof(N)/sizeof(uint16_t)) #define NSIZE WORD_SIZE(uint16_t) // crash point size = NSIZE;
I determined the point at which the compiler expired by #ifdef #endif usage to narrow down the section of 'death'. Erstwhile I corrected my macro (duh moment), however it is a bit difficult to find the offending code if the compiler just dies (oh well). This is a 'Just In Case' post I suppose.
Stephen
#define WORD_SIZE(N) (sizeof((N))/sizeof(uint16_t)) #define NSIZE WORD_SIZE(uint16_t)
Just tested it using another compiler, using visual C does not crash but creates an error. The problem seems to be that using a type name in braces ((uint16_t)) looks more like a 'cast to type' than a type name. But there is no variable for that 'cast', like in ((uint16_t)variable)
That macro works fine for a variable instead of a type name:
#define WORD_SIZE(N) (sizeof((N))/sizeof(uint16_t)) unsigned long temp; #define NSIZE WORD_SIZE(temp)
because (variable) and ((variable)) is the same.
For precise information, if your macro is legal C, I would need to read the C standard in detail, which I don't have the time right now.
But there is no need to use double braces here at all, instead sizeof((N)) the sizeof(N) does the job already, as you already mentioned.
Little off topic, did you know that you could write (but shouldn't for better readability)
int variable; int var = sizeof variable; instead int var = sizeof(variable);