container_of

Manish Katiyar mkatiyar at gmail.com
Sat Jan 17 11:58:13 EST 2015


on Sat, Jan 17, 2015 at 8:32 AM, Simon Brand <simon.brand at postadigitale.de>
wrote:

> Good evening,
>
> i read the article about the container_of function:
> http://www.kroah.com/log/linux/container_of.html
>
>  I understand what it does and how it works.
>
> I thought, it could be simplified by replacing it through this:
>
> #define container_of(ptr, type, member) ({ \
>                 (type *)( (char *)ptr - offsetof(type,member) );})
>
> Original:
> #define container_of(ptr, type, member) ({ \
>                 const typeof( ((type *)0)->member ) *__mptr = (ptr);
>                 (type *)( (char *)__mptr - offsetof(type,member) );})
>
>
> ptr has the type of a pointer to the member, which should be the same
> as __mptr? The value should although be the same.
>
> First I tried it in a self written script, then replaced it in
> include/linux/kernel.h and compiled it as usermode linux -> working
> well.
>
> Then I compiled it and run it in a VM, but it is not working.
>
> Can you please explain to me, why the original version is always working
> and "mine" is not?
>


Have you searched through archives. Exactly 7 years ago, I had the same
question.

http://comments.gmane.org/gmane.linux.kernel.kernelnewbies/24141

Thanks -
Manish




> _______________________________________________
> Kernelnewbies mailing list
> Kernelnewbies at kernelnewbies.org
> http://lists.kernelnewbies.org/mailman/listinfo/kernelnewbies
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.kernelnewbies.org/pipermail/kernelnewbies/attachments/20150117/d27a94a6/attachment.html 


More information about the Kernelnewbies mailing list