Public Group

# c typechecking macro with statements within extensions

This topic is 2571 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

I've come across a strange macro in some C code and I can't understand how it works. I've figured out that the {} thing is a GCC extension which lets you put statements within expressions but the rest doesn't make any sense. If __dummy and __dummy2 are seperate objects, how can they ever have the same address? And how does the comparison affect anything?

/* * Check at compile time that something is of a particular type. * Always evaluates to 1 so you may use it easily in comparisons. */ #define typecheck(type,x) \ ({ type __dummy; \ typeof(x) __dummy2; \ (void)(&__dummy == &__dummy2); \ 1; \ })

##### Share on other sites
The address comparison will cause a compiler error if the type of x and type are different. That's all that the comparison is for.

##### Share on other sites
Hidden
Also, is there any advantage to macros like that over inline functions? It seems to me like C programmers are allergic to functions.

Hidden
I found another macro that doesn't make sense. It's supposed to loop over a linked list, but as far as I can tell, it never reaches the value head. Why does it start with pos = (head)->next and not just pos = (head)? Am I missing something?

 /** * list_for_each - iterate over a list * @pos: the &struct list_head to use as a loop cursor. * @head: the head for your list. */ #define list_for_each(pos, head) \ for (pos = (head)->next; prefetch(pos->next), pos != (head); \ pos = pos->next) 

1. 1
2. 2
Rutin
19
3. 3
4. 4
5. 5

• 15
• 13
• 9
• 12
• 10
• ### Forum Statistics

• Total Topics
631442
• Total Posts
3000089
×