5

instead of defining container_of as:

#define container_of(ptr, type, member) ({ \
            const typeof( ((type *)0)->member ) *__mptr = (ptr);
            (type *)( (char *)__mptr - offsetof(type,member) );})

Why won't this simply work :

#define container_of(ptr, type, member) ({ \
                    (type *)( (char *)(ptr) - offsetof(type,member) );})

what's the usage of first line in the definition?

1 Answer 1

8

It adds some level of type safety. With your second version, I could pass anything in for ptr and it would compile fine. With the kernel's version, you'll at least get a warning if you pass in a pointer for ptr that doesn't match the type of type.member.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.