Sign in to follow this  
Storyyeller

c typechecking macro with statements within extensions

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?

[code]/*
* 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; \
})[/code]

Share this post


Link to post
Share on other sites
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?

[code]
/**
* 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)
[/code]

Share this post


Link to post

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this