Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does the following "assert_disabled()" macro work?

Tags:

c

assert

sizeof

I see this macro appearing in many places in a code base to find if a particular field is disabled or not (0 or 1).

#define assert_disabled(e)      ((void)sizeof(e))

How does sizeof help here in finding if a field is 0 or 1?

Can someone explain this with a working example?

like image 250
gjain Avatar asked Dec 26 '22 15:12

gjain


2 Answers

I'm pretty sure this macro is just being used when assertions are turned off. The trick of using ((void)sizeof(e)) instead of just (void)0 or similar is clever: it avoids evaluating e (mostly), but still has the compiler check that e is a valid expression, so you won't get surprise compile errors when you change the definition to turn assertions on.

like image 102
R.. GitHub STOP HELPING ICE Avatar answered Jan 09 '23 07:01

R.. GitHub STOP HELPING ICE


This is kind of blackmagic I saw in Linux Kernel codes.

Its used to check expression 'e' at compile time.

like image 29
Aniket Inge Avatar answered Jan 09 '23 08:01

Aniket Inge