Http://www.linuxidc.com/Linux/2016-08/134481.htm
http://blog.csdn.net/npy_lp/article/details/7010752
http://www.linuxdiyf.com/viewarticle.php?id=104768
This article first introduces Linux under the Classic macro definition, feel the wisdom of the geek, and then according to the classic definition for the next article to pave the groundwork.
OFFSETOF macro Definition:
Gets the offset of the variable member (MEMBER) of the struct body (TYPE) in this struct.
#define OFFSETOF (Type, MEMBER) ((size_t) & ((TYPE *) 0)->member)
Description: Gets the offset of the variable member (MEMBER) of the struct body (TYPE) in this struct.
1. ((TYPE *) 0) transforms 0 to a type pointer, that is, the address of a pointer of type types is 0.
2. ((TYPE *) 0)->member access to data members in the structure.
3.& (((TYPE *) 0)->member) takes out the address of the data member. Because the address of type is 0, the address obtained here is the offset of the relative member in type.
4. (size_t) (& ((type*) 0)->member) result conversion type. For 32-bit systems, size_t is the unsigned int type, and for 64-bit systems, size_t is the unsigned long type.
Examples of Use:
struct student
{
char gender;
int id;
int age;
Char name[20];
};
int _tmain (int argc, _tchar* argv[])
{
int Gender_offset, Id_offset, Age_offset, Name_offset;
Gender_offset = offsetof (struct student, gender);
Id_offset = offsetof (struct student, id);
Age_offset = offsetof (struct student, age);
Name_offset = offsetof (struct student, name);
printf ("Gender_offset =%d\n", gender_offset);
printf ("Id_offset =%d\n", id_offset);
printf ("Age_offset =%d\n", age_offset);
printf ("Name_offset =%d\n", name_offset);
System ("pause");
return 0;
}
Results:
/*
Gender_offset = 0
Id_offset = 4//byte on its
Age_offset = 8
Name_offset = 12
*/
Offsetof diagram
Type is a struct, which represents "whole", while member is a member, which is a part of the whole.
CONTAINER_OF macro Definition:
#define CONTAINER_OF (PTR, type, member) ({\
Const typeof (((type *) 0)->member) *__mptr = (PTR); \
(Type *) ((char *) __mptr-offsetof (Type,member));})
Description: Gets a pointer to the entire struct variable, based on the pointer (PTR) of the domain member variable (member) in the struct (type) variable.
1. typeof (((type *) 0)->member) takes out the variable type of the member member.
2.const typeof (((type *) 0)->member) *__mptr = (PTR) defines the variable __mptr pointer and assigns PTR to __mptr. After this step, __mptr is a constant pointer to the member data type, which points to the address that PTR points to.
3. (char *) __MPTR converts the __mptr to a byte-type pointer.
4. Offsetof (Type,member)) is to get the position offset of "member member" in "struct type".
5. (char *) __mptr-offsetof (Type,member)) is the starting address (a char * type pointer) for the pointer that is used to get "struct type".
6. (Type *) ((char *) __mptr-offsetof (Type,member)) is the "pointer to struct type" of type "char *" to "pointer to struct type of type *".
7. Backslash "/" indicates row connection
Two classic macro definitions in Linux go