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萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章