Two classic macro definitions under Linux

Source: Internet
Author: User

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 blog to pave the groundwork.

Offsetof MacroDefinition:

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);p rintf ("Gender_offset =%d\n", gender_offset);p rintf ("Id_offset =%d\n", id_offset); printf ("Age_offset =%d\n", age_offset);p rintf ("Name_offset =%d\n", name_offset); System ("pause"); return 0;} Result:/*gender_offset = 0 Id_offset = 4//byte to its age_offset = 8name_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


Use Example: NOTE: The AddressOf macro definition in this example is the same as the macro definition above, and is just a rewrite.
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)/* Based on "pointer (PTR) of domain member variable (MEMBER)" in "struct (type) variable" To get a pointer to the entire struct variable */#define AddressOf (Pt,type,member) ((TYPE *) ((char *) (PT)-Offsetof (type,member)) struct student{ Char gender;int id;int Age;char name[20];}; void Main () {struct student stu;struct student *pstu;stu.gender = ' 1 '; stu.id = 9527;stu.age = 24;strcpy (Stu.name, "zhouxing Xing "), or" address of struct "based on" ID address ". Pstu = AddressOf (& (Stu.id), struct student, id);//access to other Members printf ("Student gender="), based on the address of the obtained struct%c\n, pstu-> gender);p rintf ("age=%d\n", pstu->age);p rintf ("Name=%s\n", pstu->name);} /* Results gender= 1age= 24name= zhouxingxinggender= 1age= 24name= zhouxingxing*/

Container_of diagram

Type is a struct, which represents "whole", while member is a member, it is a part of the whole, and the address of member is known.

Finally, you can think about what this macro does.

Copyright NOTICE: This article for Bo Master original article, without Bo Master permission not reproduced.

Two classic macro definitions under Linux

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.