container_of的实现

面试的过程中,container_of的实现很重要:

#define container_of(ptr, type, member) ({              \        

const typeof( ((type *)0)->member ) *__mptr = (ptr);    \        

(type *)( (char *)__mptr - offsetof(type,member) );})    

offserof:

#define offsetof(TYPE, MEMBER) ((size_t) &((TYPE *)0)->MEMBER)

 

发布了236 篇原创文章 · 获赞 16 · 访问量 24万+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章